Skip to content

Go modules in depth: vendoring, workspaces, and reproducible builds

Operate Go modules in production — go.mod hygiene, vendoring choices, GOFLAGS for CI, workspaces for multi-module repos, and the proxy/sumdb story.

Go modules look simple on the surface — a go.mod file, a go.sum, and you are done — but the operational choices (vendoring policy, proxy posture, workspace use, replace directives, version pinning) decide whether a build five months from now produces the same binary. This article walks an operator through what each piece does, what to commit, and what to wire up in CI for a real Go service.

How to verify

Confirm the toolchain, inspect the module graph, and check that the proxy and sum database are answering:

go version
go env GOPROXY GOSUMDB GOFLAGS GOMODCACHE
go list -m all | head
go mod graph | head
go mod verify
GOFLAGS=-mod=readonly go build ./...

go mod verify checks every cached module against go.sum. -mod=readonly fails the build if go.mod would be modified — that is the CI guarantee that nobody added a dependency without committing it.

What’s happening

A Go module is a tree of packages versioned together by a go.mod file at its root. The toolchain reads go.mod to find the module path and required dependencies, then resolves the dependency graph using Minimal Version Selection (MVS): pick the highest version that any module in the graph requires, never higher. go.sum records the hash of every module + every go.mod ever loaded, so future builds can detect tampering.

The cache lives in $GOMODCACHE (default $GOPATH/pkg/mod). When a build needs a module that is not cached, the toolchain asks $GOPROXY (default https://proxy.golang.org,direct) for the zip and the go.mod. The transparency layer $GOSUMDB (default sum.golang.org) signs hashes the proxy returns so the proxy itself cannot forge a module. Inside an enterprise that does not allow the public proxy, you set GOPROXY=https://proxy.internal/,direct and GONOSUMCHECK=* or run a mirror like Athens.

Vendoring copies every dependency into vendor/ at the project root. With vendor/ present and consistent, the toolchain builds offline from it and ignores the cache. Workspaces (go.work) glue multiple modules in one repo together for local development — they do not affect builds outside the workspace.

The procedure

  1. Initialize the module with a stable path.

    go mod init github.com/example/billing

    The module path is the canonical import path. Renaming it later is painful — pick the production path on day one.

  2. Pin the Go toolchain explicitly. Modern go.mod carries a go 1.22.5 line and an optional toolchain go1.22.5. Both should match the version your CI builds with:

    module github.com/example/billing
    
    go 1.22.5
    toolchain go1.22.5
    
    require (
        github.com/jackc/pgx/v5 v5.6.0
        go.opentelemetry.io/otel v1.28.0
    )
  3. Set CI environment variables. A .env file consumed by your pipeline:

    export GOFLAGS='-mod=readonly -trimpath'
    export GOPROXY=https://proxy.internal/,https://proxy.golang.org,direct
    export GOSUMDB=sum.golang.org
    export GOMODCACHE=/srv/cache/go-mod
    export CGO_ENABLED=0

    -trimpath strips your build host’s filesystem prefix from the binary — required for reproducibility.

  4. Decide on vendoring. Two camps. Commit vendor/ if you need offline builds, deterministic CI without proxy access, or supply-chain review at PR time. Skip it if you trust the proxy and want a smaller repo. To vendor:

    go mod vendor
    git add vendor go.mod go.sum

    Then build with GOFLAGS=-mod=vendor. The toolchain will refuse if vendor/ and go.mod disagree.

  5. Use workspaces for multi-module repos. A monorepo with service-a/, service-b/, and a shared pkg/auth/ module:

    go work init ./service-a ./service-b ./pkg/auth
    git add go.work

    Commit go.work if every developer wants the same view; .gitignore it if individual developers maintain their own workspace overlays. We commit it.

  6. Replace directives sparingly. A replace line forces the build to use a specific path or version instead of what require lists:

    replace github.com/upstream/lib => github.com/example/lib-fork v0.0.0-20260601000000-abc1234

    Use them for emergency forks only. A replace makes your module non-importable as a dependency, because consumers see the upstream version but get the fork — undocumented behavior.

  7. Tighten supply-chain checks. A separate CI job runs:

    go mod tidy -diff       # fail if go.mod/go.sum would change
    govulncheck ./...       # govulndb advisory check
    go list -json -m all | nancy sleuth   # OSS Index advisory check

    go mod tidy -diff is the linter — block PRs that add unused deps or skip a tidy.

Operational notes

  • go.sum is not optional. Removing it skips the integrity check. If go.sum is missing on a fresh clone, the toolchain refuses to build in readonly mode.
  • GOPROXY=off disables network fetches entirely. Useful for air-gapped CI — but every required module must already live in vendor/ or the cache.
  • The proxy caches forever; tags do not. If you force-push a tag (don’t), the proxy will keep serving the original commit hash until you rotate the version.
  • go env -w writes to ~/.config/go/env. Operators sometimes forget that and wonder why CI behaves differently from the laptop. The CI environment must set vars explicitly.
  • MVS does not bump to the latest patch. You must run go get -u=patch ./... (patch-only) or go get -u ./... (minor) to refresh — and re-run tidy.

Stack Harbor runs Go services for clients with reproducible builds wired through a private module proxy, go.work committed in monorepos, and govulncheck blocking on advisories at PR time. See how that fits into our managed operations practice.