Skip to content

caddyfile: Keep an opening brace that ends the input - #8047

Merged
steadytao merged 2 commits into
caddyserver:masterfrom
n0liu:fix-fmt-trailing-brace
Oct 3, 2026
Merged

steadytao merged 2 commits into
caddyserver:masterfrom
n0liu:fix-fmt-trailing-brace

Conversation

@n0liu

@n0liu n0liu commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

Fixes #8046.

Format writes an opening brace lazily. The case ch == '{' branch records openBrace = true / openBraceWritten = false and 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 fmt deletes a brace the user typed:

"localhost {"  ->  "localhost\n"
"{"            ->  "\n"
"{ {"          ->  "{\n"

A Caddyfile whose last line has just opened a block is a normal state while editing, and caddy fmt even points the user at --overwrite, which writes the truncated result back over their file.

This also breaks idempotence, since Format("{ {") is "{\n" and Format("{\n") is "\n". formatter_fuzz.go does 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 TestFormatter cases fail on master (localhost { formats to localhost) and pass with the change, go test ./... is green across the repo, and gofmt/go vet are clean. Checked with the CLI too, where caddy fmt on localhost { now prints localhost {.

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.

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.
@CLAassistant

CLAassistant commented Sep 21, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@steadytao steadytao left a comment

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.

CLA also needs signing.

Comment thread caddyconfig/caddyfile/formatter.go Resolved
@steadytao steadytao added the bug 🐞 Something isn't working label Sep 21, 2026
@steadytao steadytao added this to the v2.11.5 milestone Sep 21, 2026
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.
@n0liu

n0liu commented Sep 22, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, the adjacent-brace point is right and I have pushed a fix for it.

{{ was indeed still losing a brace, and so were {{{ (3 to 1), a {{ and {{ }}. The { case replaced the pending-brace state without emitting the brace already waiting, and only the space-separated path ever flushed.

I moved that inline flush block into a flushOpenBrace closure and now 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 too means a trailing brace gets indented the same way as the other paths, so {{{ now formats as properly nested braces instead of one flat line.

One correction on the report: { { was not losing a brace on the previous head. It formatted to {\n{\n, which keeps both, because the space-separated path already flushed the first one. What it did get wrong was the indentation of the second brace, and that is fixed too now, so { { and {{ both format to the same nested output. I have added regression cases for both, as you asked.

Both new cases fail on the previous head and pass with this change. go test ./... is green across the repo, and gofmt and go vet are clean.

The CLA is on me to sign and I will get that sorted.

@steadytao steadytao left a comment

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.

CLA still needs to be signed. That covers it though. Looks good enough.

@steadytao

Copy link
Copy Markdown
Member

"The CLA is on me to sign and I will get that sorted." -- just do that, it takes like 1 minute 😔

@francislavoie francislavoie modified the milestones: v2.11.6, v2.11.7 Sep 30, 2026

@steadytao steadytao left a comment

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.

CLA signed.

@steadytao
steadytao merged commit b72bf7a into caddyserver:master Oct 3, 2026
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug 🐞 Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

caddy fmt deletes an opening brace at the end of the input

4 participants