Skip to content

go: end a header after its go:embed lines (fix embeds under Go 1.25) - #76

Merged
vito merged 1 commit into
mainfrom
fix/per-package-embed
Oct 22, 2026
Merged

vito merged 1 commit into
mainfrom
fix/per-package-embed

Conversation

@vito

@vito vito commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Symptom

With v0.3.0's per-package build, any package with a //go:embed fails to build under Go 1.25. github.com/vito/booklit at b29265743f80 (go 1.25.0) is an example: building cmd/booklit fails like this:

/src/src/errhtml/embed.go:5:3: invalid go:embed: build system did not supply embed configuration
/src/src/render/text/embed.go:5:3: invalid go:embed: build system did not supply embed configuration
/src/src/render/html/embed.go:5:3: invalid go:embed: build system did not supply embed configuration

The failing per-package scripts have no -embedcfg at all, because the go build -n plan never had one. parsePlan and the script writer were fine.

Root cause

The plan is made from the scan's header pack. In the pack, goHeader cuts each Go file down to its imports and then its //go:embed lines, and the file ends right there:

package errhtml

import "embed"
//go:embed *.tmpl *.css

In Go ≤ 1.25, go/build and the go command's copy in cmd/go/internal/modindex/build_read.go read //go:embed lines with findEmbed. That function only runs while the import reader hasn't reached EOF (for r.err == nil && !r.eof). After the imports, the reader's peekByte(true) skips whitespace and comments. In a header, that runs to the end of the file, so findEmbed returns false right away. The result: no embed patterns, no -embedcfg in the plan, and the compile error above. In real source a var follows 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 why embedCheck passed: it only ran under Go 1.26.1.

Fix

When a header has //go:embed lines, goHeader now 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 updated sourcePosition'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 new pkgembed shape 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 with build system did not supply embed configuration. After the fix, all of them match plain go build.
  • TestHeaderEndsAfterEmbeds (go-includes) covers three header shapes: single import, import block, and import _ "embed" with embeds inside a var (...) block. For each, it checks that something other than comments follows the imports, and that go/build finds the same embed patterns in the header as in the source. With the fix disabled, all three subtests fail.
  • I also ran: binary-check, test-binary-check, pgo-check, vendored-check, other-workspace-check, all of gomod-checks/** and dang-sdk/generate/stale. All pass.
  • For a manual check I added a throwaway check, not committed, that builds cmd/booklit from vito/booklit@b29265743f80 with this module. The errhtml exec now compiles with -goversion go1.25.0 ... -embedcfg, and booklit --version runs.

go-sdk/generate/stale failed for me locally only because my engine predates v1.0.0-beta.20's main schema (see 1252d96). This change doesn't touch gobuild's generated client.

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 sipsma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@vito
vito merged commit c25c457 into main Oct 22, 2026
59 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants