Skip to content

Commit

Permalink
scripts[patch]: Fix bug for checking allowed side effects comment (#6579
Browse files Browse the repository at this point in the history
)

* scripts[patch]: Fix bug for checking allowed side effects comment

* fix lint
  • Loading branch information
bracesproul authored Aug 20, 2024
1 parent ac80c99 commit 306f83b
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions libs/langchain-scripts/src/build_v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,40 @@ function listEntrypoints(packageJson: Record<string, any>) {
return entrypoints;
}

/**
* Checks whether or not the file has side effects marked with the `__LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__`
* keyword comment. If it does, this function will return `true`, otherwise it will return `false`.
*
* @param {string} entrypoint
* @returns {Promise<boolean>} Whether or not the file has side effects which are explicitly marked as allowed.
*/
const checkAllowSideEffects = async (entrypoint: string): Promise<boolean> => {
let entrypointContent: Buffer | undefined;
try {
entrypointContent = await fs.promises.readFile(
`./dist/${entrypoint.replace(/^\.\//, "")}`
);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (e: any) {
if (e.message.includes("ENOENT")) {
// Entrypoint is likely via an `index.js` file, retry with `index.js` appended to path
entrypointContent = await fs.promises.readFile(
`./dist/${entrypoint
.replace(/^\.\//, "")
.replace(/\.js$/, "")}/index.js`
);
}
}

// Allow escaping side effects strictly within code directly
// within an entrypoint
return entrypointContent
? entrypointContent
.toString()
.includes("/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */")
: false;
};

async function checkTreeShaking(config: LangChainConfig) {
const packageJson = JSON.parse(
await fs.promises.readFile("package.json", "utf8")
Expand Down Expand Up @@ -508,14 +542,7 @@ async function checkTreeShaking(config: LangChainConfig) {

let hasUnexpectedSideEffects = sideEffects.length > 0;
if (hasUnexpectedSideEffects) {
const entrypointContent = await fs.promises.readFile(
`./dist/${entrypoint.replace(/^\.\//, "")}`
);
// Allow escaping side effects strictly within code directly
// within an entrypoint
hasUnexpectedSideEffects = !entrypointContent
.toString()
.includes("/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */");
hasUnexpectedSideEffects = !(await checkAllowSideEffects(entrypoint));
}
reportMap.set(entrypoint, {
log: sideEffects,
Expand Down

0 comments on commit 306f83b

Please sign in to comment.