Skip to content

cmd/go: toolchain selection does not consider the contents of alternate go.mod given to -modfile flag #75440

Open

Description

@ChrisHines

Go version

1.24.7

Output of go env in your module/workspace:

set AR=ar
set CC=gcc
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_ENABLED=0
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set CXX=g++
set GCCGO=gccgo
set GO111MODULE=
set GOAMD64=v1
set GOARCH=amd64
set GOAUTH=netrc
set GOBIN=
set GOCACHE=C:\Users\Chris\AppData\Local\go-build
set GOCACHEPROG=
set GODEBUG=toolchaintrace=1
set GOENV=C:\Users\Chris\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFIPS140=off
set GOFLAGS=
set GOGCCFLAGS=-m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\Chris\AppData\Local\Temp\go-build2159939572=/tmp/go-build -gno-record-gcc-switches
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMOD=C:\Users\Chris\proj\continuous-music\go.mod
set GOMODCACHE=C:\Users\Chris\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\Chris\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Users\Chris\go\pkg\mod\golang.org\toolchain@v0.0.1-go1.24.7.windows-amd64
set GOSUMDB=sum.golang.org
set GOTELEMETRY=on
set GOTELEMETRYDIR=C:\Users\Chris\AppData\Roaming\go\telemetry
set GOTMPDIR=
set GOTOOLCHAIN=go1.24.7+auto
set GOTOOLDIR=C:\Users\Chris\go\pkg\mod\golang.org\toolchain@v0.0.1-go1.24.7.windows-amd64\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.24.7
set GOWORK=
set PKG_CONFIG=pkg-config

What did you do?

I want to check if the new go vet checks introduced in Go 1.25 find any problems in my code before I upgrade. I would like to perform this check without modifying any version controlled files. Ideally I could run go vet -go=1.25.0 ./... for each of my modules. Since that is not a supported feature at this time I am trying to write a tool to simulate that feature.

The tool attempts to do that by:

  1. Copying the go.mod file to a temporary directory.
  2. Running go get go@1.25 in that directory.
  3. Running go vet -modfile=<tempdir>\go.mod ./...

What did you see happen?

The first two steps work as expected, but the third step fails with the following output. Note I have GODEBUG=toolchaintrace=1 while trying to debug the problem.

go: default toolchain set to go1.24.7 from GOTOOLCHAIN=go1.24.7+auto
go: using go1.24.7 toolchain from cache located at C:\Users\Chris\go\pkg\mod\golang.org\toolchain@v0.0.1-go1.24.7.windows-amd64\bin\go
go: \tmp\go.mod requires go >= 1.25.1 (running go 1.24.7; GOTOOLCHAIN=go1.24.7+auto)

What did you expect to see?

I expected the +auto suffix of the GOTOOLCHAIN setting to allow automatically switching to the go1.25.1 toolchain required by the alternate go.mod file provided to the -modfile flag and the new version of go vet to scan the module for problems.

Activity

  1. added
    BugReportIssues describing a possible bug in the Go implementation.
    on Sep 12, 2025
  2. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Sep 15, 2025
  3. mrkfrmn commented on Sep 15, 2025

    @mrkfrmn
    Contributor

    cc @golang/command-line

  4. matloob commented on Oct 7, 2025

    @matloob
    Contributor

    We probably want to allow a toolchain switch in the middle of the execution of the go command if -modfile is set? I'd need to think about it some more to make sure that makes sense. But I think it would be too complicated to have the toolchain selection logic look at -modfile.

  5. self-assigned this
    on Oct 7, 2025
  6. adonovan commented on Feb 9, 2026

    @adonovan
    Member

    Is the actual problem "I can't test new go vet checks before the release"? If so then we should retitle this issue more descriptively without prescribing one specific technical approach to solving it.

  7. thepudds commented on Feb 9, 2026

    @thepudds
    Member

    Is the actual problem "I can't test new go vet checks before the release"?

    Hi @adonovan, that was one of the key topics over in #57063. E.g., see the #57063 (comment) comment by @ChrisHines there:

    Ideally I'd like to run something like the following to get the list of problems to fix before upgrading and it wouldn't modify any files in the module.

    GOTOOLCHAIN=govN+1 go vet -go=vN+1
    

    Plus a ~couple of other related comments there...

    (Putting my cards on the table, I'd love to see some conclusion to #57063, and maybe "how do I test new go vet checks" is somewhat naturally part of that, though of course free feel to do issue management / de-dup however you think best. 😅)

  8. adonovan commented on Feb 9, 2026

    @adonovan
    Member

    Better testing infrastructure for analyzers (from the micro level of "did applying this fix break the build?" to "what are the population statistics of this checker on the module mirror corpus of open-source Go code?") are on our roadmap for the next year.

  9. ChrisHines commented on Feb 10, 2026

    @ChrisHines
    ContributorAuthor

    Is the actual problem "I can't test new go vet checks before the release"? If so then we should retitle this issue more descriptively without prescribing one specific technical approach to solving it.

    Maybe, or maybe that should be its own issue, which I haven't filed yet.

    I explained my use cases #57063. I talked with @dmitshur about adding a -go flag to go vet at the GopherCon 2025 contributor summit. He suggested that I write an external tool to prototype the desired behavior as a way to demonstrate the value.

    My idea for how to implement that tool (without forking the whole go command) as described in the top post is what led to this issue.

    This issue is about the apparent discrepancy between the docs for the -modfile flag and actual behavior. That seems like an issue regardless of the decision about how to test new go vet checks before upgrading, which I think I have explained well enough in #57063.

  10. removed their assignment
    on Mar 3, 2026
  11. added this to the Go1.27 milestone on Mar 17, 2026
  12. matloob commented on Jun 26, 2026

    @matloob
    Contributor

    I've put together a CL (https://go-review.googlesource.com/c/go/+/794900), but the more I think about it, the more I think we shouldn't do this (and that we should try to remove the -modfile flag altogther). I think the CL itself is a case against making the chonge.

    The modfile flag is really weird- in the case of vet we don't even parse the flags before the command is invoked because it has custom parsing logic, so it's definitely not available when we do toolchain selection.

  13. gopherbot commented on Jun 26, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/794900 mentions this issue: cmd/go: support switching to toolchains from -modfile in vet

  14. modified the milestones: Go1.27, Backlog on Jun 26, 2026
  15. removed their assignment
    on Jun 26, 2026
  16. ChrisHines commented on Jun 26, 2026

    @ChrisHines
    ContributorAuthor

    I would not object to removing the -modfile flag. The experiment that led to this issue is the only time I've ever tried to directly use it. My understanding is that one of the original motivations for -modfile was for gopls to use it. I don't know if it is still needed for that purpose. I don't know what other uses exist in the ecosystem.

    Having said that, it is surprising that go vet didn't already support -modfile. Ergonomically go vet is just like any other subcommand of the go command. There is no obvious reason why it should be different from go build or go test. Furthermore, the module reference docs for -modfile are clearly part of the "Build commands" section in which go vet is explicitly listed. Quoted below:

    Build commands

    All commands that load information about packages are module-aware. This includes:

    • go build
    • go fix
    • go generate
    • go install
    • go list
    • go run
    • go test
    • go vet

    When run in module-aware mode, these commands use go.mod files to interpret import paths listed on the command line or written in Go source files. These commands accept the following flags, common to all module commands.

    [snip]

    • The -modfile=file.mod flag instructs the go command to read (and possibly write) an alternate file instead of go.mod in the module root directory. The file’s name must end with .mod. A file named go.mod must still be present in order to determine the module root directory, but it is not accessed. When -modfile is specified, an alternate go.sum file is also used: its path is derived from the -modfile flag by trimming the .mod extension and appending .sum.

    So the current situation is that either the documentation is wrong or go vet has a bug.

  17. adonovan commented on Jun 26, 2026

    @adonovan
    Member

    I would not object to removing the -modfile flag. The experiment that led to this issue is the only time I've ever tried to directly use it. My understanding is that one of the original motivations for -modfile was for gopls to use it. I don't know if it is still needed for that purpose. I don't know what other uses exist in the ecosystem.

    It is used by gopls, in two places:

    • for go mod tidy, so that the tidied go.mod file can be read without mutating the actual source;
    • for go get, for similar reasons.

    Overlays work great when you only need to provide alternate go.mod files as inputs, but -modfile is currently necessary when the updated file is an output. If the two commands above were changed so that they, like gofmt, printed the updated file to stdout, then we could stop using that flag and just use overlays.

  18. matloob commented on Jun 26, 2026

    @matloob
    Contributor

    @ChrisHines I totally agree. If we don't get the CL in, we should definitely update the documentation. Ideally we can make a decision quickly. And as @adonovan mentions, we should have a plan to provide an alternative for gopls available before we remove the modfile flag, should we choose to do so. (See #80179 which I just filed and which I should have linked to here)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

BugReportIssues describing a possible bug in the Go implementation.GoCommandcmd/goNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions