Skip to content

Harden handleError() against secondary exceptions#7387

Merged
Jenson3210 merged 3 commits intomainfrom
Jenson3210/fix-docker-label-null-pairs
Apr 16, 2026
Merged

Harden handleError() against secondary exceptions#7387
Jenson3210 merged 3 commits intomainfrom
Jenson3210/fix-docker-label-null-pairs

Conversation

@Jenson3210
Copy link
Copy Markdown
Contributor

Summary

  • Wraps the FindRecipeRunException re-traversal in RecipeRunCycle.handleError() with a try-catch
  • If the tree is too broken for node-level error marker insertion, falls back to Markup.error() on the entire file
  • Ensures errors are always visible even when the tree has invariant violations (e.g. null collection fields after deserialization)

Context

When a recipe throws on a tree with broken invariants, handleError() attempts to re-traverse the tree via FindRecipeRunException to attach an error marker to the specific node that failed. If that re-traversal itself throws (because the tree is too broken to traverse), the secondary exception could escape handleError(). This change catches that secondary exception and falls back to a file-level error marker instead.

Test plan

  • ./gradlew :rewrite-core:test passes

… insertion

Wrap the FindRecipeRunException re-traversal in a try-catch so that if the
tree is too broken for node-level marker insertion, we fall back to marking
the entire file with Markup.error. This ensures the error is always visible
even when the tree has invariant violations (e.g. null collection fields).
DockerPrinter.visitLabel() and visitEnv() used for-each loops directly
on getPairs() which throws NPE when the list is null. Added null guards
matching the existing pattern used for other nullable list fields like
getFlags().
visit(pair.getKey(), p);
if (pair.isHasEquals()) {
p.append("=");
if (env.getPairs() != null) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think Merlin would have fixed this in his AST write PR. Not sure if we'd need this still then, as I would not expect the field to be null ever, but haven't checked (on mobile on plane)

Copy link
Copy Markdown
Contributor Author

@Jenson3210 Jenson3210 Apr 16, 2026

Choose a reason for hiding this comment

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

https://github.com/openrewrite/rewrite/blob/main/rewrite-docker/src/main/java/org/openrewrite/docker/DockerVisitor.java#L133 is null safe, but this one is not.

I will let you decide if we want to have this "defensive" fix in or not :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah I now see neither field had a safe default, so yes then this makes sense, thanks.

Might even want to mark those as nullable then, unless I misunderstood. Still reading from a small screen.

try {
RecipeRunException vt = (RecipeRunException) t;
after = (SourceFile) new FindRecipeRunException(vt).visitNonNull(after, 0);
} catch (Throwable ignored) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good catch!

@github-project-automation github-project-automation Bot moved this from In Progress to Ready to Review in OpenRewrite Apr 16, 2026
@Jenson3210 Jenson3210 merged commit f0ad7a9 into main Apr 16, 2026
1 check passed
@Jenson3210 Jenson3210 deleted the Jenson3210/fix-docker-label-null-pairs branch April 16, 2026 16:56
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Apr 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants