fix(yamlfmt): expand flow collections under the key's real column - #619
Merged
kehoecj merged 1 commit intoJul 31, 2026
Merged
Conversation
A flow collection expanded past the print width took its indentation from the line's TokIndent, which stops before the sequence indicator. Inside a block sequence item the key starts further right, so 'key: [...]' put the bracket on the key's own column, where it is no longer that key's value: 'cfv format --fix' rewrote valid documents into unparseable ones while reporting success. Both '[' and '{' were affected.
Lay the expansion out from the key's own column instead, which also covers anything else sitting between the line indent and the key. The result matches Prettier byte for byte.
kehoecj
approved these changes
Jul 31, 2026
kehoecj
left a comment
Collaborator
There was a problem hiding this comment.
@snowyukitty Thank you for the PR and fix! From your findings, I've updated my parity and functional suite to include validation after the formatting.
Collaborator
|
@snowyukitty Validated both of the other issues on "Two things left out" - those are valid as well. Please submit them as issues if you don't mind |
This was referenced Jul 31, 2026
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Found while testing the release candidate as asked in #551, and rebased onto
37b2b6c.
The bug
cfv format --fixrewrites valid YAML into YAML that no longer parses, andreports success while doing it.
The rewritten file is:
valuessits at column 4 and its expanded value is put at column 4 as well, soit is no longer that key's value.
This shape — a key inside a block sequence item whose value is a long flow
collection — is common in Prometheus and Promtail
relabel_configs, GitHubActions matrices, and Kubernetes manifests. I hit it by running
cfv format --fixover the config files of an unrelated repository.Cause
expandLongFlowSequencestakes the expansion indent from the line'sTokIndent, which stops before the sequence indicator. For- values: [...]that gives 2, while the key actually starts at column 4, sothe bracket lands exactly on the key instead of under it. Outside a sequence
item the two agree, which is why the existing fixtures pass.
The fix tracks the key token while walking back to the line indent and lays the
expansion out from the key's real column, so anything sitting between the indent
and the key is accounted for rather than just
-. The result matches Prettierbyte for byte:
Both
[and{were affected.Tests
pkg/formatter/yamlfmt/testdata/flow_expand_in_sequence.{input,expected}.yamlcovers
[and{inside a sequence item.TestIdempotencypicks the goldenfile up automatically, so the same fixture also pins stability across passes.
cmd/cfv/testdata/format_yaml_flow_in_sequence.txtarpins it end to end:format, then
cfv checkmust still pass, then formatting again must be ano-op.
Both fail on
feat/3.0— the txtar case fails with× relabel.yaml, which isthe corruption itself.
The golden file is byte-for-byte equal to
prettier --parser yamlon the sameinput, checked with Prettier 3, so it encodes the reference layout rather than
my preference.
Verification
go test ./...(Linux,golang:1.26)feat/3.0— same 4 pre-existing failures, nothing newgolangci-lint runon the changed packagesgo vet ./...,gofmt -s -l -e .These all produced unparseable output before and are valid and idempotent after:
a value carrying an anchor (
key: &anc [...]), a flow nested in a flow, amulti-document file, a CRLF file, a quoted key, and a doubly nested sequence.
Empty collections, comments inside a collection, tags, explicit keys, and
brackets or commas inside quoted strings were checked too.
The four failures common to both branches are
Test_CLIWithUnreadableFile,Test_FormatFixUnwritableDirectory,Test_fsFinderWalkDirErrorandTestFetchAndCacheUnwritableDir; each asserts that an unreadable or unwritablepath errors, which does not hold when the suite runs as root in a container.
Two things left out
Prettier first tries the whole collection on its own line and only explodes it
element per element if that still does not fit:
cfvalways explodes. That is a layout difference rather than a correctnessone and it would move a lot of golden files, so it is not in this PR.
Separately, and unrelated to flow collections: when a sequence indicator is
followed by more than one space, the item's sibling keys get re-indented to a
different column and the document stops parsing.
It reproduces on
feat/3.0with no flow collection involved and is untouchedhere — happy to report it properly on #551 if that helps.