Skip to content

Commit

Permalink
Allow io.moderne recipes to mutate the execution context
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Jan 24, 2025
1 parent d71f13c commit 04059ce
Showing 1 changed file with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,19 @@ public CursorValidatingExecutionContextView setValidateImmutableExecutionContext

@Override
public void putMessage(String key, @Nullable Object value) {
boolean mutationAllowed = !getMessage(VALIDATE_CTX_MUTATION, false) || key.equals(VALIDATE_CURSOR_ACYCLIC) || key.equals(VALIDATE_CTX_MUTATION)
|| key.equals(ExecutionContext.CURRENT_CYCLE) || key.equals(ExecutionContext.CURRENT_RECIPE) || key.equals(ExecutionContext.DATA_TABLES)
|| key.startsWith("org.openrewrite.maven"); // MavenExecutionContextView stores metrics
assert mutationAllowed
: "Recipe mutated execution context key \"" + key + "\". " +
"Recipes should not mutate the contents of the ExecutionContext as it allows mutable state to leak between " +
"recipes, opening the door for difficult to debug recipe composition errors. " +
"If you need to store state within the execution of a single recipe use Cursor messaging. " +
"If you want to pass state between recipes, use a ScanningRecipe instead.";
boolean mutationAllowed = !getMessage(VALIDATE_CTX_MUTATION, false)
|| key.equals(VALIDATE_CURSOR_ACYCLIC)
|| key.equals(VALIDATE_CTX_MUTATION)
|| key.equals(ExecutionContext.CURRENT_CYCLE)
|| key.equals(ExecutionContext.CURRENT_RECIPE)
|| key.equals(ExecutionContext.DATA_TABLES)
|| key.startsWith("org.openrewrite.maven") // MavenExecutionContextView stores metrics
|| key.startsWith("io.moderne"); // We ought to know what we're doing
assert mutationAllowed : "Recipe mutated execution context key \"" + key + "\". " +
"Recipes should not mutate the contents of the ExecutionContext as it allows mutable state to leak between " +
"recipes, opening the door for difficult to debug recipe composition errors. " +
"If you need to store state within the execution of a single recipe use Cursor messaging. " +
"If you want to pass state between recipes, use a ScanningRecipe instead.";
super.putMessage(key, value);
}
}

0 comments on commit 04059ce

Please sign in to comment.