Skip to content

fix(swc-plugin): closure variable detection for new expressions and module-level declarations#1368

Open
TooTallNate wants to merge 9 commits intomainfrom
fix/swc-closure-variable-bugs
Open

fix(swc-plugin): closure variable detection for new expressions and module-level declarations#1368
TooTallNate wants to merge 9 commits intomainfrom
fix/swc-closure-variable-bugs

Conversation

@TooTallNate
Copy link
Member

@TooTallNate TooTallNate commented Mar 13, 2026

Summary

Fixes all four SWC compiler plugin bugs from #1365 related to closure variables:

  • Bug 1: new expressions (e.g. new MockLanguageModelV3(...args)) were not analyzed for closure variables because ClosureVariableCollector::collect_from_expr had no Expr::New case. Arguments and callee identifiers were silently missed.
  • Bug 2: Nested step functions in the step bundle were replaced with bare references to the hoisted copy, breaking direct (non-workflow) calls because __private_getClosureVars() has no values outside workflow context. Now the original function body is preserved inline (directive stripped) so JavaScript's normal closure semantics work for direct calls.
  • Bug 3: Module-level declarations (functions, variables, classes) were incorrectly captured as closure variables, since only import bindings were excluded. This caused runtime failures (serializing functions) and prevented DCE from removing step-only helpers and their imports from the workflow bundle.
  • Bug 4: Closure variables used deeply inside nested function/method bodies (e.g., inside a ReadableStream's start() method) were not detected because the collector skipped nested function/arrow/method bodies entirely. Now it walks into them while properly tracking inner parameters as local vars.

Additionally, the closure variable collector was expanded to handle many more AST patterns that were previously falling through silently (Prop::Shorthand, OptChain, Seq, Yield, Throw, Try/Catch/Finally, Switch, ForIn, ForOf, DoWhile, Labeled, computed property keys). Common Web API globals (ReadableStream, WritableStream, TransformStream, etc.) were added to the known globals list.

Changes

  • packages/swc-plugin-workflow/transform/src/lib.rs:
    • Add Expr::New arm to closure variable collector
    • Pass declared_identifiers to collect_from_function/collect_from_arrow_expr so module-level declarations are excluded
    • Preserve original step function bodies in enclosing functions (step mode) instead of replacing with bare hoisted references
    • Walk into nested function/arrow/method/getter/setter bodies for closure variable detection
    • Add handling for Prop::Shorthand, OptChain, Seq, Yield, computed keys, Throw, Try, Switch, ForIn, ForOf, DoWhile, Labeled
    • Add ReadableStream, WritableStream, TransformStream, and other Web API globals to known globals list
  • packages/swc-plugin-workflow/spec.md: Document all new behaviors
  • New test fixture closure-new-expr-and-module-declarations with 15 test cases
  • Updated fixture expectations across 10+ existing fixtures

All 172 tests pass with no regressions.

Fixes #1365
Fixes #1369

… module-level declarations

Fix two SWC compiler plugin bugs related to closure variable detection:

1. Add Expr::New handling to ClosureVariableCollector so `new Class(...args)`
   properly captures both the callee and arguments as closure variables.

2. Exclude module-level declarations (functions, variables, classes) from
   closure variable detection, preventing over-capturing of identifiers
   that are already available in all bundles. This also allows DCE to
   properly remove step-only helpers and their imports from the workflow
   bundle.

Fixes #1365
@TooTallNate TooTallNate requested a review from a team as a code owner March 13, 2026 06:11
Copilot AI review requested due to automatic review settings March 13, 2026 06:11
@changeset-bot
Copy link

changeset-bot bot commented Mar 13, 2026

🦋 Changeset detected

Latest commit: 796c53a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 17 packages
Name Type
@workflow/swc-plugin Patch
@workflow/astro Patch
@workflow/builders Patch
@workflow/cli Patch
@workflow/nest Patch
@workflow/next Patch
@workflow/nitro Patch
@workflow/rollup Patch
@workflow/sveltekit Patch
workflow Patch
@workflow/vite Patch
@workflow/vitest Patch
@workflow/world-testing Patch
@workflow/nuxt Patch
@workflow/ai Patch
@workflow/core Patch
@workflow/web-shared Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link
Contributor

vercel bot commented Mar 13, 2026

@github-actions
Copy link
Contributor

github-actions bot commented Mar 13, 2026

🧪 E2E Test Results

Some tests failed

Summary

Passed Failed Skipped Total
✅ ▲ Vercel Production 560 0 67 627
✅ 💻 Local Development 600 0 84 684
✅ 📦 Local Production 600 0 84 684
✅ 🐘 Local Postgres 600 0 84 684
✅ 🪟 Windows 54 0 3 57
❌ 🌍 Community Worlds 116 55 15 186
❌ 📋 Other 143 1 27 171
Total 2673 56 364 3093

❌ Failed Tests

🌍 Community Worlds (55 failed)

mongodb (3 failed):

  • hookWorkflow is not resumable via public webhook endpoint
  • webhookWorkflow
  • concurrent hook token conflict - two workflows cannot use the same hook token simultaneously

redis (2 failed):

  • hookWorkflow is not resumable via public webhook endpoint
  • concurrent hook token conflict - two workflows cannot use the same hook token simultaneously

turso (50 failed):

  • addTenWorkflow
  • addTenWorkflow
  • wellKnownAgentWorkflow (.well-known/agent)
  • should work with react rendering in step
  • promiseAllWorkflow
  • promiseRaceWorkflow
  • promiseAnyWorkflow
  • importedStepOnlyWorkflow
  • hookWorkflow
  • hookWorkflow is not resumable via public webhook endpoint
  • webhookWorkflow
  • sleepingWorkflow
  • parallelSleepWorkflow
  • nullByteWorkflow
  • workflowAndStepMetadataWorkflow
  • fetchWorkflow
  • promiseRaceStressTestWorkflow
  • error handling error propagation workflow errors nested function calls preserve message and stack trace
  • error handling error propagation workflow errors cross-file imports preserve message and stack trace
  • error handling error propagation step errors basic step error preserves message and stack trace
  • error handling error propagation step errors cross-file step error preserves message and function names in stack
  • error handling retry behavior regular Error retries until success
  • error handling retry behavior FatalError fails immediately without retries
  • error handling retry behavior RetryableError respects custom retryAfter delay
  • error handling retry behavior maxRetries=0 disables retries
  • error handling catchability FatalError can be caught and detected with FatalError.is()
  • hookCleanupTestWorkflow - hook token reuse after workflow completion
  • concurrent hook token conflict - two workflows cannot use the same hook token simultaneously
  • hookDisposeTestWorkflow - hook token reuse after explicit disposal while workflow still running
  • stepFunctionPassingWorkflow - step function references can be passed as arguments (without closure vars)
  • stepFunctionWithClosureWorkflow - step function with closure variables passed as argument
  • closureVariableWorkflow - nested step functions with closure variables
  • spawnWorkflowFromStepWorkflow - spawning a child workflow using start() inside a step
  • health check (queue-based) - workflow and step endpoints respond to health check messages
  • pathsAliasWorkflow - TypeScript path aliases resolve correctly
  • Calculator.calculate - static workflow method using static step methods from another class
  • AllInOneService.processNumber - static workflow method using sibling static step methods
  • ChainableService.processWithThis - static step methods using this to reference the class
  • thisSerializationWorkflow - step function invoked with .call() and .apply()
  • customSerializationWorkflow - custom class serialization with WORKFLOW_SERIALIZE/WORKFLOW_DESERIALIZE
  • instanceMethodStepWorkflow - instance methods with "use step" directive
  • crossContextSerdeWorkflow - classes defined in step code are deserializable in workflow context
  • stepFunctionAsStartArgWorkflow - step function reference passed as start() argument
  • cancelRun - cancelling a running workflow
  • cancelRun via CLI - cancelling a running workflow
  • pages router addTenWorkflow via pages router
  • pages router promiseAllWorkflow via pages router
  • pages router sleepingWorkflow via pages router
  • hookWithSleepWorkflow - hook payloads delivered correctly with concurrent sleep
  • sleepWithSequentialStepsWorkflow - sequential steps work with concurrent sleep (control)
📋 Other (1 failed)

e2e-local-postgres-nest-stable (1 failed):

  • webhookWorkflow

Details by Category

✅ ▲ Vercel Production
App Passed Failed Skipped
✅ astro 50 0 7
✅ example 50 0 7
✅ express 50 0 7
✅ fastify 50 0 7
✅ hono 50 0 7
✅ nextjs-turbopack 55 0 2
✅ nextjs-webpack 55 0 2
✅ nitro 50 0 7
✅ nuxt 50 0 7
✅ sveltekit 50 0 7
✅ vite 50 0 7
✅ 💻 Local Development
App Passed Failed Skipped
✅ astro-stable 48 0 9
✅ express-stable 48 0 9
✅ fastify-stable 48 0 9
✅ hono-stable 48 0 9
✅ nextjs-turbopack-canary 54 0 3
✅ nextjs-turbopack-stable 54 0 3
✅ nextjs-webpack-canary 54 0 3
✅ nextjs-webpack-stable 54 0 3
✅ nitro-stable 48 0 9
✅ nuxt-stable 48 0 9
✅ sveltekit-stable 48 0 9
✅ vite-stable 48 0 9
✅ 📦 Local Production
App Passed Failed Skipped
✅ astro-stable 48 0 9
✅ express-stable 48 0 9
✅ fastify-stable 48 0 9
✅ hono-stable 48 0 9
✅ nextjs-turbopack-canary 54 0 3
✅ nextjs-turbopack-stable 54 0 3
✅ nextjs-webpack-canary 54 0 3
✅ nextjs-webpack-stable 54 0 3
✅ nitro-stable 48 0 9
✅ nuxt-stable 48 0 9
✅ sveltekit-stable 48 0 9
✅ vite-stable 48 0 9
✅ 🐘 Local Postgres
App Passed Failed Skipped
✅ astro-stable 48 0 9
✅ express-stable 48 0 9
✅ fastify-stable 48 0 9
✅ hono-stable 48 0 9
✅ nextjs-turbopack-canary 54 0 3
✅ nextjs-turbopack-stable 54 0 3
✅ nextjs-webpack-canary 54 0 3
✅ nextjs-webpack-stable 54 0 3
✅ nitro-stable 48 0 9
✅ nuxt-stable 48 0 9
✅ sveltekit-stable 48 0 9
✅ vite-stable 48 0 9
✅ 🪟 Windows
App Passed Failed Skipped
✅ nextjs-turbopack 54 0 3
❌ 🌍 Community Worlds
App Passed Failed Skipped
✅ mongodb-dev 3 0 2
❌ mongodb 51 3 3
✅ redis-dev 3 0 2
❌ redis 52 2 3
✅ turso-dev 3 0 2
❌ turso 4 50 3
❌ 📋 Other
App Passed Failed Skipped
✅ e2e-local-dev-nest-stable 48 0 9
❌ e2e-local-postgres-nest-stable 47 1 9
✅ e2e-local-prod-nest-stable 48 0 9

📋 View full workflow run


Some E2E test jobs failed:

  • Vercel Prod: success
  • Local Dev: success
  • Local Prod: success
  • Local Postgres: failure
  • Windows: success

Check the workflow run for details.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 13, 2026

📊 Benchmark Results

📈 Comparing against baseline from main branch. Green 🟢 = faster, Red 🔺 = slower.

workflow with no steps

💻 Local Development

World Framework Workflow Time Wall Time Overhead Samples vs Fastest
💻 Local 🥇 Express 0.032s (-0.9%) 1.005s (~) 0.973s 10 1.00x
💻 Local Nitro 0.033s (+3.7%) 1.007s (~) 0.973s 10 1.03x
🐘 Postgres Express 0.040s (-22.5% 🟢) 1.011s (~) 0.971s 10 1.23x
💻 Local Next.js (Turbopack) 0.042s 1.005s 0.963s 10 1.31x
🐘 Postgres Nitro 0.043s (+4.9%) 1.011s (~) 0.969s 10 1.32x
🌐 Redis Next.js (Turbopack) 0.043s 1.006s 0.964s 10 1.33x
🌐 MongoDB Next.js (Turbopack) 0.103s 1.008s 0.905s 10 3.19x
🐘 Postgres Next.js (Turbopack) ⚠️ missing - - - -

▲ Production (Vercel)

World Framework Workflow Time Wall Time Overhead Samples vs Fastest
▲ Vercel 🥇 Nitro 0.446s (-23.9% 🟢) 2.386s (-7.4% 🟢) 1.940s 10 1.00x
▲ Vercel Next.js (Turbopack) 0.460s (-26.0% 🟢) 2.386s (-7.6% 🟢) 1.926s 10 1.03x
▲ Vercel Express 0.460s (~) 2.382s (-0.7%) 1.922s 10 1.03x

🔍 Observability: Nitro | Next.js (Turbopack) | Express

workflow with 1 step

💻 Local Development

World Framework Workflow Time Wall Time Overhead Samples vs Fastest
🐘 Postgres 🥇 Nitro 1.092s (~) 2.012s (~) 0.920s 10 1.00x
💻 Local Nitro 1.102s (~) 2.006s (~) 0.904s 10 1.01x
🌐 Redis Next.js (Turbopack) 1.103s 2.007s 0.904s 10 1.01x
💻 Local Express 1.105s (~) 2.006s (~) 0.902s 10 1.01x
💻 Local Next.js (Turbopack) 1.108s 2.005s 0.897s 10 1.01x
🐘 Postgres Express 1.110s (-1.1%) 2.019s (~) 0.910s 10 1.02x
🌐 MongoDB Next.js (Turbopack) 1.312s 2.009s 0.696s 10 1.20x
🐘 Postgres Next.js (Turbopack) ⚠️ missing - - - -

▲ Production (Vercel)

World Framework Workflow Time Wall Time Overhead Samples vs Fastest
▲ Vercel 🥇 Express 2.037s (-0.9%) 3.589s (-2.2%) 1.552s 10 1.00x
▲ Vercel Next.js (Turbopack) 2.058s (+3.4%) 3.600s (-4.7%) 1.542s 10 1.01x
▲ Vercel Nitro 2.097s (+4.2%) 3.841s (~) 1.744s 10 1.03x

🔍 Observability: Express | Next.js (Turbopack) | Nitro

workflow with 10 sequential steps

💻 Local Development

World Framework Workflow Time Wall Time Overhead Samples vs Fastest
🐘 Postgres 🥇 Nitro 10.618s (~) 11.042s (~) 0.424s 3 1.00x
🐘 Postgres Express 10.623s (-1.7%) 11.041s (~) 0.418s 3 1.00x
🌐 Redis Next.js (Turbopack) 10.653s 11.022s 0.368s 3 1.00x
💻 Local Next.js (Turbopack) 10.686s 11.024s 0.338s 3 1.01x
💻 Local Nitro 10.780s (~) 11.022s (~) 0.242s 3 1.02x
💻 Local Express 10.788s (~) 11.023s (~) 0.235s 3 1.02x
🌐 MongoDB Next.js (Turbopack) 12.268s 13.026s 0.758s 3 1.16x
🐘 Postgres Next.js (Turbopack) ⚠️ missing - - - -

▲ Production (Vercel)

World Framework Workflow Time Wall Time Overhead Samples vs Fastest
▲ Vercel 🥇 Nitro 16.427s (-1.3%) 18.373s (~) 1.946s 2 1.00x
▲ Vercel Express 16.474s (-6.0% 🟢) 18.419s (-5.1% 🟢) 1.945s 2 1.00x
▲ Vercel Next.js (Turbopack) 16.669s (+0.9%) 18.904s (+4.0%) 2.235s 2 1.01x

🔍 Observability: Nitro | Express | Next.js (Turbopack)

workflow with 25 sequential steps

💻 Local Development

World Framework Workflow Time Wall Time Overhead Samples vs Fastest
🐘 Postgres 🥇 Nitro 26.408s (~) 27.057s (~) 0.649s 3 1.00x
🐘 Postgres Express 26.484s (-1.8%) 27.060s (~) 0.576s 3 1.00x
🌐 Redis Next.js (Turbopack) 26.637s 27.051s 0.414s 3 1.01x
💻 Local Next.js (Turbopack) 26.891s 27.051s 0.160s 3 1.02x
💻 Local Nitro 27.190s (~) 28.052s (~) 0.862s 3 1.03x
💻 Local Express 27.223s (~) 28.052s (~) 0.829s 3 1.03x
🌐 MongoDB Next.js (Turbopack) 30.454s 31.037s 0.583s 2 1.15x
🐘 Postgres Next.js (Turbopack) ⚠️ missing - - - -

▲ Production (Vercel)

World Framework Workflow Time Wall Time Overhead Samples vs Fastest
▲ Vercel 🥇 Express 42.873s (-0.8%) 45.603s (+1.4%) 2.730s 2 1.00x
▲ Vercel Next.js (Turbopack) 45.940s (+5.3% 🔺) 48.472s (+7.3% 🔺) 2.532s 2 1.07x
▲ Vercel Nitro 48.968s (+14.1% 🔺) 50.960s (+12.3% 🔺) 1.991s 2 1.14x

🔍 Observability: Express | Next.js (Turbopack) | Nitro

workflow with 50 sequential steps

💻 Local Development

World Framework Workflow Time Wall Time Overhead Samples vs Fastest
🐘 Postgres 🥇 Nitro 52.836s (~) 53.092s (~) 0.256s 2 1.00x
🐘 Postgres Express 52.871s (-2.0%) 53.105s (-1.9%) 0.233s 2 1.00x
🌐 Redis Next.js (Turbopack) 52.971s 53.099s 0.128s 2 1.00x
💻 Local Next.js (Turbopack) 55.483s 56.100s 0.617s 2 1.05x
💻 Local Nitro 56.025s (~) 56.098s (~) 0.073s 2 1.06x
💻 Local Express 56.109s (~) 56.605s (+0.9%) 0.496s 2 1.06x
🌐 MongoDB Next.js (Turbopack) 60.938s 61.078s 0.141s 2 1.15x
🐘 Postgres Next.js (Turbopack) ⚠️ missing - - - -

▲ Production (Vercel)

World Framework Workflow Time Wall Time Overhead Samples vs Fastest
▲ Vercel 🥇 Nitro 92.240s (~) 94.312s (~) 2.072s 1 1.00x
▲ Vercel Express 93.955s (+2.4%) 95.063s (+1.0%) 1.108s 1 1.02x
▲ Vercel Next.js (Turbopack) 95.046s (+0.6%) 96.363s (~) 1.317s 1 1.03x

🔍 Observability: Nitro | Express | Next.js (Turbopack)

Promise.all with 10 concurrent steps

💻 Local Development

World Framework Workflow Time Wall Time Overhead Samples vs Fastest
🌐 Redis 🥇 Next.js (Turbopack) 1.239s 2.006s 0.768s 15 1.00x
🐘 Postgres Nitro 1.268s (~) 2.011s (~) 0.743s 15 1.02x
🐘 Postgres Express 1.286s (-5.7% 🟢) 2.011s (~) 0.725s 15 1.04x
💻 Local Express 1.407s (~) 2.005s (~) 0.598s 15 1.14x
💻 Local Next.js (Turbopack) 1.420s 2.005s 0.585s 15 1.15x
💻 Local Nitro 1.424s (+2.0%) 2.006s (~) 0.582s 15 1.15x
🌐 MongoDB Next.js (Turbopack) 2.136s 3.009s 0.873s 10 1.72x
🐘 Postgres Next.js (Turbopack) ⚠️ missing - - - -

▲ Production (Vercel)

World Framework Workflow Time Wall Time Overhead Samples vs Fastest
▲ Vercel 🥇 Express 2.279s (+1.0%) 3.960s (+2.8%) 1.680s 8 1.00x
▲ Vercel Nitro 2.434s (-4.2%) 4.463s (+1.4%) 2.030s 7 1.07x
▲ Vercel Next.js (Turbopack) 2.956s (+23.5% 🔺) 5.021s (+27.5% 🔺) 2.065s 6 1.30x

🔍 Observability: Express | Nitro | Next.js (Turbopack)

Promise.all with 25 concurrent steps

💻 Local Development

World Framework Workflow Time Wall Time Overhead Samples vs Fastest
🐘 Postgres 🥇 Nitro 2.446s (+1.5%) 3.012s (~) 0.567s 10 1.00x
🐘 Postgres Express 2.453s (-0.9%) 3.012s (~) 0.559s 10 1.00x
🌐 Redis Next.js (Turbopack) 2.563s 3.108s 0.545s 10 1.05x
💻 Local Next.js (Turbopack) 2.594s 3.007s 0.413s 10 1.06x
💻 Local Express 2.630s (+3.3%) 3.008s (~) 0.378s 10 1.08x
💻 Local Nitro 2.642s (+3.1%) 3.008s (~) 0.367s 10 1.08x
🌐 MongoDB Next.js (Turbopack) 4.792s 5.178s 0.386s 6 1.96x
🐘 Postgres Next.js (Turbopack) ⚠️ missing - - - -

▲ Production (Vercel)

World Framework Workflow Time Wall Time Overhead Samples vs Fastest
▲ Vercel 🥇 Express 2.572s (-3.9%) 3.879s (-13.0% 🟢) 1.307s 8 1.00x
▲ Vercel Next.js (Turbopack) 2.617s (-4.0%) 4.287s (-1.1%) 1.670s 8 1.02x
▲ Vercel Nitro 2.787s (+18.5% 🔺) 4.289s (+9.9% 🔺) 1.502s 8 1.08x

🔍 Observability: Express | Next.js (Turbopack) | Nitro

Promise.all with 50 concurrent steps

💻 Local Development

World Framework Workflow Time Wall Time Overhead Samples vs Fastest
🐘 Postgres 🥇 Nitro 3.860s (+6.0% 🔺) 4.300s (+7.1% 🔺) 0.440s 7 1.00x
🐘 Postgres Express 3.924s (-3.0%) 4.301s (-6.2% 🟢) 0.377s 7 1.02x
🌐 Redis Next.js (Turbopack) 4.067s 5.010s 0.943s 6 1.05x
💻 Local Next.js (Turbopack) 7.047s 7.516s 0.469s 4 1.83x
💻 Local Nitro 7.566s (+3.7%) 8.024s (~) 0.458s 4 1.96x
💻 Local Express 7.574s (+0.9%) 8.020s (~) 0.446s 4 1.96x
🌐 MongoDB Next.js (Turbopack) 9.891s 10.350s 0.459s 3 2.56x
🐘 Postgres Next.js (Turbopack) ⚠️ missing - - - -

▲ Production (Vercel)

World Framework Workflow Time Wall Time Overhead Samples vs Fastest
▲ Vercel 🥇 Nitro 2.770s (-13.8% 🟢) 4.035s (-18.0% 🟢) 1.265s 8 1.00x
▲ Vercel Express 2.961s (+2.3%) 4.429s (-1.2%) 1.468s 7 1.07x
▲ Vercel Next.js (Turbopack) 3.301s (+4.0%) 4.797s (+2.5%) 1.496s 7 1.19x

🔍 Observability: Nitro | Express | Next.js (Turbopack)

Promise.race with 10 concurrent steps

💻 Local Development

World Framework Workflow Time Wall Time Overhead Samples vs Fastest
🌐 Redis 🥇 Next.js (Turbopack) 1.253s 2.006s 0.754s 15 1.00x
🐘 Postgres Nitro 1.295s (~) 2.011s (~) 0.716s 15 1.03x
🐘 Postgres Express 1.297s (-6.2% 🟢) 2.011s (~) 0.714s 15 1.04x
💻 Local Next.js (Turbopack) 1.416s 2.005s 0.589s 15 1.13x
💻 Local Express 1.442s (-1.6%) 2.005s (~) 0.563s 15 1.15x
💻 Local Nitro 1.458s (+1.9%) 2.006s (~) 0.548s 15 1.16x
🌐 MongoDB Next.js (Turbopack) 2.151s 3.009s 0.858s 10 1.72x
🐘 Postgres Next.js (Turbopack) ⚠️ missing - - - -

▲ Production (Vercel)

World Framework Workflow Time Wall Time Overhead Samples vs Fastest
▲ Vercel 🥇 Express 2.240s (-3.0%) 4.093s (+2.6%) 1.853s 8 1.00x
▲ Vercel Nitro 2.348s (+6.0% 🔺) 4.150s (+1.6%) 1.803s 8 1.05x
▲ Vercel Next.js (Turbopack) 2.960s (+40.5% 🔺) 4.624s (+26.1% 🔺) 1.664s 7 1.32x

🔍 Observability: Express | Nitro | Next.js (Turbopack)

Promise.race with 25 concurrent steps

💻 Local Development

World Framework Workflow Time Wall Time Overhead Samples vs Fastest
🐘 Postgres 🥇 Express 2.425s (-2.2%) 3.012s (~) 0.587s 10 1.00x
🐘 Postgres Nitro 2.443s (-2.1%) 3.012s (~) 0.569s 10 1.01x
🌐 Redis Next.js (Turbopack) 2.513s 3.008s 0.495s 10 1.04x
💻 Local Next.js (Turbopack) 2.635s 3.009s 0.374s 10 1.09x
💻 Local Express 2.743s (+1.1%) 3.008s (~) 0.264s 10 1.13x
💻 Local Nitro 2.784s (+2.2%) 3.009s (~) 0.225s 10 1.15x
🌐 MongoDB Next.js (Turbopack) 4.782s 5.179s 0.396s 6 1.97x
🐘 Postgres Next.js (Turbopack) ⚠️ missing - - - -

▲ Production (Vercel)

World Framework Workflow Time Wall Time Overhead Samples vs Fastest
▲ Vercel 🥇 Express 2.557s (+1.5%) 4.082s (-0.7%) 1.525s 8 1.00x
▲ Vercel Next.js (Turbopack) 2.671s (+9.7% 🔺) 4.062s (+5.9% 🔺) 1.391s 8 1.04x
▲ Vercel Nitro 2.674s (+4.0%) 4.652s (+18.4% 🔺) 1.977s 7 1.05x

🔍 Observability: Express | Next.js (Turbopack) | Nitro

Promise.race with 50 concurrent steps

💻 Local Development

World Framework Workflow Time Wall Time Overhead Samples vs Fastest
🐘 Postgres 🥇 Express 3.828s (-5.1% 🟢) 4.140s (-9.8% 🟢) 0.312s 8 1.00x
🐘 Postgres Nitro 3.884s (+1.8%) 4.140s (~) 0.256s 8 1.01x
🌐 Redis Next.js (Turbopack) 4.038s 4.582s 0.543s 7 1.05x
💻 Local Next.js (Turbopack) 7.431s 8.015s 0.584s 4 1.94x
💻 Local Nitro 7.936s (+1.2%) 8.273s (+3.2%) 0.337s 4 2.07x
💻 Local Express 8.316s (+5.5% 🔺) 9.021s (+12.5% 🔺) 0.705s 4 2.17x
🌐 MongoDB Next.js (Turbopack) 10.040s 10.687s 0.647s 3 2.62x
🐘 Postgres Next.js (Turbopack) ⚠️ missing - - - -

▲ Production (Vercel)

World Framework Workflow Time Wall Time Overhead Samples vs Fastest
▲ Vercel 🥇 Nitro 2.743s (-12.5% 🟢) 4.221s (-10.0% 🟢) 1.478s 8 1.00x
▲ Vercel Next.js (Turbopack) 2.801s (-5.8% 🟢) 4.383s (-2.7%) 1.582s 8 1.02x
▲ Vercel Express 3.030s (~) 4.343s (+0.7%) 1.313s 8 1.10x

🔍 Observability: Nitro | Next.js (Turbopack) | Express

Stream Benchmarks (includes TTFB metrics)
workflow with stream

💻 Local Development

World Framework Workflow Time TTFB Slurp Wall Time Overhead Samples vs Fastest
🐘 Postgres 🥇 Express 0.141s (-32.0% 🟢) 0.999s (+0.7%) 0.001s (~) 1.012s (~) 0.871s 10 1.00x
💻 Local Next.js (Turbopack) 0.142s 1.002s 0.011s 1.017s 0.875s 10 1.00x
🐘 Postgres Nitro 0.143s (+5.9% 🔺) 0.999s (~) 0.002s (-6.3% 🟢) 1.012s (~) 0.869s 10 1.01x
🌐 Redis Next.js (Turbopack) 0.150s 1.000s 0.002s 1.007s 0.857s 10 1.06x
💻 Local Express 0.173s (+2.7%) 1.002s (~) 0.011s (-5.0% 🟢) 1.017s (~) 0.843s 10 1.23x
💻 Local Nitro 0.173s (+3.6%) 1.003s (~) 0.011s (-1.8%) 1.017s (~) 0.844s 10 1.23x
🌐 MongoDB Next.js (Turbopack) 0.502s 0.943s 0.002s 1.009s 0.507s 10 3.55x
🐘 Postgres Next.js (Turbopack) ⚠️ missing - - - - -

▲ Production (Vercel)

World Framework Workflow Time TTFB Slurp Wall Time Overhead Samples vs Fastest
▲ Vercel 🥇 Nitro 1.565s (-16.9% 🟢) 2.342s (-8.6% 🟢) 0.007s (+56.3% 🔺) 2.916s (-6.9% 🟢) 1.351s 10 1.00x
▲ Vercel Next.js (Turbopack) 1.607s (+3.4%) 2.332s (+1.2%) 0.005s (+6.5% 🔺) 2.955s (-4.1%) 1.348s 10 1.03x
▲ Vercel Express 1.622s (+7.0% 🔺) 2.593s (+13.7% 🔺) 0.007s (+11.7% 🔺) 3.152s (+11.4% 🔺) 1.529s 10 1.04x

🔍 Observability: Nitro | Next.js (Turbopack) | Express

Summary

Fastest Framework by World

Winner determined by most benchmark wins

World 🥇 Fastest Framework Wins
💻 Local Next.js (Turbopack) 9/12
🐘 Postgres Nitro 8/12
▲ Vercel Nitro 6/12
Fastest World by Framework

Winner determined by most benchmark wins

Framework 🥇 Fastest World Wins
Express 🐘 Postgres 8/12
Next.js (Turbopack) 🌐 Redis 8/12
Nitro 🐘 Postgres 9/12
Column Definitions
  • Workflow Time: Runtime reported by workflow (completedAt - createdAt) - primary metric
  • TTFB: Time to First Byte - time from workflow start until first stream byte received (stream benchmarks only)
  • Slurp: Time from first byte to complete stream consumption (stream benchmarks only)
  • Wall Time: Total testbench time (trigger workflow + poll for result)
  • Overhead: Testbench overhead (Wall Time - Workflow Time)
  • Samples: Number of benchmark iterations run
  • vs Fastest: How much slower compared to the fastest configuration for this benchmark

Worlds:

  • 💻 Local: In-memory filesystem world (local development)
  • 🐘 Postgres: PostgreSQL database world (local development)
  • ▲ Vercel: Vercel production/preview deployment
  • 🌐 Turso: Community world (local development)
  • 🌐 MongoDB: Community world (local development)
  • 🌐 Redis: Community world (local development)
  • 🌐 Jazz: Community world (local development)

📋 View full workflow run

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes SWC plugin closure-variable analysis so step extraction correctly captures identifiers used in new expressions, and avoids over-capturing module-scope declarations (which can lead to attempting to serialize functions and can block DCE of step-only helpers).

Changes:

  • Add Expr::New handling in ClosureVariableCollector to analyze constructor callee + args.
  • Exclude module-level declarations (in addition to imports) from closure-variable detection by seeding collector locals with declared_identifiers.
  • Add a new fixture covering both scenarios and update the plugin spec + changelog entry.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
packages/swc-plugin-workflow/transform/src/lib.rs Implements Expr::New traversal and passes module-level declared identifiers into closure var collection to prevent over-capture.
packages/swc-plugin-workflow/spec.md Documents the updated closure-variable detection behavior.
packages/swc-plugin-workflow/transform/tests/fixture/closure-new-expr-and-module-declarations/input.js New fixture input reproducing both reported bugs.
packages/swc-plugin-workflow/transform/tests/fixture/closure-new-expr-and-module-declarations/output-workflow.js Expected workflow-mode output validating closure capture and DCE-friendly behavior.
packages/swc-plugin-workflow/transform/tests/fixture/closure-new-expr-and-module-declarations/output-step.js Expected step-mode output validating rehydration + direct access to module-level helpers.
packages/swc-plugin-workflow/transform/tests/fixture/closure-new-expr-and-module-declarations/output-client.js Expected client-mode output validating closure capture in non-step bundle.
.changeset/fix-swc-closure-variable-bugs.md Changeset for patch release of @workflow/swc-plugin.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

…ure variable collector

Expand closure variable detection to cover more AST patterns:

Expressions: Seq (comma), Yield, OptChain, Prop::Shorthand,
computed property keys, Prop::Assign defaults, Class (skip bodies)

Statements: Throw, Try/Catch/Finally, Switch, ForIn, ForOf,
DoWhile, Labeled

Also fix existing Prop::Shorthand bug where object shorthand
properties like { url } were not being collected as closure vars.

Extend test fixture with cases for all newly handled patterns.
Fix spec.md wording per review feedback.
…od bodies

The closure variable collector was skipping nested function expressions,
arrow functions, and method bodies entirely. This meant closure
variables used deep inside inner functions (e.g., a variable used inside
a ReadableStream's start() method) were not captured.

Now the collector walks into nested function/arrow/method/getter/setter
bodies while adding their parameters to the local var set, so only
truly free variables from the outer step scope are captured.

Also add ReadableStream, WritableStream, TransformStream, and other
common Web API globals to the known globals list.
…es in closure detection

After comparing with Next.js's SWC plugin closure detection approach,
identified and fixed remaining gaps:

- TypeScript expression wrappers (as, satisfies, !, type assertions,
  const assertions, instantiation expressions) now traverse to the
  inner expression instead of being silently skipped
- Class expressions and declarations now walk their body members
  (methods, properties, constructors, static blocks) to detect
  closure variables used inside them
- Document all remaining safe-to-skip Expr variants (This, Lit,
  SuperProp, MetaProp, PrivateName, Invalid, JSX)
…ion wrappers

Add a proper input.ts fixture that tests closure variable detection
through real TypeScript syntax: `as`, `satisfies`, `!` (non-null),
angle-bracket type assertions, `as const`, and generic function calls.

Update test harness to support input.ts files by adding swc_ecma_parser
dev-dependency and auto-detecting TypeScript syntax from file extension.

Remove the incorrectly placed TypeScript-related test cases from the JS
fixture (they were using plain JS syntax, not actual TS wrappers).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 compiler bugs related to closure variables Closure variables break steps from being reused as normal functions outside workflow context

2 participants