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
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 . && ./appvendor
├── 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/textcontributes two packages out of dozens. Test files of dependencies are left out too. vendor/modules.txtrecords each module's version (# path version), whethergo.modrequires it (## explicit), and the packages copied. The go command uses it to check thatvendor/matchesgo.mod.- The build worked with
GOPROXY=offand an empty cache: when avendor/directory exists and thegoline is 1.14 or later, builds use it automatically (-mod=vendoris 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:
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.txtgo: 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?
- Never; you must pass -mod=vendor
- When vendor/ exists and go.mod's go line is 1.14 or later
- Only when GOPROXY=off
- Only in workspaces
What does go mod vendor copy?
- Every file of every module in go.sum
- The packages needed to build the main module's packages (and their tests), without dependencies' tests
- Only direct dependencies
- The module cache
After go get upgrades a module, the build fails with "inconsistent vendoring". What is the fix?
- Delete go.sum
- Run
go mod vendorand commit the result - Edit vendor/modules.txt by hand
- Set GOFLAGS=-mod=mod permanently