From d1780dbe654889bdd3218696aeb5a7364233c606 Mon Sep 17 00:00:00 2001 From: Sebastian Spaink Date: Tue, 16 Jun 2026 15:51:27 -0500 Subject: [PATCH] format: Fix dropped with-clause after comment in object value (#8785) resolve: https://github.com/open-policy-agent/opa/issues/8765 Signed-off-by: Sebastian Spaink --- v1/format/format.go | 8 +++++++- .../v1/test_issue_8765_with_after_object_comment.rego | 9 +++++++++ ...t_issue_8765_with_after_object_comment.rego.formatted | 9 +++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 v1/format/testfiles/v1/test_issue_8765_with_after_object_comment.rego create mode 100644 v1/format/testfiles/v1/test_issue_8765_with_after_object_comment.rego.formatted diff --git a/v1/format/format.go b/v1/format/format.go index 351ae0a600..b281d78a4c 100644 --- a/v1/format/format.go +++ b/v1/format/format.go @@ -1212,7 +1212,13 @@ func (w *writer) writeWith(with *ast.With, comments []*ast.Comment, indented boo w.write(" as ") comments, err = w.writeTerm(with.Value, comments) if err != nil { - return comments, err + // An unexpectedCommentError from writeTerm signals that it fell + // back to writing the term's original unformatted text — the value + // was written successfully, so don't abort the surrounding chain + // of `with` clauses (issue #8765). + if !errors.As(err, &unexpectedCommentError{}) { + return comments, err + } } return comments, nil } diff --git a/v1/format/testfiles/v1/test_issue_8765_with_after_object_comment.rego b/v1/format/testfiles/v1/test_issue_8765_with_after_object_comment.rego new file mode 100644 index 0000000000..06b545c1f9 --- /dev/null +++ b/v1/format/testfiles/v1/test_issue_8765_with_after_object_comment.rego @@ -0,0 +1,9 @@ +package test + +test_x if { + r := allow with input as { + "a": "b", # a comment + } + with opa.runtime as {"env": {"E": "PROD"}} + r == false +} diff --git a/v1/format/testfiles/v1/test_issue_8765_with_after_object_comment.rego.formatted b/v1/format/testfiles/v1/test_issue_8765_with_after_object_comment.rego.formatted new file mode 100644 index 0000000000..b72cbf7d85 --- /dev/null +++ b/v1/format/testfiles/v1/test_issue_8765_with_after_object_comment.rego.formatted @@ -0,0 +1,9 @@ +package test + +test_x if { + r := allow with input as { + "a": "b", # a comment + } + with opa.runtime as {"env": {"E": "PROD"}} + r == false +}