Description
Abstract
This is a proposal to enable the use of the main module's vendor
directory, by default, in module mode. This proposal replaces #30240 and #29058, and subsumes #27227.
Background
The Go 1.5 release included support for a vendor
subdirectory for each package subtree within GOPATH
. A vendor
directory, if present, implicitly changed the interpretation of import
statements within that subtree to always refer to packages within the vendor
directory if present.
In module mode, the meaning of each import is instead determined by the dependencies of the main module used for the build. However, from the first release of module mode (in Go 1.11), the go
command has included a go mod vendor
subcommand, which populates a vendor
directory at the root of the main module with source code from those dependencies; and a -mod=vendor
flag, which instructs the go
command to use that source code for building.
In module mode, unlike GOPATH
mode, the vendor
subdirectories of other packages and modules are ignored — only the vendor
directory at the root of the main module is used. This gives the author of the main module complete control over their dependencies via require
and replace
directives.
Go users have built a variety of workflows around vendored dependencies for a variety of use-cases — including self-contained builds, language-agnostic code review, and ephemeral patches — and since the launch of module mode in Go 1.11 they have made it clear that those workflows remain important in module mode.
However, vendoring in module mode today has a few rough edges:
-
A maintainer who introduces a new
import
statement may easily and accidentally introduce skew between the versions in thego.mod
file and those in thevendor
directory (cmd/go: the vendor/ folder should be kept up to date if present #29058).- In CL 174528, we added a simple check for this skew in the
std
andcmd
modules in the standard library, but it relies on some assumptions about the dependencies ofstd
andcmd
that do not hold in general.
- In CL 174528, we added a simple check for this skew in the
-
The maintainers of projects that use vendoring expect the
vendor
directory to be used by default (cmd/go: should default to use mod=vendor flag if the vendor folder exists #27227), but today in module mode that requires users to set-mod=vendor
, either explicitly on the command line or in aGOFLAGS
variable. For users working on multiple modules with different vendoring strategies, setting the variable may require repeated intervention over the course of the day.
This proposal attempts to address those rough edges.
Proposal
The concrete proposal is posted in a comment below. (Any updates will be linked from here.)
Latest update: #33848 (comment)
(CC @jayconrod @ianthehat)