Skip to content

proposal: cmd/go: 'go get', 'go mod tidy' should update the vendor directory when present #45161

Open
@jayconrod

Description

@jayconrod

This is basically the same issue as #29058, but the default behavior has changed, so it may be worth reopening the discussion.

Since 1.14, the go command may use the vendor directory by default (without the -mod=vendor flag) if the go version in go.mod is 1.14 or higher and vendor/modules.txt is present. The go command reports an error if go.mod and vendor/modules.txt are out of sync.

Two commands primarily change go.mod: go mod tidy and go get. When these commands make changes, they break vendor builds until go mod vendor is run again.

We've avoided rebuilding the vendor directory implicitly, since the go command currently deletes the entire folder, loads the import graph, and recreates it from scratch. That may cause developers to lose local changes (like a patch being prepared for a dependency).

I propose some new behavior:

  • We need a mechanism to detect local changes to vendor. That probably means comparing the contents of each package directory to the corresponding directory in the module cache. That would help us implement cmd/go: allow verifying vendored code #27348.
  • go get would add or update packages in the vendor directory automatically if it's present. Only packages needed to build the packages named on the command line would be affected.
  • go mod tidy would implicitly rebuild the vendor directory. go mod vendor would be a no-op after go mod tidy.
  • Any command that modifies the vendor directory would report an error if local changes would be overwritten. We might want a flag to force overwrite, or the developer could run git checkout or rm -rf vendor first.
  • No change to go build and other commands. -mod=readonly and -mod=mod would continue to ignore the vendor directory.

cc @bcmills @FiloSottile @matloob

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions