You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(builders): avoid false-positive node-module errors for step-only usage in shared modules (vercel#1821)
The workflow-node-module-error plugin records violations in onResolve, which
runs before esbuild's tree-shaker. A shared module with both a workflow-safe
export and a step-only export that imports node:* would be flagged even when
the workflow never reached the step-only export.
Mark node:/bun: imports as sideEffects: false so esbuild can tree-shake them
when unused in surviving code, enable metafile, and filter recorded violations
in onEnd against outputs[].imports so only builtins that actually survive in
the emitted bundle are reported.
Fixesvercel#1817
// Different messages for built-in modules vs npm packages that use them
442
-
consttext=isBuiltinModule
443
-
? `You are attempting to use "${violation.packageName}" which is a ${moduleType} module. ${moduleType} modules are not available in workflow functions.\n\nLearn more: https://workflow-sdk.dev/err/${ERROR_SLUGS.NODE_JS_MODULE_IN_WORKFLOW}`
444
-
: `You are attempting to use "${violation.packageName}" which depends on ${moduleType} modules. Packages that depend on ${moduleType} modules are not available in workflow functions.\n\nLearn more: https://workflow-sdk.dev/err/${ERROR_SLUGS.NODE_JS_MODULE_IN_WORKFLOW}`;
445
-
446
-
return{
447
-
text,
448
-
location: violation.location
449
-
? {
450
-
...violation.location,
451
-
suggestion: 'Move this function into a step function.',
452
-
}
453
-
: undefined,
454
-
};
455
-
}),
456
-
};
450
+
// Report violations at the end of the build, filtered to only those
451
+
// whose Node.js / Bun builtin imports actually survived tree-shaking.
452
+
//
453
+
// `onResolve` fires during module loading, before esbuild's tree-shaker
454
+
// runs. That means a shared module whose step-only export references a
455
+
// builtin will record a violation even if the workflow never touches
456
+
// that export. By marking builtin imports as `sideEffects: false` in
457
+
// `onResolve` above, esbuild can drop such imports from the output.
458
+
// Here we consult the metafile to see which builtin imports remain
459
+
// in the emitted bundle and only report those.
460
+
build.onEnd((result)=>{
461
+
if(packageViolations.length===0)return;
462
+
463
+
constsurvivingBuiltins=newSet<string>();
464
+
constoutputs=result.metafile?.outputs;
465
+
if(outputs){
466
+
for(constoutputofObject.values(outputs)){
467
+
for(constimpofoutput.imports){
468
+
// External imports in the metafile are recorded with the path
469
+
// that was returned from `onResolve` (e.g. "node:fs/promises",
470
+
// "fs", "bun:sqlite"). Only collect the ones that match
// Different messages for built-in modules vs npm packages that use them
498
+
consttext=isBuiltinModule
499
+
? `You are attempting to use "${violation.packageName}" which is a ${moduleType} module. ${moduleType} modules are not available in workflow functions.\n\nLearn more: https://workflow-sdk.dev/err/${ERROR_SLUGS.NODE_JS_MODULE_IN_WORKFLOW}`
500
+
: `You are attempting to use "${violation.packageName}" which depends on ${moduleType} modules. Packages that depend on ${moduleType} modules are not available in workflow functions.\n\nLearn more: https://workflow-sdk.dev/err/${ERROR_SLUGS.NODE_JS_MODULE_IN_WORKFLOW}`;
501
+
502
+
return{
503
+
text,
504
+
location: violation.location
505
+
? {
506
+
...violation.location,
507
+
suggestion: 'Move this function into a step function.',
0 commit comments