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 <sebastianspaink@gmail.com>
This commit is contained in:
Sebastian Spaink
2026-06-16 15:51:27 -05:00
committed by GitHub
parent 4f3e238cad
commit d1780dbe65
3 changed files with 25 additions and 1 deletions
+7 -1
View File
@@ -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
}
@@ -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
}
@@ -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
}