Skip to content

gobuild: take the repeated work out of a module's first per-package build, build test binaries as a set, and make failures readable - #104

Merged
sipsma merged 54 commits into
mainfrom
sipsma/go-build-fixed-cost
Nov 17, 2026
Merged

sipsma merged 54 commits into
mainfrom
sipsma/go-build-fixed-cost

Conversation

@sipsma

@sipsma sipsma commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

This takes about 1.5 s off a Go module's first build through the per-package build on a fresh engine, with more expected in CI, where each removed step is several times slower; keys each build by only the source it reads, so dagger/dagger's CLI builds 1.8 s faster after a commit that doesn't reach it; runs dagger/go's tests from prebuilt test binaries with no Go cache volume; makes two kinds of failure readable; fixes builds of modules under testdata/ and of a dependency's testdata package; and adds GoPackages.testBinary, which builds a set of packages' test binaries from one plan, as go test plans several packages. Builds of later modules on the same engine take the same time and make the same number of API requests as before.

Draft while it is measured in dagger/dagger's CI.

What each commit does

Measured on an idle amd64 machine (c7a.4xlarge), on a built-in Go SDK module's first call on a fresh engine, in 4 interleaved pairs per commit, each against the commits before it.

  1. One dependency call for a build's archives and its binary. Codegen's archives and the module's binary each called Gobuild.dependencies for the same packages, with arguments that differed only in whether the caller links. On a fresh engine the second call ran again: it re-declared every package (about 1,400 requests) and hashed each one's object code. The call now returns no object code, so both callers make the same call and the second is a cache hit. −0.76 s.

  2. A dependency's error named by its own import path. With the first commit, a failed read of a dependency's object code panicked instead of returning its error. With a unit test that panics without the fix.

  3. The archives manifest's go settings recorded in the plan's exec. write-manifest env ran in an exec of its own after the dependency call; it now runs in the plan's exec, in the same container. One fewer exec: no change on an idle machine; in CI's test engines that exec and its wait take about 0.7 s.

  4. The dependency call asks for its directory IDs at once. Returning about 450 package directories made the SDK ask for each one's ID in turn, 0.25 s after the walk on an idle machine and 0.7 to 1.1 s in CI. The call now asks for them 32 at a time before returning. −0.31 s.

  5. The dependencies' object code read in a call of its own. With the first commit, a build that links read each dependency's object code itself, about 450 requests on every binary build, including later modules'. Gobuild.linkObjects does those reads in a call the engine caches, so a later build of the same dependencies finds them in one request. −0.39 s on first builds; later modules back to the request count before this PR (331 against 780 without it).

  6. A failed module download reports every source it tried, not only the last one's error. With a go-dev test.

  7. A build whose setup fails reports why, what the go command wrote, not only its exit code. With a go-dev test.

  8. A block that resumes keeps the plan's directory. A parser fix for -cover plans, where a package's steps resume after another's.

  9. A set of packages' test binaries from one plan (GoPackages.testBinary(ws)). Built one by one, each test binary ran its own discovery, plan, test main and dependency call; as a set they run once. Each binary is still byte for byte what go test -c builds. In dagger/dagger's test-packages shard (102 test binaries), measured on an m8gd.4xlarge in 4 interleaved pairs: 477.6 s median, against 512.3 s one by one and 473.4 s for one go test. As on go test's command line, a flag without a package pattern applies to every package of the set.

  10. A package without test files for the target is left out of a set, as go test leaves it out.

  11. A gccgo build of a set is refused, since the per-package build replays gc's plan.

  12. gofmt. Formatting only: three files main already had unformatted.

  13. A module under a testdata directory finds its packages. The testdata rule was applied from the workspace's root instead of the build's directory, so a module that is itself under testdata/, as fixtures often are, had every package left out, and its archives' manifest came out empty. With a unit test and a go-dev check.

  14. The archives list the packages they hold, so code generation's type-check needs no go command. Archives writes listing.json beside manifest.json: every package outside the module, with its name and imports, from the plan it already makes. golangci-lint's export driver (export-driver: answer a declarations-only caller from the builder's listing golangci-lint#3) answers the built-in Go SDK's type-check from it, where it ran go list -deps against a module cache. With that driver change and the built-in Go SDK's change in dagger/dagger, that step goes from 0.46 s to 0.09 s, and each later Go module load gets 0.19 s and 0.29 s faster on an idle c7a.4xlarge (4 interleaved pairs, the second and third module), with a fresh engine's first module within noise. On its own, this commit only adds the file. The listing holds nothing of the module's own packages, so an edit of them leaves it unchanged.

  15. Dependency calls shared across modules (sharedImports). A module's first build on a fresh engine looked up each of its ~450 dependency packages in the engine image's entries one by one, at about two API requests and six engine calls each. The dependency call is keyed by exactly the packages it compiles, and a module's own imports make that set unique to it. go's new optional sharedImports setting names imports that many builds share, such as what generated code imports. The dependency call then runs in layers: the standard library's packages those imports reach, their other packages, then the rest. The first two are the same call in every build that keeps those packages' module versions, whatever else it imports, so an engine image can ship them whole. (archiveBuilds no longer reports them: item 39.) Builds without the setting make exactly the call they made before. With dagger/dagger #15127, which passes the setting from the built-in Go SDK and ships the calls in the image, measured on one 16-CPU amd64 machine on top of this PR's other commits: a module's first build on a fresh engine is 0.3–0.5 s faster idle and 0.7 s with five loads at once, makes about 950 fewer API requests, and halves the CLI's CPU. A second module on the same engine is unchanged.

  16. Every workspace is mounted at /src by default. A workspace without a mountPath was mounted at /src/<workspace name>, and that path is in every package's compile, so the same source under another name (another checkout's directory, a worktree, CI's repository name) rebuilt all of its own packages: on dagger/dagger, 374 package compiles against 30 for the same commit under one name. Behavior change: a workspace whose tests expect the repository's name in their working directory must now set mountPath.

  17. Each package's source is synced as soon as the plan is read, alongside the dependency call, instead of after it. On dagger/dagger, 354 source syncs ran at once after the dependency call and delayed the first compile after a commit by up to 0.4 s.

  18. A package's testBinary, taken from a collection, is planned with its collection's other packages. Discovery, the plan, the test mains and the dependency call are those of the collection's set, which the engine runs once for all of its members, and each member compiles only what its own binary links, so it is ready as soon as its own link is. With -coverpkg, or a -gcflags, -asmflags or -gccgoflags without a package pattern, a package's binary is planned alone, so it stays byte for byte go test -c's.

  19. A test binary's command-line flags are read as the go command reads them, including a quoted GOFLAGS such as "-gcflags=-N -l".

  20. A test binary with an empty compiler-flag value is planned alone, since an empty value clears the flags of the packages on the command line alone.

  21. A Go 1.25 test plan's headers are read. go test -c -n makes the test main's work directory first; Go 1.26 then prints each package's header after its work directory's mkdir, Go 1.25 before it. In a Go 1.25 plan every header landed one block off and the plan failed with "no link output", so a test binary of any module on Go 1.25 failed to build, on main too. A header followed by a new block's mkdir is now that block's.

  22. A module's tests run from prebuilt test binaries, without Go cache volumes. dagger/go's test function ran go test under otelgotest in a container with the shared go-mod and go-build cache volumes. Each module's test binaries now come from the per-package build, one set for the packages that run, and each package's binary runs in its own exec from its directory under otelgotest's prebuilt mode, which reports what go test -json would: events, output and per-test spans. GOFLAGS' testing flags reach the binary as go test passes them. otelgotest and the test report helper are built by the per-package build too, so nothing in the test path mounts a Go cache volume, and an unchanged package's run is a cache hit of its own. Differences from go test: go vet no longer runs with the tests (golangci-lint's govet covers it); -failfast stops the failing package, not others; a module's test doesn't compile packages without tests; packages run all at once, each with its own nested Dagger session.

  23. A package with no file for the target is left out of a test set. A directory holding only another platform's test files made the set's go list fail and the whole set with it; go test ./... leaves such a package out, and the set now does too.

  24. go test's behavior is kept in a module's prebuilt test runs. A module's test runs every package that builds and reports every one that doesn't; report files are writable by a non-root test user; a quoted GOFLAGS flag and a relative -outputdir behave as for go test; a package with no test files for the platform has no binary in every path; otelgotest builds alongside the test binaries.

  25. A test package's errors are kept, and "no packages" is told by the source. A package with an unreadable test file stays in the set so that it fails, unless build constraints merely leave out all its files; a module's test fails as go test ./... does when no package matches; gccgo asks go list before go test -c.

  26. Nested modules' files are left out of a module's "no packages" test.

  27. A dependency's testdata package builds. A build that imports a package under a dependency's testdata directory, as grpc's programs import google.golang.org/grpc/testdata, failed in the plan with "no required module provides package", where go build succeeds: the module's plan pack left out every testdata file. The pack now keeps them as any other file (Go files as their headers), leaving out only test files. With a go-dev check.

  28. Each build is keyed by only the source it reads. A binary, test binary or archives call took the whole workspace's Go source and header pack, so an edit of any Go file anywhere ran the call again in full. Gobuild.scope keeps what the build reads: the files of the directories its plan reads (everything below one that embeds), every go.mod and go.sum, and the matching part of the header pack, each keyed by its content; for a build without tests, not the test files. The whole source is kept when the build may read a file this can't tell: C, C++ or assembly in those directories, cgo, a symlink, a flag or GOWORK naming a file, a package named by import path, version control metadata, vendor mode, or a toolchain switch. Measured on dagger/dagger after a commit that doesn't reach the CLI (#15126; a fresh beta.27 engine per step, c8gd.4xlarge, 4 interleaved rounds, medians): the CLI's binary 6.30 s → 4.46 s, its binary call a cache hit; archives of every package, where the commit changes the module so they rebuild anyway, 30.40 s → 31.94 s, the scope's own work, which item 30 removes; first builds on a fresh engine unchanged for the CLI (77.1 s both) and 135.8 s → 137.9 s for archives. Behavior change: go build reads test files' headers too, so a build whose test file doesn't parse used to fail and now builds. With a go-dev check that an edit of a file embedded outside the build leaves the plan as it was.

The 1.5 s is the sum of the per-commit measurements, not one measurement. dagger check -l of dagger/dagger doesn't change.

  1. A go.sum's module versions as a module proxy directory (Go.moduleProxy(goSum)). It downloads every module version a go.sum names, as builds download them, and returns one directory laid out as a Go module proxy, for GOPROXY=file://<dir>. A caller that fills a module cache volume with go mod download today, such as a test container, can read results instead. Each version is checked against the go.sum, and any failed or mismatched download fails the call. It leaves out version lists, so it answers only for exact versions. With a go-dev check.

  2. A build of every package keeps the whole source. Such a build, such as a lint's archives of ./..., reads the module's whole source, so the scope (item 28) saved nothing there and cost about 1.5 s after a commit that changed the module. Gobuild.scope now returns the source as it is for it, before any work. Measured on dagger/dagger after #15126, 3 interleaved rounds: archives of every package 30.40–31.00 s without the scope, 30.66–31.01 s with this commit, against 31.80–32.30 s with item 28 alone.

  3. A test build whose source holds version control metadata is scoped too. The scope kept the whole source whenever it held .git and the like, as go build stamps a binary from it. go test -c doesn't, unless -buildvcs is true (checked on Go 1.27.3: go build stamps 4 vcs fields, go test -c none, go test -c -buildvcs=true 4), and archives link nothing. A test that includes the repository's .git, as dagger/dagger's e2e/helm tests do, so ran its test binaries again after every commit (1.1 s on helm's second call). The fallback now applies to a build without tests, or with -buildvcs true.

  4. A module's test tools build beside its test binaries. otelgotest and the test report helper were built while testBinaries' arguments were evaluated, so on a fresh engine their build ran before any module's test binaries; with the scoped builds (item 28) that put about 11 s ahead of the first test. They are now a block that testBinaries builds in a Dang {{ }} beside the set, so both build at once; a tool that fails still fails the call. Measured on m8gd.4xlarge against main: on a fresh engine the first test package starts at 60.2 s against 60.6 s (74.6 s without this commit); on a second call after an unrelated commit, e2e/helm's tests start at 7.65 s against 7.5 s.

  5. A module's dependencies' archives are keyed by its imports alone. Code generation asks for archives of a package's dependencies, without tests; they depend only on what the module's own packages import outside it, yet the call took the module's own source and ran its setup, plan and walk again for every module. Gobuild.scope now gives such a build a stand-in for the module's own packages: its go.mod and go.sum with another module path, and a package that imports exactly those imports (a main package if it is one). A later module with the same requirements and imports, such as one with the same generated code, makes the same call, which Dagger answers from its cache. A module whose own packages could make the stand-in's result differ keeps the build it had (build constraints, cgo or C sources, embeds, symlinks, internal-package rules, nested or replacing modules, a self-naming go.mod, a go.work or vendor directory, a default.pgo, package-pattern flags, -mod=mod, a toolchain switch, or an error the go command would report in them); when the stand-in's build fails, as when a dependency imports the module back, go.dang builds the archives from the module's own source. Measured on two modules with the same imports, fresh engines, 3 interleaved rounds: the second module's call 1.23–1.26 s → 0.83–0.87 s, first calls unchanged within noise. In go-sdk's generation (two such calls per module), measured by its group, 4 interleaved rounds on fresh beta.27 engines: a second new module's generate 7.58 s → 6.54 s, regenerating it after an edit 5.44 s → 4.90 s, its first call and a fresh engine's first module unchanged. With dependencyArchivesCheck and backImportArchivesCheck.

  6. Scope reads the go command's own settings. GOFLAGS, GOWORK and GOTOOLCHAIN set with go env -w don't show in the container's environment; the scope now asks the go command (go env -json, once per base), and keeps the whole source if that fails.

  7. The dependency call takes its package list as a file. It took the list as a JSON string argument, up to 1.1 MB for a build, which the engine held three times per call: in the call's frame, and in the frame and stored value of the exec that runs it. On a whole pull request's runs these records were 1.0 GB of 1.68 GB of stored call records. The build now writes the list to a file and imports it through core.NewHost(), which the engine keys by the file's content, name and mode, so builds with the same list find the same call, as before; each layer's call does this. No measurable cost, as two builds interleaved, 4 rounds on fresh engines, medians: dagger/dagger's CLI cold 77.10 → 76.99 s, with nothing changed 3.35 → 3.29 s, after a one-line edit 7.11 → 7.06 s; go-sdk's generation of a second module 6.55 → 6.27 s, regenerating it after an edit 5.22 → 5.00 s. Written by the Remote Cache group's lead for this, ported onto this PR.

  8. A module proxy runs its downloads 32 at a time, as a build's own downloads do. It started every one of a go.sum's module versions at once, one exec each: 745 for dagger/dagger's, where a CI run had 11 of them fail looking up proxy.golang.org through the engine's resolver. Cold on dagger/dagger's go.sum, 3 interleaved rounds: 27.8 s median before, 24.7 s after.

  9. A package's test binary is planned alone when its collection's plan fails. Since item 18, a package taken from a collection is planned with the collection's other tested packages, and that plan fails if any of them can't load: a sibling it never imports failed its test binary, where go test -c of the package alone builds it. With a go-dev check.

  10. A test binary's plan is shared only within a narrowed collection. Since item 18, a package taken from a collection was planned with every package of the collection with tests, so one package's test binary from a module's whole collection planned and compiled for all of them: on dagger/dagger, 78 s on a fresh engine against 36 s planned alone, compiling the dependencies of 1,596 packages against 216. A package from the whole collection is now planned alone, as go test -c plans it; one from a subset of the collection shares that subset's plan, so a caller that builds many packages' test binaries passes their subset. With a go-dev check of which packages each plan names; item 37's check now takes its packages from a subset, where the shared plan, and so the fallback, still applies.

  11. archiveBuilds returns no IDs. It returned each shared dependency call (item 15) as its ID turned into a string, in sharedDependencies. An ID names a result only in the session that made it, so on another engine those strings named the wrong result or none: the image export failed with "missing shared result" or a wrong type. They are removed, with the record of the calls that only fed them. A module's object can't hold another module's objects, so nothing takes their place: archiveBuilds returns each package's build, the downloads and the tools, as before. The shared dependency calls themselves are unchanged. Nothing else in this PR keeps an ID as data.

  12. gobuild's entrypoint builds with go-sdk's main, without cache volumes. dagger/go's go-sdk pin moves to go-sdk main (3adb1f4, feat(go): target v1.0.0-beta.27 go-sdk#105), and gobuild is generated again. gobuild still builds with plain go build: it is what the per-package build runs, so it can't be built by it, and its scope now says so explicitly (daggerGo = false). That build no longer mounts the go-sdk-v2-mod and go-sdk-v2-build cache volumes; Dagger caches the build exec by its inputs. The entrypoint's runtime image is go-sdk's pinned one, and its description comes from the module. gobuild's own build gets a new cache key, so an engine whose image doesn't ship this pin builds it once. Done by the go-sdk cache-volume group.

  13. The back-import check publishes the version the dependency requires. go-dev's backImportArchivesCheck had its dependency require a version of the module that no proxy had. Real pairs (golang.org/x/crypto requiring golang.org/x/net) require a published version, so the fixture now publishes it too. The check passes as before: the stand-in's build fails on the checksum a go.sum never has for its own module, and the archives come from the module's own source. Test-only.

  14. A failed plan keeps the go command's error. A build whose plan failed printed the last 40 lines of the go command's -n output. go list -e reports a package's error, such as a missing go.sum entry for a module a dependency imports, and then plans the rest of the build, so those lines were build steps and the error was cut off. The plan's script now prints the lines of that output that aren't plan steps, up to 60, and the last 40 lines only when there are none. With a go-dev check that builds such a module.

  15. A failed exec's output reaches the caller. A gobuild function that failed on an exec it only forced, such as the last exec of a custom base, reported exit code: 1 and nothing else: the engine's error carried the exec's output, but gobuild returned it unwrapped from most call sites, and the entrypoint reports a function's error as its message alone. Each exported function now adds the exec's stderr, or its stdout when stderr is empty, to its error, unless the message holds it already. With a go-dev check that builds with a failing base.

  16. A plan error the go command gives after its dependencies is kept too. A cgo package with a flag the go command rejects fails only once it is planned itself, after hundreds of its dependencies' steps. The blank lines around them and the go command's C compiler probes took all of item 42's 60 lines; both are now left out. With a go-dev check that builds such a package.

  17. dagger/go's tools start building together. On an engine that holds none of them, gobuild's own binary, gohdr, write-manifest and Go's pack were each a Go build from nothing, and they ran one after another. gohdr's source moves from a string in gobuild to gomod/helpers/gohdr, and gomod builds it and write-manifest, for both machines, as it builds go-includes. go.dang starts gobuild's build, gohdr and, for archives, write-manifest together with the scan, and hands gohdr to gobuild's constructor and write-manifest to archives and archiveBuilds; pack starts as soon as a build's setup knows its target. Each tool directory is declared for linux/amd64 for every build, as its contents don't depend on the engine or the target. Measured on fresh arm64 engines, medians: the archive builds an engine image's entries need, 26.7 s → 21.9 s; a module's first binary build, 27.0 s → 23.9 s; no change where the tools are already held. Done by the helper-tools group.

  18. gohdr's tests run without Go cache volumes, as they did in gobuild's check before item 45 moved them. Test-only.

  19. gohdr's build is keyed by its own source. Item 45 built gohdr from dagger/go's module source as it is, which keyed the build by the whole of dagger/go: another dagger/go that leaves gohdr unchanged built it again, and ran again every module download, plan and link that mounts it. gomod now writes gohdr's go.mod and main.go out from their contents, as when gohdr was a string in gobuild. Measured on fresh arm64 engines, medians of 3: archives built again from a dagger/go that differs in one unrelated line, 7.4 s → 2.7 s. With a gomod check that builds gohdr from two directories holding the same files. Done by the helper-tools group.

  20. gohdr's listing and gobuild's reading of it are tested apart. A gobuild test built gohdr from gomod/helpers/gohdr, which the checks that test gobuild as a module of its own don't have, so they failed after item 45. gohdr's test now checks the exact text it lists for a fixed pack, and gobuild's test parses that same text.

  21. Each build call starts gobuild's own build first. A build's first read of the scan, such as its Go version, ran the scan before anything asked for gobuild's build, the longest a per-package build waits for on an empty engine. Each build call, binaries, test binaries and archives, now asks for item 45's tool builds first, so they start with the scan. Finding a module runs no scan and waits for none of them. On fresh arm64 engines, the archive builds an engine image's entries need: 21.2 s → 18.9 s (medians). Done by the helper-tools group, with item 48.

  22. go-includes and gohdr are built from their own files, keyed by content. go-includes was built from dagger/go's module source as it is, so every dagger/go version, even one that left it unchanged, built it again and ran every scan again; gohdr avoided that only by writing its source into the recipe (item 47). Both now build from a directory of the tool's own Go files, go.mod and go.sum, each keyed by its content, so a dagger/go that leaves a tool unchanged finds its build. Measured across sessions and engine restarts: after a pin move that changes nothing in the tools, go-includes, the scans and gohdr are all found instead of rebuilt. A gomod check builds both tools from two sources differing outside them. Done by the key-by-content group.

  23. write-manifest is built from its files, keyed by content, as item 50's tools are. It was built from its git checkout, and an engine gives a checkout its content key only after the checkout has run, so an engine image's build of it was keyed by the checkout's recipe while a fresh engine of the image keyed it by the checkout's content. On another platform, where only that key can match, a fresh engine built write-manifest again: an arm64 image made on amd64 did, twice, on its first Go module call. No cost measured where the tools are already held (17.68 s against 17.79 s, medians of 6 pairs). Done by the write-manifest group.

Checks

CI's Dagger checks report as commit statuses; they passed on the commit of item 44; running on the newer commits.

A Go module's runtime build makes two dependency calls for the same
packages: one for the archives codegen type-checks against, and one for
the binary. Their arguments differed only in links, whether the caller
reads each package's object code, so on a fresh engine the binary's call
ran again: it re-declared every package, and read and hashed the object
code of each, about 1 s of every first build on an idle machine and 4 s
in CI's test engines.

The call now returns no object code, so it takes the same arguments
whoever calls it, and the binary's call finds the archives' call. A build
that links reads each package's object code from the directory the call
returns, keyed by its content as before, while its own packages compile.
A build that doesn't link, such as a lint's archives, reads none.

Signed-off-by: Erik Sipsma <[email protected]>
A dependency the dependency call built is no node of the walk that reads
it, so a link whose dependency's object code couldn't be read looked up
a node that isn't there and panicked, losing the error. Each result now
carries its import path, which its dependents' errors name.

Signed-off-by: Erik Sipsma <[email protected]>
An archives manifest records the go command's settings where the plan ran
it, which write-manifest env read in an exec of its own on the plan's
container, after the dependency call: one more exec start on every
archives build's path, about 0.7 s in CI's loaded test engines. The plan's
exec now runs it after the go command, in the same container and
directory, and the manifest reads its env.json from there.

Signed-off-by: Erik Sipsma <[email protected]>
The dependency call returns each package's directory, and returning an
object sends its ID, which the SDK asked the engine for one directory
after another: about 450 requests in a row after the walk, 0.25 s of the
call on an idle machine and 0.7 to 1.1 s in CI's test engines. The call
now asks for them several at a time before returning; the SDK remembers
an ID once asked for.

Signed-off-by: Erik Sipsma <[email protected]>
Since the dependency call stopped returning object code, a build that
links read each dependency's object code itself, a content-keyed sync
per package that split: about 450 requests and engine calls on every
binary build, even a later module's whose dependency call was a cache
hit. Those reads are now LinkObjects, a call the engine caches by its
arguments, the package directories and the files in them: a later build
of the same dependencies finds all of their object code in one request.
The binary still reads it while its own packages compile, and a build
that doesn't link reads none.

Signed-off-by: Erik Sipsma <[email protected]>
A module download that failed reported only the go command's error,
which names one source: a direct fetch's failure wins over a proxy's. So
when the proxy failed and the direct fetch then needed a git the image
doesn't have, the error showed only the missing git, and why the proxy
failed was nowhere. The download now runs with -x, and a failure reports
its log of each request and its answer as well as the error.

Signed-off-by: Erik Sipsma <[email protected]>
A build's first exec, which asks the go command for the build's go.mod
files and their go.sums, reported a failure as "go mod download: exit
code: 1": its error left out what the go command wrote, such as a go.mod
asking for a newer Go than the toolchain's. Nine CI runs of dagger/dagger
on 10 Nov failed that way with nothing in their traces to say why. That
exec's error, and those of the other execs before the downloads (go env
GOMODCACHE, the source's imports, and the module cache's checksums), now
carry what the exec wrote, as a download's and a compile's errors do.

Signed-off-by: Erik Sipsma <[email protected]>
With -cover, a covered package's steps come in two parts: its cover run,
which leaves the plan in its own work directory, and, after the next
package's block, its compile, which runs in the source through a "cd" that
the plan printed in that other block.

The parser carried the previous block's work directory into the next
block, which gave that block a dependency on a package it doesn't import,
and carried nothing into the covered package's block when its steps
resumed, so its compile ran in its work directory and didn't find its
test files. A plan of several test binaries with -cover has both.

A block now never starts in another block's work directory, which its
exec doesn't have, and gets the plan's directory whenever its lines start
or resume.

Signed-off-by: Erik Sipsma <[email protected]>
GoPackages.testBinary builds the test binaries of the selected packages
that have test files as go test plans several packages: one discovery,
one plan (go test -c -n of them all), one step that writes every test
main, and one dependency call, and each package more than one binary
compiles is compiled once. Each binary is byte for byte the one go test -c
builds for its package alone, but for build IDs, as a package's own
testBinary is.

Built one by one, each test binary of a module ran all of those itself:
dagger/dagger's test-packages shard builds 102 of them, and each one's
discovery, plan, test main and dependency call took most of the time
before its own packages compiled.

In gobuild, TestBinaries takes the packages; discovery follows imports from
several package directories; a plan of several test binaries links each
in its test main's block, and names each package variant by its ID in a
go list -deps -test listing, as an archives plan does, keeping the test
mains. The step that writes test mains now runs alongside the walk rather
than before it, for one test binary too: only its link reads it.

Signed-off-by: Erik Sipsma <[email protected]>
…y set

A selected package can have test files that no file builds for the
target, such as a command with only a _windows_test.go on Linux. go test
builds it no test binary, and a set that held it failed whole: its plan
compiled the package and linked nothing for it.

The plan now asks go list which packages have test files for the target,
and plans and writes test mains for those alone, by import path, in the
same order in both steps. The doc comments also say what a set's build
flags apply to: as on go test's command line, a flag without a package
pattern applies to every package of the set.

Signed-off-by: Erik Sipsma <[email protected]>
A gccgo set built each package with go test -c, which writes no binary for
a package whose test files are all for another platform, and so failed
the whole set. Nothing tests gccgo here, and gccgo has no plan to share:
a gccgo set is refused, as gccgo archives are, and each package's
testBinary builds it as before.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma sipsma changed the title gobuild: take the repeated work out of a module's first per-package build, and make failures readable gobuild: take the repeated work out of a module's first per-package build, build test binaries as a set, and make failures readable Nov 11, 2026
sipsma added a commit to dagger/dagger that referenced this pull request Nov 11, 2026
For one CI run only, to read test-container with dagger/go#104
(6056363d94) against this PR's previous run: the built-in Go SDK's
dagger/go, and the workspace's go-sdk pin, at a go-sdk commit whose
daggerGo names that dagger/go, with the go-sdk modules regenerated. The
dagger/go clients and the golang module stay at b0476af. A revert
follows this commit.

Signed-off-by: Erik Sipsma <[email protected]>
sipsma added a commit to dagger/dagger that referenced this pull request Nov 11, 2026
Formatting only: gofmt -l listed main.go, plan.go and walk.go, already on
main.

Signed-off-by: Erik Sipsma <[email protected]>
A build of every package of a module, such as its archives for a lint,
followed every package of the source but those in a testdata directory,
by their paths in the source, from the workspace's root. So a module that
is itself under a testdata directory, as a fixture often is, had all of
its packages left out: discovery downloaded none of its modules, and the
plan fetched each one on its own instead, outside the downloads every
build shares. The testdata rule is the go command's "./...", which
matches from the build's working directory: a testdata directory below
it. It now applies there.

Signed-off-by: Erik Sipsma <[email protected]>
Archives' plan already lists every package it compiles. It now lists
their imports too, and writes the packages outside the module, with their
names and imports, beside the manifest (listing.json). golangci-lint's
export driver answers a caller that type-checks the module from source,
such as code generation, from it, with no go command and no module cache.
It holds nothing of the module's own packages, so an edit of them leaves it
as it is.

Signed-off-by: Erik Sipsma <[email protected]>
…r own

A build's dependency call is cached by exactly the packages it compiles, so
two modules share it only when they import exactly the same packages. A
module that imports one package beyond its generated code's imports makes a
call of its own, and on an engine that hasn't made it, that call looks up
each of its packages one by one: about two requests and six engine calls
for each of ~450 packages, though every one is already built.

go's new sharedImports setting names imports that many builds have, such as
what generated code imports. The dependency call then runs in layers: the
standard library's packages those imports reach, then their other
packages, then the rest, each given the archives of the layers before it.
The first two are the same call in every build that keeps those packages'
module versions, whatever else it imports; a build that changes one of
those versions still shares the standard library's. archiveBuilds names
the shared calls it made, by ID, so that a cache that is to have them
whole, such as an engine image's, can take them.

Without sharedImports, the dependency call is the one it was.

Signed-off-by: Erik Sipsma <[email protected]>
A workspace without a mountPath was mounted at /src/<workspace name>, the
last segment of its address. That path is in every package's compile, in
its script and in the file names the compiler records, so the same source
under another name rebuilt every one of its own packages: another
checkout's directory, a worktree, or CI's repository name against a local
checkout. On dagger/dagger, a commit built under a second name reran 374
package compiles where the same commit under one name reran 30.

A workspace made from a directory or a git ref was already mounted at
/src for the same reason. Every workspace now is, and mountPath remains
the way to mount it elsewhere, such as for tests that expect the
repository's name in their working directory.

Signed-off-by: Erik Sipsma <[email protected]>
The walk selected and synced each package's source in the package's own
goroutine, which starts only after the module cache is prepared and the
dependency call returns. A package's source depends only on the plan,
and the first compile after a commit waits for its own: on dagger/dagger,
354 source syncs ran at once after the dependency call and took up to
0.4 s before the first package exec could start.

The syncs now start as soon as the build has its plan, alongside the
module cache's preparation and the dependency call.

Signed-off-by: Erik Sipsma <[email protected]>
A package's testBinary, taken from a collection, is now planned with the
test binaries of the collection's other packages: Gobuild.TestBinary takes
the set, and its discovery, plan, test mains and dependency call are those
of TestBinaries of the set, which the engine runs once for every member.
Each member compiles only what its own binary links, so it is ready when
its own link is, as a caller that starts each package's tests once its
binary arrives needs. Built one by one, each test binary ran all of those
itself.

With -coverpkg, or -gcflags, -asmflags or -gccgoflags without a package
pattern, what a flag reaches depends on which packages are on go test's
command line: a package's binary is then planned alone, so that it stays
byte for byte go test -c's of that package. testBinaryCheck now takes
each package from its module's collection and adds a -gcflags=-N -l
variant; without the rule, it and the -coverpkg one differ.

Signed-off-by: Erik Sipsma <[email protected]>
Whether a package's test binary is planned alone was decided in go.dang,
which split GOFLAGS on spaces: a quoted GOFLAGS such as "-gcflags=-N -l",
one flag to the go command, escaped it, and so did a value with leading
spaces. gobuild now decides, from the flags and the base's GOFLAGS split
by splitGOFLAGS, the go command's own rules, with each value trimmed as
the go command trims it, and with the flag's value as the next argument
when it has none of its own.

Signed-off-by: Erik Sipsma <[email protected]>
An empty -gcflags, -asmflags or -gccgoflags value clears the flags of the
packages on the go command line alone, so a binary planned with its set
could differ from go test -c's of its package alone. It is planned alone.

Signed-off-by: Erik Sipsma <[email protected]>
go test -c -n makes a test binary's main package's work directory first.
Go 1.26 then prints each package's header after its work directory's
mkdir, and Go 1.25 before it. So in a Go 1.25 plan the first header, the
next package's, landed on the test main's block, every later header one
block off, and the plan failed with "no link output": any test binary of
a module on Go 1.25 failed to build.

A header followed by a new block's mkdir is now that block's.

Signed-off-by: Erik Sipsma <[email protected]>
…lumes

dagger/go's test function ran go test under otelgotest in a container
with the shared go-mod and go-build cache volumes, so every test check
downloaded and compiled there, outside Dagger's own cache. otelgotest
and the test report helper were built with plain go commands in that
container too.

Each module's test binaries now come from the per-package build: one set
for the packages that run, as go test -c builds several packages. Each
package's binary runs in its own exec, from its directory, under
otelgotest's prebuilt mode, which runs a test binary as go test -json
runs one and reports the same events, output and spans. Nothing in the
test path mounts a Go cache volume, and an unchanged package's run is a
cache hit of its own.

What go test did that the binary's run now does here:
- The testing flags in GOFLAGS (-failfast, -short, -count, ...) reach the
  binary as go test passes them; the build flags reach the plan.
- When the set fails to build, each package is built alone, so a package
  that doesn't compile fails its own tests and the others still run.
- A package whose test files are all for other platforms reports no test
  files and passes.
- GoModule.test compiles the packages without tests too, as go test ./...
  does, and leaves out vendor and testdata directories as ./... does.
- A gccgo build, which has no set, builds each binary with go test -c.

go vet no longer runs with the tests, and -failfast stops a package, not
the others.

otelgotest is built from otel-go's source at a pinned commit, the report
helper from this repository's source, both by the per-package build. The
helper reads the exit status from a file the test exec writes, so a
package's report needs no evaluation of its exec to be set up.

Signed-off-by: Erik Sipsma <[email protected]>
A test binary set asks go list which of its packages have test files. A
package none of whose files builds for the target, such as a directory
holding only a _windows_test.go on Linux, made that go list fail, and
with it the whole set. go test ./... leaves such a package out. go list
-e now leaves it out of the set too; a package with a real error still
fails, when the set's plan lists and builds it.

Signed-off-by: Erik Sipsma <[email protected]>
From review of the prebuilt test runs:
- GoModule.test no longer compiles the packages without tests first: it
  stopped there, before any package's tests ran, when one failed, and it
  compiled with the build setting's flags, which are go build's alone.
  It now runs the packages with tests, reports every package that fails
  to build or fails its tests, and fails as go test does when ./...
  matches no package.
- A package whose test files are all for other platforms has no binary
  after a failed set or with gccgo either: the fallback builds a set of
  one, or with gccgo checks what go test -c wrote.
- The report files are made writable before the test exec, whose user a
  custom base may make not root.
- GOFLAGS: a flag in quotes is that flag, and a relative -outputdir is the
  module's directory's, as for go test, which also sets it there for a
  profile or -artifacts.
- otelgotest and the report helper build at the same time as the test
  binaries, not after them: on a fresh engine they were on the path to
  the first test.

Signed-off-by: Erik Sipsma <[email protected]>
From the second review:
- The test set's go list -e left out a package with an error and no test
  files to list, such as a test file with a malformed build constraint,
  and the module's tests passed without it. Such a package is now in the
  set, so the set's plan fails on it, unless build constraints merely
  leave out all its files.
- GoModule.test failed a module whose only test files are for another
  platform but whose library builds: discovery doesn't list every package.
  It now fails as go test ./... does only when no test binary was built
  and the module has no Go file that isn't a test's.
- With gccgo, a package whose files are all for other platforms had go
  test -c fail; it now asks go list first, with the set's template, and
  has no binary.

Signed-off-by: Erik Sipsma <[email protected]>
A parent module's source holds its nested modules' files, and a nested
module's library kept GoModule.test from failing as go test ./... does
when the parent itself has no package for the platform.

Signed-off-by: Erik Sipsma <[email protected]>
A build's first read of the scan, such as its Go version, ran the scan,
which on an engine that holds neither builds go-includes first, before
headers started gobuild's own build, the longest a per-package build
waits for. Each build call now asks for headers first, so gobuild's,
gohdr's and, for archives, write-manifest's builds start with the scan.
Finding a module, which runs no scan, doesn't wait for any of them.

Signed-off-by: Erik Sipsma <[email protected]>
A single package's binary, a set of test binaries and a collection's
binaries read the scan, through the module's main or tested packages,
before reaching a call that asks for headers: they ask for it first too.

Signed-off-by: Erik Sipsma <[email protected]>
go-includes was built from this module's source as it is, so its build
was keyed by the whole of dagger/go: every dagger/go version, even one
that leaves go-includes as it is, built it again and ran every scan
again. gohdr avoided that by writing its source's text into the recipe.

Both now build from toolSource: a new directory of the tool's Go files
that aren't tests', with its go.mod and go.sum, each keyed by its
content with File.sync. A file's content key is its bytes and its path
in the source, and a tool sits at the same path in every dagger/go, so
a version that leaves a tool as it is finds its build. gohdr's source
text no longer goes into the recipe.

toolKeyCheck replaces gohdrKeyCheck: it builds both tools from two
dagger/go sources that differ only in a file outside the tools, and
checks that an exec mounting each one runs once. With the sources used
as they are, each exec runs twice.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma force-pushed the sipsma/go-build-fixed-cost branch from 4603eed to 4beea52 Compare November 17, 2026 01:28
write-manifest was built from its git checkout as it is, and declared
for one platform (machineTools). A declaration's key counts an input
by its content key only once the input has one, and an engine gives a
git checkout its content key only after the checkout has run. The
engine that makes an engine image's cache declares write-manifest
before checking it out, so the image ships the build keyed by the
checkout's recipe, with the checkout itself, by then keyed by its
content. A fresh engine of the image finds that checkout and keys the
build by the checkout's content instead, so on another platform, where
the build's recipe differs and only that key can match, it misses and
builds write-manifest again for both machines: an arm64 image made on
amd64 did, on its first Go module call. An engine of the image's own
platform finds the build by its recipe.

write-manifest now builds from toolSource, as gohdr and go-includes do:
the manifest package's and its main package's Go files that aren't
tests', and its go.mod, each keyed by its content before the build is
declared, on every engine. The checkout is still what a cache that
holds the build holds, so a fresh engine reads the files from it.

Signed-off-by: Erik Sipsma <[email protected]>
sipsma added a commit to dagger/dagger that referenced this pull request Nov 17, 2026
…yed by content

Move every dagger/go pin, the built-in Go SDK's included, to dagger/go#104's
head 4beea52, and go-sdk to a commit on go-sdk's main that names it. That
dagger/go starts building its tools, gobuild's own build, gohdr,
write-manifest and pack, together, where it built them in turn; gohdr and
write-manifest come from its gomod module, and go-includes and gohdr are
keyed by their own files' content, so a pin move that leaves them unchanged
rebuilds neither.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma marked this pull request as ready for review November 17, 2026 03:14
sipsma added a commit to dagger/dagger that referenced this pull request Nov 17, 2026
Move every dagger/go pin, the built-in Go SDK's included, to dagger/go#104's
head f1d5c0b, and go-sdk to a commit on go-sdk's main that names it. That
dagger/go keys write-manifest's build by its files' contents, as it keys
go-includes and gohdr, so an engine finds the build in an image whose
entries were made on another platform instead of building it again.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma merged commit 79a92be into main Nov 17, 2026
107 checks passed
sipsma added a commit to dagger/go-sdk that referenced this pull request Nov 17, 2026
Move daggerGo to dagger/go main's 79a92be, the merge of dagger/go#104,
which takes the repeated work out of a module's first per-package build.
The calls generated entrypoints make are unchanged. Dagger's built-in Go
SDK moves to the same commit, since the engine image's build requires the
two to match.

Signed-off-by: Erik Sipsma <[email protected]>
sipsma added a commit to dagger/dagger that referenced this pull request Nov 17, 2026
Move every dagger/go pin, the built-in Go SDK's included, to dagger/go#104's
head 3fe256c, and go-sdk to a commit on go-sdk's main that names it. That
dagger/go keeps the go command's error when a plan or build fails, such as
"missing go.sum entry", and adds a failed exec's stderr to gobuild's
errors, where it gave only the last lines of its steps or a bare exit code.

Signed-off-by: Erik Sipsma <[email protected]>
sipsma added a commit to dagger/dagger that referenced this pull request Nov 17, 2026
…yed by content

Move every dagger/go pin, the built-in Go SDK's included, to dagger/go#104's
head 4beea52, and go-sdk to a commit on go-sdk's main that names it. That
dagger/go starts building its tools, gobuild's own build, gohdr,
write-manifest and pack, together, where it built them in turn; gohdr and
write-manifest come from its gomod module, and go-includes and gohdr are
keyed by their own files' content, so a pin move that leaves them unchanged
rebuilds neither.

Signed-off-by: Erik Sipsma <[email protected]>
sipsma added a commit to dagger/dagger that referenced this pull request Nov 17, 2026
Move every dagger/go pin, the built-in Go SDK's included, to dagger/go#104's
head f1d5c0b, and go-sdk to a commit on go-sdk's main that names it. That
dagger/go keys write-manifest's build by its files' contents, as it keys
go-includes and gohdr, so an engine finds the build in an image whose
entries were made on another platform instead of building it again.

Signed-off-by: Erik Sipsma <[email protected]>
sipsma added a commit to dagger/dagger that referenced this pull request Nov 17, 2026
Move every dagger/go pin, the built-in Go SDK's included, to dagger/go
main's 79a92be, the merge of dagger/go#104, whose tree is that PR's last
head; and go-sdk to its main with daggerGo naming the same commit. That
go-sdk main also brings its #109: a module's local clients are addressed
by their path from the module.

Signed-off-by: Erik Sipsma <[email protected]>
sipsma added a commit to dagger/dagger that referenced this pull request Nov 17, 2026
Move every dagger/go pin, the built-in Go SDK's included, to dagger/go#104's
head 3fe256c, and go-sdk to a commit on go-sdk's main that names it. That
dagger/go keeps the go command's error when a plan or build fails, such as
"missing go.sum entry", and adds a failed exec's stderr to gobuild's
errors, where it gave only the last lines of its steps or a bare exit code.

Signed-off-by: Erik Sipsma <[email protected]>
sipsma added a commit to dagger/dagger that referenced this pull request Nov 17, 2026
…yed by content

Move every dagger/go pin, the built-in Go SDK's included, to dagger/go#104's
head 4beea52, and go-sdk to a commit on go-sdk's main that names it. That
dagger/go starts building its tools, gobuild's own build, gohdr,
write-manifest and pack, together, where it built them in turn; gohdr and
write-manifest come from its gomod module, and go-includes and gohdr are
keyed by their own files' content, so a pin move that leaves them unchanged
rebuilds neither.

Signed-off-by: Erik Sipsma <[email protected]>
sipsma added a commit to dagger/dagger that referenced this pull request Nov 17, 2026
Move every dagger/go pin, the built-in Go SDK's included, to dagger/go#104's
head f1d5c0b, and go-sdk to a commit on go-sdk's main that names it. That
dagger/go keys write-manifest's build by its files' contents, as it keys
go-includes and gohdr, so an engine finds the build in an image whose
entries were made on another platform instead of building it again.

Signed-off-by: Erik Sipsma <[email protected]>
sipsma added a commit to dagger/dagger that referenced this pull request Nov 17, 2026
Move every dagger/go pin, the built-in Go SDK's included, to dagger/go
main's 79a92be, the merge of dagger/go#104, whose tree is that PR's last
head; and go-sdk to its main with daggerGo naming the same commit. That
go-sdk main also brings its #109: a module's local clients are addressed
by their path from the module.

Signed-off-by: Erik Sipsma <[email protected]>
sipsma added a commit to dagger/dagger that referenced this pull request Nov 17, 2026
Move every dagger/go pin, the built-in Go SDK's included, to dagger/go#104's
head 3fe256c, and go-sdk to a commit on go-sdk's main that names it. That
dagger/go keeps the go command's error when a plan or build fails, such as
"missing go.sum entry", and adds a failed exec's stderr to gobuild's
errors, where it gave only the last lines of its steps or a bare exit code.

Signed-off-by: Erik Sipsma <[email protected]>
sipsma added a commit to dagger/dagger that referenced this pull request Nov 17, 2026
…yed by content

Move every dagger/go pin, the built-in Go SDK's included, to dagger/go#104's
head 4beea52, and go-sdk to a commit on go-sdk's main that names it. That
dagger/go starts building its tools, gobuild's own build, gohdr,
write-manifest and pack, together, where it built them in turn; gohdr and
write-manifest come from its gomod module, and go-includes and gohdr are
keyed by their own files' content, so a pin move that leaves them unchanged
rebuilds neither.

Signed-off-by: Erik Sipsma <[email protected]>
sipsma added a commit to dagger/dagger that referenced this pull request Nov 17, 2026
Move every dagger/go pin, the built-in Go SDK's included, to dagger/go#104's
head f1d5c0b, and go-sdk to a commit on go-sdk's main that names it. That
dagger/go keys write-manifest's build by its files' contents, as it keys
go-includes and gohdr, so an engine finds the build in an image whose
entries were made on another platform instead of building it again.

Signed-off-by: Erik Sipsma <[email protected]>
sipsma added a commit to dagger/dagger that referenced this pull request Nov 17, 2026
Move every dagger/go pin, the built-in Go SDK's included, to dagger/go
main's 79a92be, the merge of dagger/go#104, whose tree is that PR's last
head; and go-sdk to its main with daggerGo naming the same commit. That
go-sdk main also brings its #109: a module's local clients are addressed
by their path from the module.

Signed-off-by: Erik Sipsma <[email protected]>
dagger-ci pushed a commit to dagger/dagger-go-sdk that referenced this pull request Nov 18, 2026
Move every dagger/go pin, the built-in Go SDK's included, to dagger/go#104's
head 3fe256c, and go-sdk to a commit on go-sdk's main that names it. That
dagger/go keeps the go command's error when a plan or build fails, such as
"missing go.sum entry", and adds a failed exec's stderr to gobuild's
errors, where it gave only the last lines of its steps or a bare exit code.

Signed-off-by: Erik Sipsma <[email protected]>
dagger-ci pushed a commit to dagger/dagger-go-sdk that referenced this pull request Nov 18, 2026
…yed by content

Move every dagger/go pin, the built-in Go SDK's included, to dagger/go#104's
head 4beea52, and go-sdk to a commit on go-sdk's main that names it. That
dagger/go starts building its tools, gobuild's own build, gohdr,
write-manifest and pack, together, where it built them in turn; gohdr and
write-manifest come from its gomod module, and go-includes and gohdr are
keyed by their own files' content, so a pin move that leaves them unchanged
rebuilds neither.

Signed-off-by: Erik Sipsma <[email protected]>
dagger-ci pushed a commit to dagger/dagger-go-sdk that referenced this pull request Nov 18, 2026
Move every dagger/go pin, the built-in Go SDK's included, to dagger/go#104's
head f1d5c0b, and go-sdk to a commit on go-sdk's main that names it. That
dagger/go keys write-manifest's build by its files' contents, as it keys
go-includes and gohdr, so an engine finds the build in an image whose
entries were made on another platform instead of building it again.

Signed-off-by: Erik Sipsma <[email protected]>
dagger-ci pushed a commit to dagger/dagger-go-sdk that referenced this pull request Nov 18, 2026
Move every dagger/go pin, the built-in Go SDK's included, to dagger/go
main's 79a92be, the merge of dagger/go#104, whose tree is that PR's last
head; and go-sdk to its main with daggerGo naming the same commit. That
go-sdk main also brings its #109: a module's local clients are addressed
by their path from the module.

Signed-off-by: Erik Sipsma <[email protected]>
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