Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/nested-step-function-expression.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@workflow/swc-plugin': patch
---

A `"use step"` function expression bound to a variable inside a workflow or another function (`const send = async function () { "use step"; ... }`) is now a nested step like the arrow form: hoisted, namespaced by the enclosing function and given the variables it captures. It was treated as a module-level step, so its step was never registered and the run failed with "Step ... is not registered".
2 changes: 2 additions & 0 deletions packages/swc-plugin-workflow/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ example.workflowId = "workflow//./input//example";
registerStepFunction("step//./input//example/innerStep", example$innerStep);
```

The same applies to every form of nested step: a function declaration (as above), an arrow function or function expression bound to a `const`/`let`/`var` (`const act = async () => { "use step"; ... }`, `const act = async function summarize() { "use step"; ... }`), and a function or arrow expression that is not bound to a variable (for example one that is returned). A step bound to a variable takes the variable's name (`example/act`), not the function expression's own name. An unbound function expression takes its own name, and an unbound anonymous one is named `_anonymousStepN`.

### Steps in Nested Object Properties

Step functions can be defined inside deeply nested object properties, including function call arguments. The plugin recursively processes nested objects to find step functions, generating compound paths for the step IDs.
Expand Down
78 changes: 78 additions & 0 deletions packages/swc-plugin-workflow/transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7865,6 +7865,84 @@ impl VisitMut for StepTransform {
// It's valid - proceed with transformation
self.step_function_names.insert(name.clone());

// Inside a function (for example a workflow body), a
// `const name = async function () { "use step" }` is a
// nested step, exactly like the arrow form below: it is
// hoisted, namespaced by the enclosing function and
// handed the closure variables it captures.
if !self.in_module_level {
match self.mode {
TransformMode::Step => {
let mut cloned_function = fn_expr.function.clone();
self.remove_use_step_directive(
&mut cloned_function.body,
);
let closure_vars =
ClosureVariableCollector::collect_from_function(
&cloned_function,
&self.module_imports,
&self.declared_identifiers,
);
let hoisted_fn_expr = FnExpr {
ident: Some(Ident::new(
name.clone().into(),
DUMMY_SP,
SyntaxContext::empty(),
)),
function: cloned_function,
};
self.nested_step_functions.push((
name.clone(),
hoisted_fn_expr,
fn_expr.function.span,
closure_vars,
false, // Not an arrow function
self.current_parent_function_name
.clone()
.unwrap_or_default(),
false, // Regular functions have their own `this`
));
// Keep the original with the directive stripped, so
// direct (non-workflow) calls keep normal closure semantics.
self.remove_use_step_directive(
&mut fn_expr.function.body,
);
}
TransformMode::Workflow => {
let parent_workflow = self
.current_parent_function_name
.clone()
.unwrap_or_default();
let step_fn_name = self.record_nested_step_name(
&name,
&parent_workflow,
);
let step_id = self.create_id(
Some(&step_fn_name),
fn_expr.function.span,
false,
);
let closure_vars =
ClosureVariableCollector::collect_from_function(
&fn_expr.function,
&self.module_imports,
&self.declared_identifiers,
);
*init = Box::new(self.create_step_proxy_reference(
&step_id,
&closure_vars,
));
}
TransformMode::Client => {
// Nested step: just remove the directive without registering
self.remove_use_step_directive(
&mut fn_expr.function.body,
);
}
}
continue;
}

match self.mode {
TransformMode::Step => {
self.remove_use_step_directive(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// A `"use step"` function expression bound to a const inside a function is a
// nested step, like the arrow form: hoisted, namespaced by the enclosing
// function, and given the closure variables it captures.
export async function closureSteps(n, label) {
'use workflow';

const named = async function summarize() {
'use step';
return `${label}:${n + 1}`;
};

const anonymous = async function () {
'use step';
return n * 2;
};

return [await named(), await anonymous()];
}

function stepWrapperReturnNamedFunctionExprVar(a, b) {
const fn = async function inner() {
'use step';
return a + b;
};
return fn;
}

export async function usesWrapper() {
'use workflow';
return stepWrapperReturnNamedFunctionExprVar(1, 2)();
}

// At module level it stays a module-level step.
const moduleLevel = async function () {
'use step';
return 1;
};

export async function usesModuleLevel() {
'use workflow';
return moduleLevel();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**__internal_workflows{"workflows":{"input.js":{"closureSteps":{"workflowId":"workflow//./input//closureSteps"},"usesModuleLevel":{"workflowId":"workflow//./input//usesModuleLevel"},"usesWrapper":{"workflowId":"workflow//./input//usesWrapper"}}},"steps":{"input.js":{"fn":{"stepId":"step//./input//fn"},"moduleLevel":{"stepId":"step//./input//moduleLevel"}}}}*/;
// A `"use step"` function expression bound to a const inside a function is a
// nested step, like the arrow form: hoisted, namespaced by the enclosing
// function, and given the closure variables it captures.
export async function closureSteps(n, label) {
throw new Error("You attempted to execute workflow closureSteps function directly. To start a workflow, use start(closureSteps) from workflow/api");
}
closureSteps.workflowId = "workflow//./input//closureSteps";
export async function usesWrapper() {
throw new Error("You attempted to execute workflow usesWrapper function directly. To start a workflow, use start(usesWrapper) from workflow/api");
}
usesWrapper.workflowId = "workflow//./input//usesWrapper";
// At module level it stays a module-level step.
const moduleLevel = async function() {
return 1;
};
moduleLevel.stepId = "step//./input//moduleLevel";
export async function usesModuleLevel() {
throw new Error("You attempted to execute workflow usesModuleLevel function directly. To start a workflow, use start(usesModuleLevel) from workflow/api");
}
usesModuleLevel.workflowId = "workflow//./input//usesModuleLevel";
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { __private_getClosureVars, registerStepFunction } from "workflow/internal/private";
/**__internal_workflows{"workflows":{"input.js":{"closureSteps":{"workflowId":"workflow//./input//closureSteps"},"usesModuleLevel":{"workflowId":"workflow//./input//usesModuleLevel"},"usesWrapper":{"workflowId":"workflow//./input//usesWrapper"}}},"steps":{"input.js":{"anonymous":{"stepId":"step//./input//closureSteps/anonymous"},"fn":{"stepId":"step//./input//stepWrapperReturnNamedFunctionExprVar/fn"},"moduleLevel":{"stepId":"step//./input//moduleLevel"},"named":{"stepId":"step//./input//closureSteps/named"}}}}*/;
async function closureSteps$named() {
const { label, n } = __private_getClosureVars();
return `${label}:${n + 1}`;
}
async function closureSteps$anonymous() {
const { n } = __private_getClosureVars();
return n * 2;
}
async function stepWrapperReturnNamedFunctionExprVar$fn() {
const { a, b } = __private_getClosureVars();
return a + b;
}
// A `"use step"` function expression bound to a const inside a function is a
// nested step, like the arrow form: hoisted, namespaced by the enclosing
// function, and given the closure variables it captures.
export async function closureSteps(n, label) {
throw new Error("You attempted to execute workflow closureSteps function directly. To start a workflow, use start(closureSteps) from workflow/api");
}
closureSteps.workflowId = "workflow//./input//closureSteps";
function stepWrapperReturnNamedFunctionExprVar(a, b) {
const fn = async function inner() {
return a + b;
};
return fn;
}
export async function usesWrapper() {
throw new Error("You attempted to execute workflow usesWrapper function directly. To start a workflow, use start(usesWrapper) from workflow/api");
}
usesWrapper.workflowId = "workflow//./input//usesWrapper";
// At module level it stays a module-level step.
const moduleLevel = async function() {
return 1;
};
export async function usesModuleLevel() {
throw new Error("You attempted to execute workflow usesModuleLevel function directly. To start a workflow, use start(usesModuleLevel) from workflow/api");
}
usesModuleLevel.workflowId = "workflow//./input//usesModuleLevel";
registerStepFunction("step//./input//moduleLevel", moduleLevel);
registerStepFunction("step//./input//closureSteps/named", closureSteps$named);
registerStepFunction("step//./input//closureSteps/anonymous", closureSteps$anonymous);
registerStepFunction("step//./input//stepWrapperReturnNamedFunctionExprVar/fn", stepWrapperReturnNamedFunctionExprVar$fn);
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**__internal_workflows{"workflows":{"input.js":{"closureSteps":{"workflowId":"workflow//./input//closureSteps"},"usesModuleLevel":{"workflowId":"workflow//./input//usesModuleLevel"},"usesWrapper":{"workflowId":"workflow//./input//usesWrapper"}}},"steps":{"input.js":{"anonymous":{"stepId":"step//./input//closureSteps/anonymous"},"fn":{"stepId":"step//./input//stepWrapperReturnNamedFunctionExprVar/fn"},"moduleLevel":{"stepId":"step//./input//moduleLevel"},"named":{"stepId":"step//./input//closureSteps/named"}}}}*/;
// A `"use step"` function expression bound to a const inside a function is a
// nested step, like the arrow form: hoisted, namespaced by the enclosing
// function, and given the closure variables it captures.
export async function closureSteps(n, label) {
const named = globalThis[Symbol.for("WORKFLOW_USE_STEP")]("step//./input//closureSteps/named", ()=>({
label,
n
}));
const anonymous = globalThis[Symbol.for("WORKFLOW_USE_STEP")]("step//./input//closureSteps/anonymous", ()=>({
n
}));
return [
await named(),
await anonymous()
];
}
closureSteps.workflowId = "workflow//./input//closureSteps";
globalThis.__private_workflows.set("workflow//./input//closureSteps", closureSteps);
function stepWrapperReturnNamedFunctionExprVar(a, b) {
const fn = globalThis[Symbol.for("WORKFLOW_USE_STEP")]("step//./input//stepWrapperReturnNamedFunctionExprVar/fn", ()=>({
a,
b
}));
return fn;
}
export async function usesWrapper() {
return stepWrapperReturnNamedFunctionExprVar(1, 2)();
}
usesWrapper.workflowId = "workflow//./input//usesWrapper";
globalThis.__private_workflows.set("workflow//./input//usesWrapper", usesWrapper);
// At module level it stays a module-level step.
const moduleLevel = globalThis[Symbol.for("WORKFLOW_USE_STEP")]("step//./input//moduleLevel");
export async function usesModuleLevel() {
return moduleLevel();
}
usesModuleLevel.workflowId = "workflow//./input//usesModuleLevel";
globalThis.__private_workflows.set("workflow//./input//usesModuleLevel", usesModuleLevel);
Loading