Repository navigation
caddyfile: Keep an opening brace that ends the input - #8047
Conversation
Format writes an opening brace lazily: the '{' case records the pending
brace and continues, and the brace is only emitted once a following
regular character is processed. When the input ends while a brace is
still pending, the read loop exits and nothing flushes it, so the brace
is dropped. 'caddy fmt' on a Caddyfile whose last line has just opened a
block therefore deletes the brace the user typed, and with --overwrite
that result is written back over the file.
Flush a pending brace after the loop ends. This also restores
idempotence for inputs such as '{ {', which formatted to '{' and then to
nothing.
The '{' case overwrote the pending-brace state without emitting the
brace already waiting, so adjacent braces lost one: Format("{{") gave
just "{". Only the space-separated path flushed, and it did so with an
inline block.
Move that block into a flushOpenBrace closure and call it from the three
places a pending brace has to be emitted: the existing space-separated
path, the '{' case before it replaces the state, and the end of input.
Using it at the end of input also indents a trailing brace the same way
the other paths do.
|
Thanks, the adjacent-brace point is right and I have pushed a fix for it.
I moved that inline flush block into a One correction on the report: Both new cases fail on the previous head and pass with this change. The CLA is on me to sign and I will get that sorted. |
steadytao
left a comment
There was a problem hiding this comment.
CLA still needs to be signed. That covers it though. Looks good enough.
|
"The CLA is on me to sign and I will get that sorted." -- just do that, it takes like 1 minute 😔 |
Fixes #8046.
Formatwrites an opening brace lazily. Thecase ch == '{'branch recordsopenBrace = true/openBraceWritten = falseand continues, and the brace is only emitted later, when a following regular character is processed. If the input ends while a brace is still pending, the read loop exits and nothing flushes it, so the brace is never written.The result is that
caddy fmtdeletes a brace the user typed:A Caddyfile whose last line has just opened a block is a normal state while editing, and
caddy fmteven points the user at--overwrite, which writes the truncated result back over their file.This also breaks idempotence, since
Format("{ {")is"{\n"andFormat("{\n")is"\n".formatter_fuzz.godoes compute that property, but it only uses it as a go-fuzz corpus priority (return 1/return 0) rather than reporting a failure, so a non-idempotent input is deprioritised instead of surfacing.The change flushes a pending brace after the loop ends. I kept it to that one case rather than also reworking how two adjacent braces are tracked, so the diff stays small.
Verified locally: the two new
TestFormattercases fail on master (localhost {formats tolocalhost) and pass with the change,go test ./...is green across the repo, andgofmt/go vetare clean. Checked with the CLI too, wherecaddy fmtonlocalhost {now printslocalhost {.Assistance Disclosure
AI used. I used Claude Code to fuzz the Caddyfile formatter for idempotence, which is how this was found, and to help trace the root cause and draft the fix and the test cases. I reviewed the change, confirmed the before and after behaviour myself, and ran the test suite locally.