Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build[patch]: Add way to escape side effects within an entrypoint #6174

Merged
merged 5 commits into from
Jul 23, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions libs/langchain-scripts/src/build_v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, { log: string; hasSideEffects: boolean; }>} */
const consoleInfo = console.info;
/** @type {Map<string, { log: string; hasUnexpectedSideEffects: boolean; }>} */
const reportMap = new Map();
jacoblee93 marked this conversation as resolved.
Show resolved Hide resolved

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`;
}
};
Expand All @@ -461,17 +461,26 @@ async function checkTreeShaking(config: LangChainConfig) {
experimentalLogSideEffects: true,
});

let hasUnexpectedSideEffects = sideEffects.length > 0;
if (hasUnexpectedSideEffects) {
const entrypointContent = fs.readFileSync(entrypoint);
jacoblee93 marked this conversation as resolved.
Show resolved Hide resolved
// Allow escaping side effects strictly within code directly
// within an entrypoints
jacoblee93 marked this conversation as resolved.
Show resolved Hide resolved
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}`);
Expand All @@ -480,7 +489,7 @@ async function checkTreeShaking(config: LangChainConfig) {
}

if (failed) {
process.exit(1);
throw new Error("Tree shaking checks failed.");
jacoblee93 marked this conversation as resolved.
Show resolved Hide resolved
} else {
console.log("Tree shaking checks passed!");
}
Expand Down
Loading