TExcelTAD
Home › Go Modules in Depth › Vendoring

Vendoring

Vendoring copies the source of every dependency into a vendor/ directory inside your module, so the build needs nothing but the repository: no proxy, no module cache, no network. Go modules made vendoring optional (a module proxy plus go.sum already gives repeatable builds), but some teams still need it: air-gapped build machines, auditors who want to review every line that ships, or a policy that a release must build from one source tarball.

go mod vendor

bash · try it yourself
mkdir app && cd app
go mod init example.com/app 2>/dev/null
cat > main.go <<'SRC'
package main

import (
	"fmt"

	"rsc.io/quote"
)

func main() { fmt.Println(quote.Go()) }
SRC
go mod tidy 2>/dev/null
go mod vendor
tree -d vendor
cat vendor/modules.txt
echo "--- build with an empty module cache and no proxy"
go clean -modcache
GOPROXY=off go build -o app . && ./app
vendor
├── golang.org
│   └── x
│       └── text
│           ├── internal
│           │   └── tag
│           └── language
└── rsc.io
    ├── quote
    └── sampler

10 directories
# golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
## explicit
golang.org/x/text/internal/tag
golang.org/x/text/language
# rsc.io/quote v1.5.2
## explicit
rsc.io/quote
# rsc.io/sampler v1.3.0
## explicit
rsc.io/sampler
--- build with an empty module cache and no proxy
Don't communicate by sharing memory, share memory by communicating.

Points to notice:

  • Only the packages the build needs are copied, not whole modules: golang.org/x/text contributes two packages out of dozens. Test files of dependencies are left out too.
  • vendor/modules.txt records each module's version (# path version), whether go.mod requires it (## explicit), and the packages copied. The go command uses it to check that vendor/ matches go.mod.
  • The build worked with GOPROXY=off and an empty cache: when a vendor/ directory exists and the go line is 1.14 or later, builds use it automatically (-mod=vendor is the default).

Keeping vendor/ in sync

vendor/ is a copy, and copies go stale. The go command refuses to build when go.mod and vendor/modules.txt disagree:

bash · try it yourself
mkdir app && cd app
go mod init example.com/app 2>/dev/null
printf 'package main\n\nimport (\n\t"fmt"\n\n\t"rsc.io/quote"\n)\n\nfunc main() { fmt.Println(quote.Go()) }\n' > main.go
go get rsc.io/quote@v1.5.1 2>/dev/null
go mod tidy 2>/dev/null && go mod vendor
go get rsc.io/quote@v1.5.2
go build . ; echo "exit=$?"
go build -mod=mod -o app . && echo "built from the module cache with -mod=mod"
go mod vendor
go build . && echo "built from vendor/ again"
grep '^# rsc.io/quote' vendor/modules.txt
go: downloading rsc.io/quote v1.5.2
go: upgraded rsc.io/quote v1.5.1 => v1.5.2
go: inconsistent vendoring in /home/learner/app:
	rsc.io/quote@v1.5.2: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt
	rsc.io/quote@v1.5.1: is marked as explicit in vendor/modules.txt, but not explicitly required in go.mod

	To ignore the vendor directory, use -mod=readonly or -mod=mod.
	To sync the vendor directory, run:
		go mod vendor
exit=1
built from the module cache with -mod=mod
built from vendor/ again
# rsc.io/quote v1.5.2

So the workflow is: change requirements with go get/go mod tidy, then go mod vendor, then commit go.mod, go.sum and vendor/ together. In CI, go mod vendor && git diff --exit-code vendor/ catches anyone who forgot.

-mod= Meaning
vendor build from vendor/ (default when it exists)
readonly use the module cache; fail if go.mod needs changes (default otherwise)
mod use the module cache and update go.mod if needed

Trade-offs

For vendoring Against
builds without network or proxy the repository grows (sometimes by hundreds of MB), diffs get noisy
every dependency line is reviewable in code review easy to forget go mod vendor; stale copies
survives upstream deletions without a caching proxy go.sum + a caching proxy (Athens, Artifactory) gives the same safety for most teams

Never edit files in vendor/ by hand to patch a dependency: the next go mod vendor silently reverts the change. Patch with replace to a fork, then re-vendor. For workspaces, the equivalent is go work vendor.

Test yourself

When does go build use the vendor directory automatically?

  1. Never; you must pass -mod=vendor
  2. When vendor/ exists and go.mod's go line is 1.14 or later
  3. Only when GOPROXY=off
  4. Only in workspaces

What does go mod vendor copy?

  1. Every file of every module in go.sum
  2. The packages needed to build the main module's packages (and their tests), without dependencies' tests
  3. Only direct dependencies
  4. The module cache

After go get upgrades a module, the build fails with "inconsistent vendoring". What is the fix?

  1. Delete go.sum
  2. Run go mod vendor and commit the result
  3. Edit vendor/modules.txt by hand
  4. Set GOFLAGS=-mod=mod permanently