Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

go: error obtaining VCS status: exit status 128

Go runs git to stamp version data and git exits 128. Dubious ownership, missing .git metadata and container builds are the usual triggers.

~5 min readGo
#Go#Build#Git#Containers
A Go binary crate with a cyan VCS stamp slot and an amber git failure plate attached

Introduction & Quick Summary

text
go: error obtaining VCS status: exit status 128 Use -buildvcs=false to disable VCS stamping.

The Go toolchain runs git status --porcelain (or the equivalent for Mercurial, Bazaar, Fossil and Subversion) to stamp version information into the binary, and that command exited 128. The build then fails even though the code compiled, because stamping is treated as part of a reproducible build.

Exit 128 from git means "fatal", and the two usual reasons are:

  • detected dubious ownership in repository. The build runs as a different user than the one that owns the checkout, which is the default inside containers and on mounted volumes.
  • The directory is not a usable repository. No git binary, a shallow or broken .git, a worktree whose metadata path is missing, or a GIT_DIR pointing elsewhere.
ContextTypical triggerRecommended fix
Docker buildCOPY . . as root, build as non-root-buildvcs=false in the image
Mounted volumeHost UID differs from container UIDsafe.directory or disable stamping
CI checkoutShallow clone without tagsEnsure .git is present, then stamp
go install from a zipNo .git at allNothing to fix; it should not fail
Nested moduleGit root far above the module-buildvcs=false per module

Core Mechanics, Signatures & Reproducible Setup

go build stamps vcs.revision, vcs.time and vcs.modified into the binary, which go version -m reads back. Stamping is enabled by default when the package being built is inside a repository and the toolchain can run the VCS command. -buildvcs=auto (the default) skips stamping when the repository is not found but fails the build when the VCS command errors, which is why a broken repository is worse than no repository.

Force the same failure in a scratch directory:

bash
mkdir -p /tmp/vcs-demo && cd /tmp/vcs-demo git init -q && printf 'module demo\n\ngo 1.22\n' > go.mod printf 'package main\n\nfunc main() {}\n' > main.go chown -R root:root /tmp/vcs-demo # as another user now go build ./... # go: error obtaining VCS status: exit status 128

Confirm the underlying git failure, which the Go message hides:

bash
git -C /tmp/vcs-demo status --porcelain; echo "exit=$?" # fatal: detected dubious ownership in repository at '/tmp/vcs-demo'

Three fixes, from narrowest to broadest:

bash
# 1. Trust this specific directory for this user git config --global --add safe.directory /workspace/app # 2. Turn stamping off for the build (no version info in the binary) go build -buildvcs=false -o /out/app ./cmd/app # 3. Persist the choice for a container build ENV GOFLAGS=-buildvcs=false

Disabling stamping is the right answer for container images, where the metadata describes the build machine rather than the artifact and is usually recorded separately in the image label anyway.

Key Comparison Table

Causegit status exitVisible to userBest fix
Dubious ownership128Yes, with a hintsafe.directory
Not a repository128not a git repositoryNothing to stamp
Git not installed127exec: "git": not foundInstall git or disable
Corrupt .git/HEAD128not a valid object nameRe-clone
Missing worktree metadata128not a working treegit worktree repair
Read-only .git128Permission deniedMount writable or disable
ValueBehaviorUse case
-buildvcs=trueFail if VCS cannot be queriedRelease pipelines that require provenance
-buildvcs=autoDefault; skip only if no repoGeneral development
-buildvcs=falseNever stampContainer builds, mounted volumes
GOFLAGS=-buildvcs=falseSame, project-wideRepos that are always built in containers
GOVCSRestricts allowed VCS per module pathModule download policy, unrelated flag

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

Case 1: Multi-stage Docker builds

The usual sequence copies the source as root, then switches to a non-root user before go build:

dockerfile
FROM golang:1.22 AS build WORKDIR /src COPY go.mod go.sum ./ RUN go mod download COPY . . RUN CGO_ENABLED=0 go build -trimpath -buildvcs=false -o /out/app ./cmd/app

Adding the source as a non-root owner works too, but -buildvcs=false also removes the need for .git inside the image, which is a smaller image and a smaller secret surface.

Case 2: safe.directory in a CI runner with a shared cache

bash
git config --global --add safe.directory "$PWD" go build ./...

On a runner that builds many directories, add each one rather than *. safe.directory=* disables a real security check that protects against a repository owned by another user being treated as trusted.

Case 3: Worktrees and submodules

go build runs inside a subdirectory, so the toolchain walks up until it finds .git. In a worktree, .git is a file pointing at ../.git/worktrees/<name>; if that path was moved, git fails and Go reports exit 128 with no explanation.

bash
git worktree list git worktree repair

Case 4: GIT_DIR inherited from the environment

A shell that exported GIT_DIR for another repository makes every git call in the session look at the wrong place:

bash
env | grep -E '^GIT_(DIR|WORK_TREE|CEILING)' unset GIT_DIR GIT_WORK_TREE

This is common in scripts that wrapped git --git-dir=... and left the variable behind.

Case 5: Vendored builds and nested modules

A nested module inside a larger repository stamps the parent's revision, which is usually wrong for the artifact. For a module published from a subdirectory, disable stamping or build from a clean checkout of that module only:

bash
cd services/api && go build -buildvcs=false ./...

Case 6: Verifying what the binary actually carries

bash
go version -m ./app | grep -E 'vcs\.(revision|time|modified)'

If these lines are absent, either stamping was disabled or the build did not happen in a repository. Recording the module version there instead is often more useful than a dirty-worktree flag that is true on every developer machine.

Common Compilation/Runtime Mistakes & Version Gotchas

  • -buildvcs does not affect tests. go test compiles test binaries and can fail the same way, so put GOFLAGS=-buildvcs=false in the environment rather than adding flags to one command.
  • go install pkg@version never stamps. Building from the module cache has no repository, so this error cannot come from that path; if it appears, the module cache is inside a repository directory, which GOMODCACHE should fix.
  • Git versions before 2.35.2 lack safe.directory. Very old images fail with dubious ownership and no way to configure it, so either upgrade git or disable stamping.
  • GOFLAGS from the environment overrides the project. A CI variable that sets -buildvcs=true overrides a go.env default and reintroduces the failure; print go env GOFLAGS during the build.
  • Exit code 128 is not specific. Any fatal git error maps to it, including a full disk while writing the index. Run the same git status by hand as the build user to see the real message.
  • Reproducibility tradeoff. -trimpath -buildvcs=false yields identical binaries across machines, which is usually what container builds want; stamping gives provenance at the cost of reproducibility.

When the difference between a working developer build and a failing container build is unclear, export the environment of each with the env file viewer and compare them, paying attention to GOFLAGS, GIT_* and HOME. If the build is driven by a Compose file, the Docker Compose viewer shows the user, volume and working directory that decide ownership. For comparing two Dockerfiles where one fails, the diff tool surfaces the USER or COPY change responsible.

Primary references: go build documentation describes -buildvcs, and git's safe.directory explains the ownership check that produces the underlying exit 128.

Official references

primary documentation

The Go documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Go work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Go guides

More in Go

All Go guides

The guides filed next to this one in Go, in library order.