Skip to content

runtime: deprecate func GOROOT #51473

Closed
Closed

Description

@rittneje

The runtime.GOROOT() function is currently implemented as follows:

  1. If the GOROOT environment variable is set, return its value.
  2. Else, return the value that was burned into the binary at compile time.

Unfortunately, this implementation cannot be relied upon to have any semantic meaning in general.

If the intent is to know what GOROOT the binary was compiled with, then consulting the GOROOT environment variable is wrong. Instead, the burned in value should be added to debug.BuildInfo.

If the intent is to know the GOROOT on the current machine, then consulting the burned in value is wrong, as it cannot handle the following (non-exhaustive) scenarios:

  1. The binary may have been compiled on another machine, with a totally irrelevant GOROOT.
  2. The go installation may have been moved since the binary was compiled.
  3. The PATH environment variable may have changed since the binary was compiled.
  4. The current machine might not even have a go installation.

Instead, the go env GOROOT command should be used. In particular, this change needs to be made for go/build.Default to function properly in all cases.

Activity

  1. added this to the Proposal milestone on Mar 4, 2022
  2. mvdan commented on Mar 4, 2022

    @mvdan
    Member

    I think this is an interesting proposal, and I agree with the current confusion causing problems at times.

    I assume that, if a program wants to try both approaches, they could have code that first tries one approach (like go env GOROOT), and then falls back to the other (like debug.BuildInfo).

  3. mvdan commented on Mar 4, 2022

    @mvdan
    Member

    Instead, the go env GOROOT command should be used. In particular, this change needs to be made for go/build.Default to function properly in all cases.

    I fully agree with your suggestion to use go env GOROOT, but I'm worried about changing go/build.Default.GOROOT. Note that Default is filled at init time, so it has a noticeable flat cost added to any program that ends up importing the package. Most of that cost currently comes from many calls to os.Getenv; adding an execution of go env would make it significantly slower, making many Go programs take a few milliseconds longer to start.

    So when it comes to go/build, I think we should either keep it as-is with a warning in its documentation, or fully deprecate the Context.GOROOT field, instead pointing people to better alternatives like go env GOROOT to just query that one field, or to https://pkg.go.dev/golang.org/x/tools/go/packages for a whole-sale replacement of the Context.Import API.

  4. rittneje commented on Mar 4, 2022

    @rittneje
    ContributorAuthor

    I fully agree with your suggestion to use go env GOROOT, but I'm worried about changing go/build.Default.GOROOT. Note that Default is filled at init time, so it has a noticeable flat cost added to any program that ends up importing the package

    One possible solution would be to add a private flag to go/build.Context, such as isDefault bool. If true, then it won't set any environment variables when calling go list, since at best this accomplishes nothing, and at worst causes bugs.

    go/src/go/build/build.go

    Lines 1182 to 1188 in 81767e2

    cmd.Env = append(os.Environ(),
    "GOOS="+ctxt.GOOS,
    "GOARCH="+ctxt.GOARCH,
    "GOROOT="+ctxt.GOROOT,
    "GOPATH="+ctxt.GOPATH,
    "CGO_ENABLED="+cgo,
    )

  5. mvdan commented on Mar 4, 2022

    @mvdan
    Member

    Would that help with the added init cost, though, if its init func may still call go list?

  6. rittneje commented on Mar 4, 2022

    @rittneje
    ContributorAuthor

    @mvdan I'm not suggesting that the init func for go/build call go list. I'm saying that when go/build calls go list within importGo (which already happens today), it should not attempt to set any of the environment variables in the case of build.Default.

    Alternatively, at the top of importGo, it should call go env GOROOT rather than relying on the (possibly broken) value of build.Default.GOROOT.

  7. mvdan commented on Mar 4, 2022

    @mvdan
    Member

    Ah, I see. I was thinking mainly of end users directly reading build.Default.GOROOT. Presumably we need to do something about that one way or another.

  8. bcmills commented on Mar 4, 2022

    @bcmills
    Contributor

    The one case I can think of where runtime.GOROOT() may still be helpful is for tests, where it may be easier to invoke runtime.GOROOT() than to shell out to go env GOROOT. The latter may not be accurate anyway if the go in $PATH is not the one used to compile the test. (Consider ~/go-devel/bin/go test ., where ~/go-devel is a GOROOT but is not listed in $PATH.)

    At least in principle, it is possible for the go command to ensure that GOROOT is set accurately in the test process's environment when it is run. Then again, if we do that then the test could run os.Getenv("GOROOT") just as easily as runtime.GOROOT(). 🤔

  9. rittneje commented on Mar 4, 2022

    @rittneje
    ContributorAuthor

    @bcmills I'm not sure what the use cases are for looking up GOROOT while unit testing, but one thing to keep in mind is that it is possible to compile a test binary with -c and then run it later. Consequently, it would run into the same general issues I mentioned above.

  10. bcmills commented on Mar 4, 2022

    @bcmills
    Contributor

    That's true, but if you compile a test binary with -c and then run it later, you are also responsible for replicating any conditions necessary for the test to succeed (working directory, environment, etc.).

    Even so, I suspect that most tests that depend on runtime.GOROOT() only do so via go/build (for example, because they are testing analyzers for Go source code), and fixing #51483 would make those tests more robust either way.

  11. rittneje commented on Mar 4, 2022

    @rittneje
    ContributorAuthor

    Gotcha. Actually, as an optimization, since we know that $GOROOT takes precedence if it's set, then go/build can just check the env var first before shelling out to go env GOROOT. Then maybe such test cases can mandate the env var gets set for stabler results (as in the example you mentioned).

    I guess another approach would be to have go test manipulate the PATH variable for the sub-process, by prefixing it with $(go env GOROOT)/bin. Not sure if that would be too surprising, but it does have the nice side effect of ensuring that any go commands that get spawned by the test do the more expected thing.

  12. rsc commented on Mar 16, 2022

    @rsc
    Contributor

    This proposal has been added to the active column of the proposals project
    and will now be reviewed at the weekly proposal review meetings.
    — rsc for the proposal review group

  13. rsc commented on Mar 23, 2022

    @rsc
    Contributor

    @bradfitz reports that he has uses of runtime.GOROOT in GitHub actions where built binaries want to find the toolchain that built them, so they can invoke it to do other things.

    It seems like we probably need to enumerate the uses people have for runtime.GOROOT and what they should do instead. It doesn't help to just say "Deprecated". We have to say what to use instead.

  14. bradfitz commented on Mar 23, 2022

    @bradfitz
    Contributor

    For example:

    bradfitz@tsdev:~/src/tailscale.com$ git grep runtime.GOROOT
    net/tlsdial/tlsdial_test.go:    cmd := exec.Command(filepath.Join(runtime.GOROOT(), "bin", "go"),
    tstest/archtest/qemu_test.go:                   cmd := exec.Command(filepath.Join(runtime.GOROOT(), "bin", "go"),
    tstest/integration/integration.go:      goBin := filepath.Join(runtime.GOROOT(), "bin", "go"+exe())
    version/modinfo_test.go:        goTool := filepath.Join(runtime.GOROOT(), "bin", "go"+exe())
    
  15. 63 remaining items

  16. rittneje commented on Nov 22, 2024

    @rittneje
    ContributorAuthor
  17. mvdan commented on Nov 23, 2024

    @mvdan
    Member

    For environments where go env GOROOT will fail because Go is not installed, that's fine - we can skip loading tzdata from GOROOT just like we currently do when the embedded GOROOT path does not actually exist on disk. Why is that a problem? We already have other parts of the standard library which execute go env for similar purposes.

    Personally I would prefer if we nudged everyone towards https://pkg.go.dev/time/tzdata in the longer term, but assuming that we want to preserve the current zip loading logic from GOROOT, I think we should switch it to go env GOROOT the sooner the better.

    I would not be surprised if people are currently relying on that behavior.

    I would also not be surprised by this either, but I think those users would continue to be satisfied by the switch to go env GOROOT.

  18. rittneje commented on Nov 24, 2024

    @rittneje
    ContributorAuthor

    @mvdan Today I can compile an application that calls time.LoadLocation for Windows, put it on a Windows machine, set $GOROOT, and arrange for $GOROOT/lib/time/zoneinfo.zip to exist, without any need to install a Go toolchain on that Windows machine. Therefore changing time.LoadLocation to invoke go env GOROOT would be a breaking change.

  19. DeedleFake commented on Nov 24, 2024

    @DeedleFake

    Proposal for a proposal: Add runtime/env to expose go env's lookup functionality programmatically, updating go env to use the new package itself for consistency. Then just use that to find the equivalent $(go env GOROOT) in time.LoadLocation().

  20. mvdan commented on Nov 24, 2024

    @mvdan
    Member

    @rittneje fair enough, but that has nothing to do with the embedded GOROOT value. The logic could be to query the GOROOT env var first, and if empty, fall back to go env GOROOT.

    @DeedleFake that is a separate proposal so it's best to not mix the two.

  21. rittneje commented on Nov 24, 2024

    @rittneje
    ContributorAuthor

    @mvdan Let's say I have Windows machine with Go installed at C:\Go. I then use that to compile my application (without -trimpath), copy it to a second Windows machine, and arrange for C:\Go\lib\time\zoneinfo.zip to exist on the second machine. Today that will work. But using os.Getenv("GOROOT") won't work (because the second machine never set that env var), nor will running go env GOROOT (because the second machine does not have a Go installation).

    @DeedleFake This can already be achieved in practice via os.Getenv("GOROOT") followed by exec.LookPath("go"). Unfortunately, it does not address this backwards compatibility issue, because in the absence of the GOROOT env var, runtime.GOROOT() returns the burned in GOROOT, not the Go installation.

  22. mvdan commented on Nov 24, 2024

    @mvdan
    Member

    @rittneje that seems to me like a rare use case which relies on the embedded path, which is now being discouraged via this deprecation. I'm not arguing we need to break it right away, and it could be a phased approach via a GODEBUG, but I definitely think we need to stop supporting such a use case.

  23. ianlancetaylor commented on Nov 25, 2024

    @ianlancetaylor
    Contributor

    If we are going to keep checking runtime.GOROOT, then are there really that many cases where runtime.GOROOT fails but go env GOROOT succeeds?

  24. rittneje commented on Nov 25, 2024

    @rittneje
    ContributorAuthor

    @ianlancetaylor I'm not sure what you mean by "keep checking runtime.GOROOT". When $GOROOT is not set, runtime.GOROOT() and go env GOROOT answer completely different questions:

    • runtime.GOROOT() gives the Go installation that compiled the application, possibly on another machine. And if you do -trimpath, then it yields the empty string.
    • go env GOROOT gives the current Go installation on this machine, assuming it has one at all.

    In my view, as the documentation for time.LoadLocation only mentions $GOROOT and not anything about it trying to find "the current Go installation", running go env GOROOT as a fallback is not necessary. The only question is whether we need to account for the possibility that people have been relying on it falling back to the burned in GOROOT when $GOROOT is not set, even though the documentation does not mention this.

    In short, there are really three choices:

    1. Leave it the way that it is.
    2. Change time.LoadLocation to use os.Getenv("GOROOT") instead of runtime.GOROOT().
    3. Add some godebug setting to opt into or out of (2).
  25. aarzilli commented on Nov 25, 2024

    @aarzilli
    Contributor

    Using go env GOROOT instead of runtime.GOROOT has potentially surprising implications now that ~everyone has multiple toolchains installed.

  26. ianlancetaylor commented on Nov 25, 2024

    @ianlancetaylor
    Contributor

    @rittneje For your point 2, the runtime.GOROOT function will already return the environment if it is set. If the environment variable is not set, it will return the value from the tools used to build the program. If we're OK with checking the environment variable, then I think the only question is whether it is harmful to also check the value from the tools.

  27. rittneje commented on Nov 25, 2024

    @rittneje
    ContributorAuthor

    @ianlancetaylor I do not personally believe it is actively harmful for time.LoadLocation to fall back to the burned in GOROOT. I do however thing it is surprising, given that it is not documented.

    Meanwhile, if someone really wants to augment time.LoadLocation to try to find the Go installation instead of using the burned in GOROOT, that ought to be its own proposal.

  28. locked and limited conversation to collaborators on Nov 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址