Skip to content

Commit e0c6618

Browse files
authored
fix(swc-plugin): specify import path of start function on error (#244)
1 parent 7013f29 commit e0c6618

File tree

8 files changed

+27
-20
lines changed

8 files changed

+27
-20
lines changed

.changeset/poor-eyes-build.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@workflow/swc-plugin": patch
3+
---
4+
5+
Specify import path of `start` function on error in SWC plugin

packages/swc-plugin-workflow/spec.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ registerStepFunction("step//input.js//add", add)
2929
```
3030

3131
**ID Generation:** Step IDs are generated using the format `step//{filepath}//{functionName}`, where:
32+
3233
- `filepath` is the relative path to the file from the project root
3334
- `functionName` is the name of the step function
3435

@@ -113,20 +114,21 @@ export async function add(a, b) {
113114
}
114115
115116
export async function workflow(a, b) {
116-
throw new Error("You attempted to execute workflow workflow function directly. To start a workflow, use start(workflow) from workflow");
117+
throw new Error("You attempted to execute workflow workflow function directly. To start a workflow, use start(workflow) from workflow/api");
117118
}
118119
workflow.workflowId = "workflow//workflow/main.js//workflow";
119120
```
120121

121122
**ID Generation:**
123+
122124
- Step functions use `runStep` with the function name (not the full ID)
123125
- Workflow functions throw an error if called directly and have the `workflowId` property attached using the format `workflow//{filepath}//{functionName}`
124126

125127
Upstream, this mode is typically used by a framework loader (like a Next.js/webpack loader) to JIT transform workflow executions into proxied calls.
126128

127129
## Notes
128130

129-
* Instead of individually marking functions with 'use step' or 'use_workflow', you can also add the directive to the top of a file to mark all exports within that file as step functions or workflows
130-
* the directives must be at the very beginning of their function or module; above any other code including imports (comments above directives are OK). They must be written with single or double quotes, not backticks.
131-
* The arguments and return value of 'use step' and 'use workflow' must be serializable.
132-
* Because the underlying network calls are always asynchronous, 'use step' and 'use workflow' can only be used on async functions.
131+
- Instead of individually marking functions with 'use step' or 'use_workflow', you can also add the directive to the top of a file to mark all exports within that file as step functions or workflows
132+
- the directives must be at the very beginning of their function or module; above any other code including imports (comments above directives are OK). They must be written with single or double quotes, not backticks.
133+
- The arguments and return value of 'use step' and 'use workflow' must be serializable.
134+
- Because the underlying network calls are always asynchronous, 'use step' and 'use workflow' can only be used on async functions.

packages/swc-plugin-workflow/transform/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,7 +2308,7 @@ impl VisitMut for StepTransform {
23082308
self.remove_use_workflow_directive(&mut fn_decl.function.body);
23092309
if let Some(body) = &mut fn_decl.function.body {
23102310
let error_msg = format!(
2311-
"You attempted to execute workflow {} function directly. To start a workflow, use start({}) from workflow",
2311+
"You attempted to execute workflow {} function directly. To start a workflow, use start({}) from workflow/api",
23122312
fn_name, fn_name
23132313
);
23142314
let error_expr = Expr::New(NewExpr {
@@ -2855,7 +2855,7 @@ impl VisitMut for StepTransform {
28552855
);
28562856
if let Some(body) = &mut fn_expr.function.body {
28572857
let error_msg = format!(
2858-
"You attempted to execute workflow {} function directly. To start a workflow, use start({}) from workflow",
2858+
"You attempted to execute workflow {} function directly. To start a workflow, use start({}) from workflow/api",
28592859
name, name
28602860
);
28612861
let error_expr = Expr::New(NewExpr {
@@ -2981,7 +2981,7 @@ impl VisitMut for StepTransform {
29812981
&mut arrow_expr.body,
29822982
);
29832983
let error_msg = format!(
2984-
"You attempted to execute workflow {} function directly. To start a workflow, use start({}) from workflow",
2984+
"You attempted to execute workflow {} function directly. To start a workflow, use start({}) from workflow/api",
29852985
name, name
29862986
);
29872987
let error_expr = Expr::New(NewExpr {
@@ -3169,7 +3169,7 @@ impl VisitMut for StepTransform {
31693169
.unwrap_or_else(|| "defaultWorkflow".to_string());
31703170
if let Some(body) = &mut fn_expr.function.body {
31713171
let error_msg = format!(
3172-
"You attempted to execute workflow {} function directly. To start a workflow, use start({}) from workflow",
3172+
"You attempted to execute workflow {} function directly. To start a workflow, use start({}) from workflow/api",
31733173
actual_name, actual_name
31743174
);
31753175
let error_expr = Expr::New(NewExpr {
@@ -3302,7 +3302,7 @@ impl VisitMut for StepTransform {
33023302
);
33033303
if let Some(body) = &mut fn_decl.function.body {
33043304
let error_msg = format!(
3305-
"You attempted to execute workflow {} function directly. To start a workflow, use start({}) from workflow",
3305+
"You attempted to execute workflow {} function directly. To start a workflow, use start({}) from workflow/api",
33063306
fn_name, fn_name
33073307
);
33083308
let error_expr = Expr::New(NewExpr {
@@ -3438,7 +3438,7 @@ impl VisitMut for StepTransform {
34383438
&mut arrow_expr.body,
34393439
);
34403440
let error_msg = format!(
3441-
"You attempted to execute workflow {} function directly. To start a workflow, use start({}) from workflow",
3441+
"You attempted to execute workflow {} function directly. To start a workflow, use start({}) from workflow/api",
34423442
name, name
34433443
);
34443444
let error_expr = Expr::New(NewExpr {
@@ -3504,7 +3504,7 @@ impl VisitMut for StepTransform {
35043504
&mut fn_expr.function.body
35053505
{
35063506
let error_msg = format!(
3507-
"You attempted to execute workflow {} function directly. To start a workflow, use start({}) from workflow",
3507+
"You attempted to execute workflow {} function directly. To start a workflow, use start({}) from workflow/api",
35083508
name, name
35093509
);
35103510
let error_expr = Expr::New(NewExpr {

packages/swc-plugin-workflow/transform/tests/errors/non-async-functions/output-client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ export async function validStep() {
2424
});
2525
}
2626
export const validWorkflow = async ()=>{
27-
throw new Error("You attempted to execute workflow validWorkflow function directly. To start a workflow, use start(validWorkflow) from workflow");
27+
throw new Error("You attempted to execute workflow validWorkflow function directly. To start a workflow, use start(validWorkflow) from workflow/api");
2828
};
2929
validWorkflow.workflowId = "workflow//input.js//validWorkflow";

packages/swc-plugin-workflow/transform/tests/fixture/mixed-functions/output-client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export async function stepFunction(a, b) {
99
});
1010
}
1111
export async function workflowFunction(a, b) {
12-
throw new Error("You attempted to execute workflow workflowFunction function directly. To start a workflow, use start(workflowFunction) from workflow");
12+
throw new Error("You attempted to execute workflow workflowFunction function directly. To start a workflow, use start(workflowFunction) from workflow/api");
1313
}
1414
workflowFunction.workflowId = "workflow//input.js//workflowFunction";
1515
export async function normalFunction(a, b) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**__internal_workflows{"workflows":{"input.js":{"workflow":{"workflowId":"workflow//input.js//workflow"}}}}*/;
22
export async function workflow(a, b) {
3-
throw new Error("You attempted to execute workflow workflow function directly. To start a workflow, use start(workflow) from workflow");
3+
throw new Error("You attempted to execute workflow workflow function directly. To start a workflow, use start(workflow) from workflow/api");
44
}
55
workflow.workflowId = "workflow//input.js//workflow";
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**__internal_workflows{"workflows":{"input.js":{"processData":{"workflowId":"workflow//input.js//processData"}}}}*/;
22
export const processData = async (data)=>{
3-
throw new Error("You attempted to execute workflow processData function directly. To start a workflow, use start(processData) from workflow");
3+
throw new Error("You attempted to execute workflow processData function directly. To start a workflow, use start(processData) from workflow/api");
44
};
55
processData.workflowId = "workflow//input.js//processData";

packages/swc-plugin-workflow/transform/tests/fixture/workflow-client-property/output-client.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
// Test workflow functions in client mode
22
/**__internal_workflows{"workflows":{"input.js":{"arrowWorkflow":{"workflowId":"workflow//input.js//arrowWorkflow"},"defaultWorkflow":{"workflowId":"workflow//input.js//defaultWorkflow"},"internalWorkflow":{"workflowId":"workflow//input.js//internalWorkflow"},"myWorkflow":{"workflowId":"workflow//input.js//myWorkflow"}}}}*/;
33
export async function myWorkflow() {
4-
throw new Error("You attempted to execute workflow myWorkflow function directly. To start a workflow, use start(myWorkflow) from workflow");
4+
throw new Error("You attempted to execute workflow myWorkflow function directly. To start a workflow, use start(myWorkflow) from workflow/api");
55
}
66
myWorkflow.workflowId = "workflow//input.js//myWorkflow";
77
export const arrowWorkflow = async ()=>{
8-
throw new Error("You attempted to execute workflow arrowWorkflow function directly. To start a workflow, use start(arrowWorkflow) from workflow");
8+
throw new Error("You attempted to execute workflow arrowWorkflow function directly. To start a workflow, use start(arrowWorkflow) from workflow/api");
99
};
1010
arrowWorkflow.workflowId = "workflow//input.js//arrowWorkflow";
1111
export default async function defaultWorkflow() {
12-
throw new Error("You attempted to execute workflow defaultWorkflow function directly. To start a workflow, use start(defaultWorkflow) from workflow");
12+
throw new Error("You attempted to execute workflow defaultWorkflow function directly. To start a workflow, use start(defaultWorkflow) from workflow/api");
1313
}
1414
defaultWorkflow.workflowId = "workflow//input.js//defaultWorkflow";
1515
// Non-export workflow function
1616
async function internalWorkflow() {
17-
throw new Error("You attempted to execute workflow internalWorkflow function directly. To start a workflow, use start(internalWorkflow) from workflow");
17+
throw new Error("You attempted to execute workflow internalWorkflow function directly. To start a workflow, use start(internalWorkflow) from workflow/api");
1818
}
1919
internalWorkflow.workflowId = "workflow//input.js//internalWorkflow";
2020
// Use the internal workflow to avoid lint warning

0 commit comments

Comments
 (0)