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
82 changes: 77 additions & 5 deletions packages/aws-durable-execution-sdk-js-eslint-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ Add the plugin to your ESLint configuration:
{
"plugins": ["@aws/durable-execution-sdk-js-eslint-plugin"],
"rules": {
"@aws/durable-execution-sdk-js-eslint-plugin/no-nested-durable-operations": "error"
"@aws/durable-execution-sdk-js-eslint-plugin/no-nested-durable-operations": "error",
"@aws/durable-execution-sdk-js-eslint-plugin/no-closure-in-durable-operations": "error"
}
}
```
Expand Down Expand Up @@ -65,22 +66,93 @@ const result = await context.runInChildContext("block1", async (childCtx) => {
});
```

### `no-closure-in-durable-operations`

Prevents modifying closure variables inside durable operations. Reading closure variables is allowed.

#### ❌ Incorrect

```javascript
const handler = withDurableExecution(
async (event: any, context: DurableContext) => {
let a = 2;
const result = await context.runInChildContext(
async (childContext: DurableContext) => {
const stepResult = await childContext.step(async () => {
// Error: Modifying 'a' from outer scope causes replay inconsistency
a = a + 1;
return "child step completed";
});
return stepResult;
},
);
return result;
},
);
```

#### ✅ Correct

```javascript
const handler = withDurableExecution(
async (event: any, context: DurableContext) => {
let a = 2;
const result = await context.runInChildContext(
async (childContext: DurableContext) => {
const stepResult = await childContext.step(async () => {
// Reading 'a' is OK
const value = a + 1;
return value;
});
return stepResult;
},
);
return result;
},
);

// Or use local variables
const handler = withDurableExecution(
async (event: any, context: DurableContext) => {
const result = await context.runInChildContext(
async (childContext: DurableContext) => {
const stepResult = await childContext.step(async () => {
let a = 2;
a = a + 1;
return "child step completed";
});
return stepResult;
},
);
return result;
},
);
```

## Supported Durable Operations

The plugin detects these durable operations:

- `step`
- `wait`
- `waitForCallback`
- `runInChildContext`
- `waitForCondition`
- `waitForCallback`
- `wait`
- `parallel`
- `map`
- `runInChildContext`

## Why This Rule?
## Why These Rules?

### No Nested Durable Operations

Nesting durable operations with the same context object can cause runtime errors and unexpected behavior in AWS Lambda Durable Functions. This rule helps catch these issues at development time.

### No Closure in Durable Operations

During replay, durable functions skip already-executed steps. If a closure variable is modified inside a step, the modification won't occur during replay, causing different outcomes between initial execution and replay. This leads to non-deterministic behavior and potential bugs.

Reading closure variables is safe because it doesn't change state. Only mutations (assignments, increments, decrements) are problematic.

## License

Apache-2.0
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ module.exports = {
preset: "ts-jest",
testEnvironment: "node",
roots: ["<rootDir>/src"],
testMatch: ["**/__tests__/**/*.test.ts"],
collectCoverageFrom: [
"src/**/*.ts",
"!src/**/*.test.ts",
"!src/**/__tests__/**",
],
testMatch: ["**/*.test.ts"],
collectCoverageFrom: ["src/**/*.ts", "!src/**/*.test.ts"],
};
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { noNestedDurableOperations } from "./rules/no-nested-durable-operations";
import { noNonDeterministicOutsideStep } from "./rules/no-non-deterministic-outside-step";
import { noNestedDurableOperations } from "./rules/no-nested-durable-operations/no-nested-durable-operations";
import { noNonDeterministicOutsideStep } from "./rules/no-non-deterministic-outside-step/no-non-deterministic-outside-step";
import { noClosureInDurableOperations } from "./rules/no-closure-in-durable-operations/no-closure-in-durable-operations";

export = {
rules: {
"no-nested-durable-operations": noNestedDurableOperations,
"no-non-deterministic-outside-step": noNonDeterministicOutsideStep,
"no-closure-in-durable-operations": noClosureInDurableOperations,
},
configs: {
recommended: {
plugins: ["@aws/durable-functions"],
rules: {
"@aws/durable-functions/no-nested-durable-operations": "error",
"@aws/durable-functions/no-non-deterministic-outside-step": "error",
"@aws/durable-functions/no-closure-in-durable-operations": "error",
},
},
},
Expand Down
Loading
Loading