Repository navigation
go: end a header after its go:embed lines (fix embeds under Go 1.25) - #76
Merged
Merged
Conversation
A per-package build under Go 1.25 failed on every package with a
//go:embed, such as github.com/vito/booklit's errhtml, imported by
cmd/booklit:
errhtml/embed.go:5:3: invalid go:embed: build system did not supply
embed configuration
The plan is made from the scan's header pack, where a Go file is cut to
its imports, then its go:embed lines, and ends there. Up to Go 1.25,
go/build (and the go command's copy of it in modindex) looks for go:embed
lines only when its scan of the imports stops at a token before the end of
the file. In a header, skipping the comments after the imports reaches the
end, so it found no patterns, and go build -n printed the package's compile
without -embedcfg. Go 1.26 scans the whole file for them, which is why
embedCheck, run only under Go 1.26.1, passed.
A header with go:embed lines now ends with a declaration after them, so the
scan stops before the end. A header without any is as before.
embedCheck runs every shape under Go 1.25.0 too, and gains booklit's: a
main package importing a package that embeds *.tmpl *.css. Under 1.25 every
shape failed before. TestHeaderEndsAfterEmbeds checks the header goes on
past its go:embed lines, and that go/build finds the source's patterns in
it.
Signed-off-by: Alex Suraci <[email protected]>
sipsma
approved these changes
Oct 22, 2026
sipsma
left a comment
Contributor
There was a problem hiding this comment.
Spot-checked: the root cause reproduces on Go 1.25.0 (a header ending in comments gets no -embedcfg; with the trailing declaration it gets one), headers without embeds are unchanged, and the new tests run in CI (embed-check under 1.25.0 and 1.26.1, TestHeaderEndsAfterEmbeds in single-module-check). LGTM.
This was referenced Oct 23, 2026
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.
Symptom
With v0.3.0's per-package build, any package with a
//go:embedfails to build under Go 1.25. github.com/vito/booklit at b29265743f80 (go 1.25.0) is an example: buildingcmd/booklitfails like this:The failing per-package scripts have no
-embedcfgat all, because thego build -nplan never had one.parsePlanand the script writer were fine.Root cause
The plan is made from the scan's header pack. In the pack,
goHeadercuts each Go file down to its imports and then its//go:embedlines, and the file ends right there:In Go ≤ 1.25,
go/buildand the go command's copy incmd/go/internal/modindex/build_read.goread//go:embedlines withfindEmbed. That function only runs while the import reader hasn't reached EOF (for r.err == nil && !r.eof). After the imports, the reader'speekByte(true)skips whitespace and comments. In a header, that runs to the end of the file, sofindEmbedreturns false right away. The result: no embed patterns, no-embedcfgin the plan, and the compile error above. In real source avarfollows the directive, so the reader stops before EOF and the patterns are found.Go 1.26 rewrote this part to scan the whole buffer with
go/scanner, so it finds the directives either way. That's whyembedCheckpassed: it only ran under Go 1.26.1.Fix
When a header has
//go:embedlines,goHeadernow appends one declaration after them (var _ struct{}). The import scan then stops on a token before EOF. Nothing ever compiles a header, so the extra declaration has no other effect. Headers without embeds are byte-for-byte unchanged, so their plan cache keys stay the same. I also updatedsourcePosition's doc in gobuild to describe the new last line. Its mapping is unaffected.Tests
embedCheck(go-dev) now runs every shape under Go 1.25.0 as well as 1.26.1. It also has a newpkgembedshape in the booklit layout: a main package importing a sibling package that has//go:embed *.tmpl *.css. Before the fix, every shape failed under 1.25.0 withbuild system did not supply embed configuration. After the fix, all of them match plaingo build.TestHeaderEndsAfterEmbeds(go-includes) covers three header shapes: single import, import block, andimport _ "embed"with embeds inside avar (...)block. For each, it checks that something other than comments follows the imports, and thatgo/buildfinds the same embed patterns in the header as in the source. With the fix disabled, all three subtests fail.gomod-checks/**and dang-sdk/generate/stale. All pass.cmd/booklitfrom vito/booklit@b29265743f80 with this module. Theerrhtmlexec now compiles with-goversion go1.25.0 ... -embedcfg, andbooklit --versionruns.go-sdk/generate/stalefailed for me locally only because my engine predates v1.0.0-beta.20'smainschema (see 1252d96). This change doesn't touch gobuild's generated client.