From cc89918afbe9e930073d1611e1e5cc42f3e39f2c Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Mon, 22 Jul 2024 17:02:23 -0700 Subject: [PATCH 1/5] Add way to escape side effects within an entrypoint --- libs/langchain-scripts/src/build_v2.ts | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/libs/langchain-scripts/src/build_v2.ts b/libs/langchain-scripts/src/build_v2.ts index e8764f6b48e5..ff705901e9b4 100644 --- a/libs/langchain-scripts/src/build_v2.ts +++ b/libs/langchain-scripts/src/build_v2.ts @@ -441,16 +441,16 @@ async function checkTreeShaking(config: LangChainConfig) { ); const externals = listExternals(packageJson, config?.internals ?? []); const entrypoints = listEntrypoints(packageJson); - const consoleLog = console.log; - /** @type {Map} */ + const consoleInfo = console.info; + /** @type {Map} */ const reportMap = new Map(); for (const entrypoint of entrypoints) { let sideEffects = ""; - console.log = function (...args) { + console.info = function (...args) { const line = args.length ? args.join(" ") : ""; - if (line.trim().startsWith("First side effect in")) { + if (line.includes("First side effect in")) { sideEffects += `${line}\n`; } }; @@ -461,17 +461,25 @@ async function checkTreeShaking(config: LangChainConfig) { experimentalLogSideEffects: true, }); + let hasUnexpectedSideEffects = sideEffects.length > 0; + if (hasUnexpectedSideEffects) { + const entrypointContent = fs.readFileSync(entrypoint); + // Allow escaping side effects strictly within code directly + // within an entrypoints + hasUnexpectedSideEffects = + !entrypointContent.toString().includes("/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */"); + } reportMap.set(entrypoint, { log: sideEffects, - hasSideEffects: sideEffects.length > 0, + hasUnexpectedSideEffects, }); } - console.log = consoleLog; + console.info = consoleInfo; let failed = false; for (const [entrypoint, report] of reportMap) { - if (report.hasSideEffects) { + if (report.hasUnexpectedSideEffects) { failed = true; console.log("---------------------------------"); console.log(`Tree shaking failed for ${entrypoint}`); @@ -480,7 +488,7 @@ async function checkTreeShaking(config: LangChainConfig) { } if (failed) { - process.exit(1); + throw new Error("Tree shaking checks failed."); } else { console.log("Tree shaking checks passed!"); } From 022a5410a198720effd2368262048932b34b2113 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Mon, 22 Jul 2024 17:05:37 -0700 Subject: [PATCH 2/5] Fix format --- libs/langchain-scripts/src/build_v2.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libs/langchain-scripts/src/build_v2.ts b/libs/langchain-scripts/src/build_v2.ts index ff705901e9b4..6cad0b39b2b9 100644 --- a/libs/langchain-scripts/src/build_v2.ts +++ b/libs/langchain-scripts/src/build_v2.ts @@ -466,8 +466,9 @@ async function checkTreeShaking(config: LangChainConfig) { const entrypointContent = fs.readFileSync(entrypoint); // Allow escaping side effects strictly within code directly // within an entrypoints - hasUnexpectedSideEffects = - !entrypointContent.toString().includes("/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */"); + hasUnexpectedSideEffects = !entrypointContent + .toString() + .includes("/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */"); } reportMap.set(entrypoint, { log: sideEffects, From 3290bec1f01c90a67d6f4f1485878d81fbb459a3 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Mon, 22 Jul 2024 17:51:16 -0700 Subject: [PATCH 3/5] Add to all old versions --- libs/langchain-scripts/bin/build.js | 24 +++++++++++------ libs/langchain-scripts/package.json | 2 +- libs/langchain-scripts/src/build_v2.ts | 4 ++- .../src/check-tree-shaking.ts | 27 +++++++++++++------ 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/libs/langchain-scripts/bin/build.js b/libs/langchain-scripts/bin/build.js index 172d1fa8fc67..c2e012ec2193 100755 --- a/libs/langchain-scripts/bin/build.js +++ b/libs/langchain-scripts/bin/build.js @@ -225,16 +225,15 @@ async function listExternals( export async function checkTreeShaking(options) { const externals = await listExternals(options?.extraInternals ?? []); const entrypoints = await listEntrypoints(); - const consoleLog = console.log; - /** @type {Map} */ + const consoleInfo = console.info; const reportMap = new Map(); for (const entrypoint of entrypoints) { let sideEffects = ""; - console.log = function (...args) { + console.info = function (...args) { const line = args.length ? args.join(" ") : ""; - if (line.trim().startsWith("First side effect in")) { + if (line.includes("First side effect in")) { sideEffects += `${line}\n`; } }; @@ -245,17 +244,26 @@ export async function checkTreeShaking(options) { experimentalLogSideEffects: true, }); + 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 entrypoints + hasUnexpectedSideEffects = !entrypointContent + .toString() + .includes("/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */"); + } reportMap.set(entrypoint, { log: sideEffects, - hasSideEffects: sideEffects.length > 0, + hasUnexpectedSideEffects, }); } - console.log = consoleLog; + console.info = consoleInfo; let failed = false; for (const [entrypoint, report] of reportMap) { - if (report.hasSideEffects) { + if (report.hasUnexpectedSideEffects) { failed = true; console.log("---------------------------------"); console.log(`Tree shaking failed for ${entrypoint}`); @@ -264,7 +272,7 @@ export async function checkTreeShaking(options) { } if (failed) { - process.exit(1); + throw new Error("Tree shaking checks failed."); } else { console.log("Tree shaking checks passed!"); } diff --git a/libs/langchain-scripts/package.json b/libs/langchain-scripts/package.json index efcba3c1f946..ffa7187fece6 100644 --- a/libs/langchain-scripts/package.json +++ b/libs/langchain-scripts/package.json @@ -1,6 +1,6 @@ { "name": "@langchain/scripts", - "version": "0.0.15", + "version": "0.0.18", "description": "Shared scripts for LangChain.js", "type": "module", "engines": { diff --git a/libs/langchain-scripts/src/build_v2.ts b/libs/langchain-scripts/src/build_v2.ts index 6cad0b39b2b9..88298ce9b5ba 100644 --- a/libs/langchain-scripts/src/build_v2.ts +++ b/libs/langchain-scripts/src/build_v2.ts @@ -463,7 +463,9 @@ async function checkTreeShaking(config: LangChainConfig) { let hasUnexpectedSideEffects = sideEffects.length > 0; if (hasUnexpectedSideEffects) { - const entrypointContent = fs.readFileSync(entrypoint); + const entrypointContent = await fs.promises.readFile( + `./dist/${entrypoint.replace(/^\.\//, "")}` + ); // Allow escaping side effects strictly within code directly // within an entrypoints hasUnexpectedSideEffects = !entrypointContent diff --git a/libs/langchain-scripts/src/check-tree-shaking.ts b/libs/langchain-scripts/src/check-tree-shaking.ts index d5f724607948..479d557a5634 100644 --- a/libs/langchain-scripts/src/check-tree-shaking.ts +++ b/libs/langchain-scripts/src/check-tree-shaking.ts @@ -60,21 +60,21 @@ async function listExternals( export async function checkTreeShaking(options?: TreeShakingArgs) { const externals = await listExternals(options?.extraInternals ?? []); const entrypoints = await listEntrypoints(); - const consoleLog = console.log; + const consoleInfo = console.info; const reportMap: Map< string, { log: string; - hasSideEffects: boolean; + hasUnexpectedSideEffects: boolean; } > = new Map(); for (const entrypoint of entrypoints) { let sideEffects = ""; - console.log = function (...args) { + console.info = function (...args) { const line = args.length ? args.join(" ") : ""; - if (line.trim().startsWith("First side effect in")) { + if (line.includes("First side effect in")) { sideEffects += `${line}\n`; } }; @@ -85,17 +85,28 @@ export async function checkTreeShaking(options?: TreeShakingArgs) { experimentalLogSideEffects: true, }); + let hasUnexpectedSideEffects = sideEffects.length > 0; + if (hasUnexpectedSideEffects) { + const entrypointContent = await fs.readFile( + `./dist/${entrypoint.replace(/^\.\//, "")}` + ); + // Allow escaping side effects strictly within code directly + // within an entrypoints + hasUnexpectedSideEffects = !entrypointContent + .toString() + .includes("/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */"); + } reportMap.set(entrypoint, { log: sideEffects, - hasSideEffects: sideEffects.length > 0, + hasUnexpectedSideEffects, }); } - console.log = consoleLog; + console.info = consoleInfo; let failed = false; for (const [entrypoint, report] of reportMap) { - if (report.hasSideEffects) { + if (report.hasUnexpectedSideEffects) { failed = true; console.log("---------------------------------"); console.log(`Tree shaking failed for ${entrypoint}`); @@ -104,7 +115,7 @@ export async function checkTreeShaking(options?: TreeShakingArgs) { } if (failed) { - process.exit(1); + throw new Error("Tree shaking checks failed."); } else { console.log("Tree shaking checks passed!"); } From 7a8fd7c5f9c603a2426e77735a69dc7504dea759 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Mon, 22 Jul 2024 17:52:42 -0700 Subject: [PATCH 4/5] Fix comment --- libs/langchain-scripts/bin/build.js | 2 +- libs/langchain-scripts/src/build_v2.ts | 2 +- libs/langchain-scripts/src/check-tree-shaking.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libs/langchain-scripts/bin/build.js b/libs/langchain-scripts/bin/build.js index c2e012ec2193..40b80ac0d611 100755 --- a/libs/langchain-scripts/bin/build.js +++ b/libs/langchain-scripts/bin/build.js @@ -248,7 +248,7 @@ export async function checkTreeShaking(options) { if (hasUnexpectedSideEffects) { const entrypointContent = await fs.promises.readFile(`./dist/${entrypoint.replace(/^\.\//, "")}`); // Allow escaping side effects strictly within code directly - // within an entrypoints + // within an entrypoint hasUnexpectedSideEffects = !entrypointContent .toString() .includes("/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */"); diff --git a/libs/langchain-scripts/src/build_v2.ts b/libs/langchain-scripts/src/build_v2.ts index 88298ce9b5ba..e43e738c4f3f 100644 --- a/libs/langchain-scripts/src/build_v2.ts +++ b/libs/langchain-scripts/src/build_v2.ts @@ -467,7 +467,7 @@ async function checkTreeShaking(config: LangChainConfig) { `./dist/${entrypoint.replace(/^\.\//, "")}` ); // Allow escaping side effects strictly within code directly - // within an entrypoints + // within an entrypoint hasUnexpectedSideEffects = !entrypointContent .toString() .includes("/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */"); diff --git a/libs/langchain-scripts/src/check-tree-shaking.ts b/libs/langchain-scripts/src/check-tree-shaking.ts index 479d557a5634..cabec3b60200 100644 --- a/libs/langchain-scripts/src/check-tree-shaking.ts +++ b/libs/langchain-scripts/src/check-tree-shaking.ts @@ -91,7 +91,7 @@ export async function checkTreeShaking(options?: TreeShakingArgs) { `./dist/${entrypoint.replace(/^\.\//, "")}` ); // Allow escaping side effects strictly within code directly - // within an entrypoints + // within an entrypoint hasUnexpectedSideEffects = !entrypointContent .toString() .includes("/* __LC_ALLOW_ENTRYPOINT_SIDE_EFFECTS__ */"); From 6eb16e55de017e98fbbfcb0ff3dc3fba6d465c7f Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Mon, 22 Jul 2024 18:10:14 -0700 Subject: [PATCH 5/5] Remove hard error --- libs/langchain-scripts/src/build_v2.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libs/langchain-scripts/src/build_v2.ts b/libs/langchain-scripts/src/build_v2.ts index e43e738c4f3f..a07ae6ec9074 100644 --- a/libs/langchain-scripts/src/build_v2.ts +++ b/libs/langchain-scripts/src/build_v2.ts @@ -491,7 +491,8 @@ async function checkTreeShaking(config: LangChainConfig) { } if (failed) { - throw new Error("Tree shaking checks failed."); + // TODO: Throw a hard error here + console.log("Tree shaking checks failed."); } else { console.log("Tree shaking checks passed!"); }