Skip to content

cmd/go: most 'go mod' subcommands should not update go.mod, go.sum #45551

Closed
@jayconrod

Description

@jayconrod

As of Go 1.16, all go mod subcommands can update go.mod and go.sum. Some commands are intended to update these files, and that shouldn't change. However, most commands may still perform an automatic update, which may be surprising. Internally, go mod subcommands run in -mod=mod mode, even though they don't have a -mod command line flag.

For some commands, updating go.mod and go.sum is expected:

  • go mod edit is supposed to edit go.mod.
  • go mod init creates go.mod and may import information from another dependency management tool.
  • go mod tidy is supposed to edit go.mod and go.sum.

For other commands, updating go.mod or go.sum may be quite unexpected. The go command should report an error if go.mod or go.sum need to be updated.

  • go mod graph is a query tool. It shouldn't have side effects.
  • go mod vendor is expected to update vendor but not go.mod.
  • go mod verify also shouldn't have side effects.
  • go mod why, same deal.

go mod download occupies an awkward middle ground. It has always been able to update go.mod (probably should not), but it may also add sums of modules in the build list to go.sum. This is usually unexpected and undesired when go mod download is invoked without arguments: users want something like go list all that populates the module cache with modules needed to build packages in the main module, not including unused dependencies of dependencies. However, we shouldn't entirely prevent go mod download from updating go.sum, since it's the best way to add a specific sum for a necessary but unused module (for disambiguating an imported package).

I'd suggest the following change in behavior for go mod download:

  • go mod download always loads the build list and reports an error if go.mod needs to be updated instead of updating it automatically.
  • go mod download only adds a hash to go.sum if the corresponding module is explicitly named on the command line (not simply matched by all or a wildcard or implied by a lack of arguments) without an explicit version or at the version in the build list.

cc @bcmills @matloob

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions