Skip leading UTF-8 BOMs in YAML, Starlark, and text templates - #1000
Skip leading UTF-8 BOMs in YAML, Starlark, and text templates#1000vsolano9 wants to merge 3 commits into
Conversation
A file saved with a UTF-8 BOM was parsed with the BOM as content, so the first key gained a leading U+FEFF and rendered as a quoted, escaped key: `test: #@ None` came out as `"\x74\x65\x73\x74": null`. The BOM is a stream encoding marker rather than content, so it is now trimmed in Parser.ParseBytes. That is ahead of the document marker check, which matters on its own: a BOM in front of a leading `---` hid the marker, so the parser prepended a second one and the file failed to parse with "mapping values are not allowed in this context". Trimming in the parser covers every YAML entry point, including data values files, schema and `ytt fmt`. NewDocumentSetFromBytes trims as well so the bytes it retains for AsSourceBytes, which back the source lines shown in template errors, are the bytes that were actually parsed. Only a leading BOM is treated as a marker; one appearing later in a file is ordinary content and is left alone. Fixes carvel-dev#999 Signed-off-by: Victor Solano <victor.solanonunez@gmail.com>
|
If you do not mind extending the pr I would prefer |
Signed-off-by: Victor Solano <victor.solanonunez@gmail.com>
|
Extended in 9228926. Leading UTF-8 BOMs are now handled for |
The lint job fails on two lines in utf8_bom_test.go: revive's line-length-limit counts 96 and 90 against a limit of 80. Hoisting the data.yml source into a named constant and splitting the two MustNewFileFromSource calls brings both under the limit without changing what the test does. golangci-lint v2.12.2 (the pinned CI version) now reports only the four pre-existing unhandled-error findings in pkg/yamlfmt and pkg/yamlmeta, neither of which this PR touches; CI's new-from-rev filter already excludes them. go build ./... and go test ./... are green, e2e included. Signed-off-by: Victor Solano <victor.solanonunez@gmail.com>
35156f4 to
28903f0
Compare
|
Fixed the lint failure at Locally, golangci-lint v2.12.2 — the pinned CI version — now reports only the four pre-existing |
Fixes #999
What
A file saved with a UTF-8 BOM was parsed with the BOM as content. In YAML it became a leading U+FEFF on the first key; in Starlark it caused a compile error; and in a text template it leaked into rendered output.
The YAML path also had a second failure:
ParseBytesdecides whether to prepend a document marker by matchingdocStartMarkerCheckagainst the raw input. A BOM in front of a leading---hid that marker, so the parser prepended a second one and the file did not parse.Where the fix lives, and why
The shared
files.TrimUTF8BOMhelper removes only a leading UTF-8 BOM. YAML parsing invokes it before the document-marker check. Starlark and text-template evaluation invoke it before compiling or parsing their template input.The trim deliberately does not happen in
files.File.Bytes(): that method also feeds the verbatim passthrough path, where a file force-markedtype=textmay be binary and must retain every byte. InEvalText, the trim therefore happens only after the plain/non-template passthrough returns.NewDocumentSetFromBytesalso trims so the bytes retained forAsSourceBytes—which back source lines shown in template errors—match the bytes parsed. The operation is idempotent. A U+FEFF later in a file remains ordinary content.Testing
---, leading#!comment, and multi-document stream, including the hidden-marker failure.starlibraries and text templates behave exactly like their no-BOM equivalentsThe new cases were confirmed failing before their respective implementation changes.
Fresh local validation on 18 Aug 2026:
go test ./... -count=1— all packages pass, includingtest/e2eandtest/filetests(with a locally built versionedyttbinary as required by the e2e harness)git diff --checkpasses; changed Go files aregofmtcleanThe branch remains mergeable with current
develop(a3e1f7b).AI assistance
This patch was prepared with AI assistance. The resulting diff and test output were reviewed locally before submission.