Skip to content

x/tools/go/packages: use the correct go version for the analyzed project #68495

Open
@hyangah

Description

@hyangah

When the system Go version is newer than the GOTOOLCHAIN go version, we observed the x/tools/go/packages picks up the system Go version as the go version. This can cause the package to use the incompatible go list command. @matloob found this is because x/tools/internal/gocommand.GoVersion called by x/tools/go/packages.(*golistState).getGoVersion, uses GO111MODULE=off which prevents correct go version computation depending on toolchain switch.

This bug prevents gopls (v0.15.3, v0.16.0, v0.16.1) from operating with old go versions like go1.19 if the system Go version is newer. (e.g. using -pgo flag that doesn't exist in go1.19)

This will potentially break the future gopls versions if users rely on the GOTOOLCHAIN to switch to go versions and go/packages ends up using new go list flags that do not exist in the GOTOOLCHAIN version but exist in the system Go version.

We first need to understand why GoVersion uses GO111MODULE=off.

cc @golang/tools-team @matloob @findleyr

Activity

added
ToolsThis label describes issues relating to any tools in the x/tools repository.
on Jul 17, 2024
added this to the Unreleased milestone on Jul 17, 2024
added
NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
on Jul 17, 2024
findleyr

findleyr commented on Nov 11, 2024

@findleyr
Member

This is not going to get fixed for gopls@v0.17.0.

@matloob do you understand why GoVersion uses GO111MODULE=off?

adonovan

adonovan commented on Apr 18, 2025

@adonovan
Member

do you understand why GoVersion uses GO111MODULE=off?

The actual logic dates from https://go.dev/cl/263985, but that doesn't explain why.
@matloob, do you know?

jakebailey

jakebailey commented on Apr 18, 2025

@jakebailey
Contributor

I'm not matloob (😅), but I remember reading this code a really long time ago when it was first sent, and remember it being slightly faster to invoke go list and others without module mode enabled. When I test that with hyperfine, it's barely true, maybe 0.1ms.

However, actually digging, that code actually stemmed from CL 253799 where it was previously the code for modfileFlagExists, which traces back to CL 211937, then eventually all the way to CL 212477 where it was seemingly just to deal with the temp modfile back when that was a thing. (And I guess, not in this case for perf; I was probably thining of go env being slow, which is still true by a bit.)

matloob

matloob commented on Apr 18, 2025

@matloob
Contributor

I am matloob but I think @jakebailey has a better answer than I do!

I really can't think of a good reason to set GO111MODULE=off for go version. Before we had toolchain switching/selection, go version's behavior shouldn't have changed at all depending on the value of GO111MODULE.

gopherbot

gopherbot commented on Apr 18, 2025

@gopherbot
Contributor

Change https://go.dev/cl/666715 mentions this issue: internal/gocommand: don't disable Go modules

adonovan

adonovan commented on Apr 18, 2025

@adonovan
Member

Thanks. I'll mail you a CL to remove it.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.ToolsThis label describes issues relating to any tools in the x/tools repository.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @hyangah@jakebailey@adonovan@gopherbot@cherrymui

        Issue actions

          x/tools/go/packages: use the correct go version for the analyzed project · Issue #68495 · golang/go