go icon indicating copy to clipboard operation
go copied to clipboard

proposal: extended forwards compatibility for Go

Open rsc opened this issue 3 years ago • 78 comments

Many people believe the go line in the go.mod file specifies which Go toolchain to use. This proposal would correct this widely held misunderstanding by making it reality. At the same time, the proposal would improve forward compatibility by making sure that old Go toolchains never try to build newer Go programs.

Define the “work module” as the one containing the directory where the go command is run. We sometimes call this the “main module”, but I am using “work module” in this document for clarity.

Updating the go line in the go.mod of the work module, or the go.work file in the current workspace, would change the minimum Go toolchain used to run go commands. A new toolchain line would provide finer-grained control over Go toolchain selection.

An environment variable GOTOOLCHAIN would control this new behavior. The default, GOTOOLCHAIN=auto, would use the information in go.mod. Setting GOTOOLCHAIN to something else would override the go.mod. GOTOOLCHAIN=local would force use of the locally installed toolchain, and other values would choose specific releases. For example, to test the package in the current directory with Go 1.17.2:

GOTOOLCHAIN=go1.17.2 go test

As part of this change, older Go toolchains would refuse to try to build newer Go code. The new system would arrange that normally this would not come up - the new toolchain would be used automatically. But if forced, such as when using GOTOOLCHAIN=local, the older Go toolchain would no longer assume it can build newer Go code. This in turn would make it safe to revisit and fix for loop scoping (discussion #56010).

See my talk on this topic at GopherCon for more background.

See the design document for details.

rsc avatar Nov 30 '22 17:11 rsc

Updated link to design doc: https://go.dev/design/57001-gotoolchain.

rsc avatar Nov 30 '22 17:11 rsc

I just want to note that the potential for confusion here is high. go build and go install should report whenever they invoke a different toolchain.

ianlancetaylor avatar Nov 30 '22 17:11 ianlancetaylor

I'm not sure about that. What I hope will be a common mode of usage is that you install some update-aware Go toolchain on your machine and then from that point on just edit go or toolchain lines in your go.mod in various projects and never explicitly update the locally installed Go toolchain ever again. The Go toolchain becomes a detail managed inside go.mod just like all the other inputs to your build. In that mode of usage, this reporting would print on literally every go command that gets run, which is too noisy.

rsc avatar Nov 30 '22 17:11 rsc

I think the combination of auto-upgrading and not allowing old toolchains to build new code will change the Go ecosystem to either no longer support multiple versions of Go, or to delay experimenting with new language features.

Where previously a Go module could target Go 1.21 but make use of 1.22 features in optional, build-tagged files, it can no longer do so. To be allowed to use Go 1.22 language features, the go.mod has to specify go 1.22, but this triggers auto-upgrading / build failures on Go 1.21. The module now only supports Go 1.22.

Auto-uprading makes this seem fine at first: anyone who needs Go 1.22 will get it for free, automatically. However, I don't think auto-upgrading can be assumed to be pervasive. For one, most people who hack on Go at least occasionally will be using GOTOOLCHAIN=local, pointed at a git checkout. Furthermore, I predict that some Linux distributions will patch their Go packages to disable the automatic upgrading, as it side-steps their packages and some are allergic to that. This'll lead to users who are left behind even more than they're now due to slow-moving distributions.

dominikh avatar Nov 30 '22 18:11 dominikh

I personally have a few reservations about this proposal.

Firstly, I don't feel that the Go version in go.mod should be the "minimum" supported Go version for a module. The way I have understood it has been that it's a "feature unlocker". See https://twitter.com/_myitcv/status/1391819772992659461, https://twitter.com/marcosnils/status/1391819263325974530, https://github.com/golang/go/issues/46201

It's very possible to enable a newer version of Go in go.mod to use things that are only available in that version, but gate those uses behind build tags, falling back to something else for older versions of Go. For example, //go:embed can be placed behind build tags such that it won't be used if the version of Go in use can't support it. The same applies for new exported stdlib types/functions. If go.mod enforced a minimum, that code couldn't be written (or at least be annoying to test).

This pattern seems common enough; x/tools and x/tools/gopls both set go 1.18, but in actuality test and support versions back to Go 1.16. It seems like this setup wouldn't be very tenable with this proposal implemented, as Go would automatically ignore that and download something else. Sure, maybe they could use GOTOOLCHAIN to work around that, but I think that'd be a major shift in how people generally get Go in CI. (It's also awkward to be able to go forward by changing Go on $PATH, but not backwards.)

Secondly, the automatic download/execution of binary releases of Go seems really surprising. I feel like it's going to be very awkward for Linux distributions to lose control of the toolchain in use without environment variables (especially if they patch Go). I do wonder how many distros might patch Go entirely to force GOTOOLCHAIN=local as the default. I believe there are also examples of corporate forks of Go (I've seen Microsoft's mentioned before), and those would also likely patch away this behavior becuase it'd be a bad thing for those to start bypassing the expected toolchain, especially without any sort of warning message that it's happening.

There are also systems where the binaries downloaded from golang.org won't be functional, e.g. Nix/NixOS (where the system layout is entirely different and outside binaries require a lot of work to use), musl-based distros like alpine (musl is not a first class libc for Go, as far as I know), and so on. Those distributors may also have to disable this download functionality to prevent breakage (be it to make things work consistently in the first place, or to just stop users from reporting the failures as distro bugs).

This part of the proposal is being compared to module dependency management itself. I strongly feel that modules work really, really well in comparison to other languages' package management scenarios (like node_modules, virtualenv, etc) in that it all happens automatically for you. But, I think the way this proposal does this for Go versions itself is going to be too surprising and likely to break.

(I mentioned some of this in https://github.com/golang/go/discussions/55092#discussioncomment-3755585, but my thread seems to have been missed as all of the threads around it were replied to.)

zikaeroh avatar Nov 30 '22 19:11 zikaeroh

I spoke to @ianlancetaylor about his concerns. For most commands, you can always run go version to see what version you are going to get in the next command. I think that will be enough for those commands (Ian does not completely agree but is willing to wait and see). The one exception is go install path@version.

For go install path@version, I think we probably have to do the module fetch and then potentially re-exec the go command based on the go line. I would be inclined to say we don't respect any toolchain line, just as we don't respect any replace lines. That is, today go install path@version is shorthand for:

mkdir /tmp/empty
cd /tmp/empty
go mod init empty
go get path@version
go install path

I think it should continue to be shorthand for that, which would mean ignoring the toolchain line in path/go.mod.

In the case where go install path@version needs to fetch and run a newer Go toolchain, I think it would be appropriate to print a message:

$ go install path@version
go: building path@version using go1.25.1
$ 

rsc avatar Nov 30 '22 19:11 rsc

@dominikh

I think the combination of auto-upgrading and not allowing old toolchains to build new code will change the Go ecosystem to either no longer support multiple versions of Go, or to delay experimenting with new language features.

Where previously a Go module could target Go 1.21 but make use of 1.22 features in optional, build-tagged files, it can no longer do so. To be allowed to use Go 1.22 language features, the go.mod has to specify go 1.22, but this triggers auto-upgrading / build failures on Go 1.21. The module now only supports Go 1.22.

This is true. Older versions of the module will still support Go 1.21, of course. It's just the latest version of the module that only supports Go 1.22. I don't think it's a sure thing that this is a problem. The same happens today for dependencies, of course, and it seems to be fine.

I agree that making it easier to update to a new Go toolchain may well result in people updating more quickly.

Auto-upgrading makes this seem fine at first: anyone who needs Go 1.22 will get it for free, automatically. However, I don't think auto-upgrading can be assumed to be pervasive. For one, most people who hack on Go at least occasionally will be using GOTOOLCHAIN=local, pointed at a git checkout. Furthermore, I predict that some Linux distributions will patch their Go packages to disable the automatic upgrading, as it side-steps their packages and some are allergic to that. This'll lead to users who are left behind even more than they're now due to slow-moving distributions.

People who hack on Go will be using GOTOOLCHAIN=local (that will be the default in their builds), but they will also presumably be hacking on the latest Go version, which will not be too old for any code.

I agree that some Linux distributions are likely to patch Go to default to GOTOOLCHAIN=local. That seems fine too, as long as users can still go env -w GOTOOLCHAIN=auto.

rsc avatar Nov 30 '22 19:11 rsc

@zikaeroh, Alpine should not have problems running standard Go distributions starting in Go 1.20. I am not sure about NixOS. The only thing it should need is a libc.so.6 for the dynamic linker to resolve. Or maybe we should build the distribution cmd/go with -tags netgo and then it wouldn't even need that. I wonder what that would break...

rsc avatar Nov 30 '22 19:11 rsc

My (rudimentary) understanding is that libc.6.so is not located in the "typical" location on NixOS, so would not be found by a normal Go binary. It'd be at a long path like /nix/store/ikl21vjfq900ccbqg1xasp83kadw6q8y-glibc-2.32-46/lib/libc.so.6 as each package deterministically uses specific other packages. So, a flavor of the Go package that uses the precompiled binary releases of Go would use patchelf to fix this. Downloaded versions would not have that patching applied, and therefore would not work.

See also: https://nixos.wiki/wiki/Packaging/Binaries

zikaeroh avatar Nov 30 '22 19:11 zikaeroh

@zikaeroh

This pattern seems common enough; x/tools and x/tools/gopls both set go 1.18, but in actuality test and support versions back to Go 1.16. It seems like this setup wouldn't be very tenable with this proposal implemented, as Go would automatically ignore that and download something else. Sure, maybe they could use GOTOOLCHAIN to work around that, but I think that'd be a major shift in how people generally get Go in CI. (It's also awkward to be able to go forward by changing Go on $PATH, but not backwards.)

x/tools/gopls only tests that far back because they want to build with what's on people's machines. If what's on people's machines knew how to fetch a newer toolchain then we'd have stopped needing to support older versions long ago.

I think that one reason people are slow to update to new Go versions because it is too difficult. What's difficult is managing Go installations. This proposal removes that difficulty, which in turn should make it easier for people to keep up with newer versions.

rsc avatar Nov 30 '22 19:11 rsc

Regarding Linux distributions setting GOTOOLCHAIN=local by default (which I think would be fine), I'm curious whether they apply similar rules to rustup or nvm. Does anyone know?

rsc avatar Nov 30 '22 19:11 rsc

I think that one reason people are slow to update to new Go versions because it is too difficult. What's difficult is managing Go installations. This proposal removes that difficulty, which in turn should make it easier for people to keep up with newer versions.

I guess I'm confused; my impression was that the hardest problem in upgrading was not obtaining a new version of Go, but making sure that the Go code works for that new version of Go, which is the backwards compatibility proposal (not this one).

I would be very interested to know what proportion of Go users obtain Go via a package manager (versus golang.org). It seems to me like most package managers are going to set GOTOOLCHAIN=local (as noted by @dominikh and me above). But it also seems to me like most Linux users are getting Go via their distro's package manager and macOS users via brew (and I personally use scoop/chocolately/winget on Windows).

If all of those users are going to end up getting local as their default, is anyone going to use auto?

I'm curious whether they apply similar rules to rustup or nvm. Does anyone know?

On Arch, you can either install the rust package or rustup; both "provide" rust, but the actual packages are usually built in a clean chroot and automatically get the former, the latest rust compiler package.

Arch doesn't package nvm or any other node version manager; without the AUR, only the latest node/npm/yarn are provided. But, I think it's the case that many users may end up installing nvm (or its many alternatives), in which case the version in use is basically whatever anyone wants. The closest example I can think is projects which check in .node_version or use volta to pin a particular version for their project, but I've more often seen this for pinning a local dev setup and then more versions are checked in CI anyway.

zikaeroh avatar Nov 30 '22 20:11 zikaeroh

We should describe what happens if we drop an existing port (per https://go.dev/wiki/PortingPolicy). Presumably the go command 1.N will see "go 1.N+1", try to download the binaries for 1.N+1, fail, and then give an error and stop the build.

For cases like NixOS I think we would have to expect users on that system to set GOTOOLCHAIN=local. And that in turn suggests that perhaps there should be a way to build a toolchain such that GOTOOLCHAIN=local is the default if not overridden by the environment variable.

ianlancetaylor avatar Nov 30 '22 20:11 ianlancetaylor

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

rsc avatar Nov 30 '22 20:11 rsc

I filed https://github.com/golang/go/issues/57007 for building cmd/go without libc.so.6, which I think would make NixOS happy.

I agree that it should be possible to build a toolchain with GOTOOLCHAIN=local as the default, but I don't think most package managers should do this. For example I don't think it makes sense for user-installed package managers like Chocolatey or Homebrew to do this at all. They are not as pedantic about "we are the only way to install software on your machine!" as the operating system-installed package managers are, and they should not be going out of their way to break what will end up being a core feature of the Go experience.

I also think a fair number of Go developers still use the installers we provide, and those of course will have the GOTOOLCHAIN=auto default.

rsc avatar Nov 30 '22 21:11 rsc

We should describe what happens if we drop an existing port (per https://go.dev/wiki/PortingPolicy). Presumably the go command 1.N will see "go 1.N+1", try to download the binaries for 1.N+1, fail, and then give an error and stop the build.

Yes, exactly. And we can give a good error.

rsc avatar Nov 30 '22 21:11 rsc

I guess I'm confused; my impression was that the hardest problem in upgrading was not obtaining a new version of Go, but making sure that the Go code works for that new version of Go, which is the backwards compatibility proposal (not this one).

Go's backward compatibility is already very good. To the extent that it needs work, the backwards compatibility proposal will make it even better. That will leave actually getting the upgraded Go toolchain as the hardest problem. My experience maintaining other machines where I build Go programs but don't do Go development has been that I don't upgrade often at all because it is annoying to go download and unpack the right tar files. If all it took was editing a go.mod, I would do that far more often.

Another point that I forgot to make in the doc is that setting the Go toolchain in go.mod means that all developers working on a project will agree on the toolchain version, without other special arrangements. This was of course one of the big improvements of modules and moving out of GOPATH, for dependency versions. The same property can be provided for the Go toolchain version. This would also mean that if you are moving back and forth between two projects that have chosen different Go toolchain versions as their standard toolchain, you get the right one for that project automatically by virtue of just being in that project. You don't have to change your PATH each time you switch, or maintain a global symlink in $HOME/bin, or remember to type 'go1.18 build' in one place and 'go1.19 build' in the other, or any other kludge. It just does the right thing.

rsc avatar Nov 30 '22 22:11 rsc

In a CI/CD environment, I don't think this feature would be useful. I would expect that job to be configured to use the correct Go version in the first place. Downloading a newer toolchain might not work anyway due to firewall restrictions. And as others have mentioned, using an older compiler with GOTOOLCHAIN=local should not fail, as it may mean the module only uses newer language features conditionally and is verifying compatibility. In addition, this could easily lead to a situation where a developer who attempts to test with an older go version on their local machine but forgets to set GOTOOLCHAIN=local (or doesn't know about it) will get different results than the build server.

On another note, while the go directive in go.mod controls language features like octal literals and generics, today it has no bearing on the standard library implementation. My expectation is that if I run go build with compiler version 1.X, then it will use standard library version 1.X. But with this change, that will not be the case if I use an older compiler, unless I set GOTOOLCHAIN=local.

Another point that I forgot to make in the doc is that setting the Go toolchain in go.mod means that all developers working on a project will agree on the toolchain version, without other special arrangements.

We build our own version of the toolchain and distribute it to our developers, rather than using what is on golang.org. So this would not address that problem for us, especially because it sounds like such a toolchain will default to GOTOOLCHAIN=local anyway. What would be more useful for us in this regard is a way to do an exact string match on the compiler's go version and fail if it is wrong. But this is independent of the go directive. (For example, we are currently using 1.18, but our go.mod file says 1.17 to prevent developers from using generics.) I see this is touched on with the new toolchain directive but it's not clear it would buy much in our particular use case.

Another use case is if I am writing a library and want to easily test it with multiple go versions on my local machine. It would be convenient if I could just do something like go test -go=1.17 ./... and have it test with the latest 1.17 release (regardless of what my "real" go version is). But the key is I'd want to specify on the command line, not in go.mod.

rittneje avatar Dec 01 '22 05:12 rittneje

Another point that I forgot to make in the doc is that setting the Go toolchain in go.mod means that all developers working on a project will agree on the toolchain version, without other special arrangements.

I find this to conflict with the idea from the original proposal that it's a minimum version; if my project says "go 1.13" because that's the minimum, I very likely do not want to use that version of Go for development. A newer toolchain will be faster and behave better when called by tooling like gopls or other analyzers (regardless of the version of Go that gopls is compiled with). Or, I will definitely want to publish binaries using the absolute latest version of Go possible. For example, esbuild says "go 1.13", but the actual binaries published to npm are from Go 1.19. If 1.13 this were to be the expected development version, that wouldn't be very optimal.

zikaeroh avatar Dec 01 '22 08:12 zikaeroh

An environment variable GOTOOLCHAIN would control this new behavior. The default, GOTOOLCHAIN=auto, would use the information in go.mod. Setting GOTOOLCHAIN to something else would override the go.mod. GOTOOLCHAIN=local would force use of the locally installed toolchain, and other values would choose specific releases. For example, to test the package in the current directory with Go 1.17.2:

GOTOOLCHAIN=go1.17.2 go test

To be honest I am fine with: go install github.org/dl/go1.17.2@latest.

mateusz834 avatar Dec 01 '22 13:12 mateusz834

If you have a module that says go 1.12 [...] Cloud Native Buildpacks will always build your code with Go 1.12, even if much newer releases of Go exist. [...] The GitHub Action setup-go [...] has the same problems that Cloud Native Buildpacks do.

I feel like this proposal also doesn't really address this problem. To me, it sounds like both of these features are fundamentally mis-designed. If I am publishing a library on GitHub, then I should be specifying a range of minor versions (e.g, 1.17+), and then it tests with the latest patch release of each of them as part of my merge gate. Since the go.mod file cannot be relied upon to denote the lower or the upper bound, it really has no bearing on this, except possibly as the default lower bound. Separately, if I am publishing an actual binary as a release artifact, then I should be specifying the Go version (for build reproducibility), but the go.mod should not be considered at all.

One final feature of treating the go version this way is that it would provide a way to fix for loop scoping, as discussed in discussion #56010. If we make that change, older Go toolchains must not assume that they can compile newer Go code successfully just because there are no compiler errors.

Existing versions of the Go compiler already do not fail just because the go directive is newer.

rittneje avatar Dec 01 '22 15:12 rittneje

if my project says "go 1.13" because that's the minimum, I very likely do not want to use that version of Go for development. A newer toolchain will be faster and behave better when called by tooling like gopls or other analyzers (regardless of the version of Go that gopls is compiled with).

I don't think this is a concern here. This proposal does not say that newer Go versions should download an older Go toolchain. It says that older Go versions should download a newer Go toolchain. When a newer Go version sees an older "go" line in go.mod, it will emulate the language features of the older Go language (that is already true today).

ianlancetaylor avatar Dec 01 '22 17:12 ianlancetaylor

don't think this is a concern here. This proposal does not say that newer Go versions should download an older Go toolchain.

I agree; that was my original interpretation of the proposal. It just seemed like the followups implied otherwise. (That is how pinning tends to work in other languages like node.)

zikaeroh avatar Dec 01 '22 18:12 zikaeroh

Change https://go.dev/cl/450916 mentions this issue: cmd/go: draft of forward compatibility work

gopherbot avatar Dec 02 '22 19:12 gopherbot

As others here have explained, this proposal would cause a lot of problems for CI/CD, for package management, for conditionally using newer features in older codebases with conditional compilation, ...

The version of go in go.mod is now a minimum, which is fine. Rather than a magic environment variable, I would add a new command, go upgrade, that can upgrade the go compiler if installed locally from the official packages, but gives a helpful message if one should upgrade using the package manager in stead.

beoran avatar Dec 03 '22 00:12 beoran

x/tools/gopls only tests that far back because they want to build with what's on people's machines. If what's on people's machines knew how to fetch a newer toolchain then we'd have stopped needing to support older versions long ago.

FWIW I only mentioned x/tools as it's an example of one the Go team maintains that was at the top of my mind. I don't find this particular point to be convincing as while this may be true for gopls specifically as it's an executable tool that users need to run somehow, most other examples are libraries (which are of course going to be built with "what's on people's machines"). If everyone is bumping the go directive in go.mod to gain access to new features, they still may be supporting older versions of Go (especially if they are following Go's own supported version policy).

This also is not exclusive to language features; I recall a series of CLs updating the x repos for lazy loading (#36460), for example CL 316111, which was only possible by bumping the version directive. All of the CLs in that series said:

Note that this does not prevent users with earlier go versions from successfully building packages from this module.

Which to me very much supports the idea that go directive is not a minimum version at all, but a feature unlocker.

(I apparently forgot to actually submit this reply days ago, oops.)

zikaeroh avatar Dec 05 '22 17:12 zikaeroh

This seems like a bad idea to me.

Other text file formats include a line identifying the tool version that created the file, and do not imply that older versions cannot process the file properly. So the current behavior is not without precedent. (Examples of these other formats aren't springing to mind, but it wasn't a novel concept when I first saw it in go.mod).

This change assumes that upgrading the toolchain will always be beneficial and regressions will never happen. That stance doesn't seem much different to me than that of a certain OS, where users can be forced to reboot for an update - with no possibility to defer or skip the update. With a language rather than an OS, this behavior may be less infuriating and less disruptive for most users, but I think it's still unacceptable.

If a change is to be made, I would instead deprecate the go keyword and replace it with two, such as recommended_go and minimum_go. The former would have the same behavior as the go keyword currently has, while the latter would cause the toolchain to exit with error. Neither of these keywords would cause silent upgrades, and the names should be less likely to mislead.

If I understand correctly, the minimum_go keyword's behavior should allow for the for loop scoping fix mentioned in the description, as it'd put a lower bound on the toolchain version in use.

mark-pictor-csec avatar Dec 05 '22 19:12 mark-pictor-csec

@mark-pictor-csec I think you just described the actual proposal.

minimum_go is spelled 'go'. It sets the minimum Go toolchain version that must be used to compile the module, whether used directly (via git clone + cd into it + run go commands) or indirectly (as a module dependency).

recommended_go is spelled 'toolchain'. It only applies when code is used directly by cd'ing into the module. It is ignored when the module is used indirectly as a required dependency.

It is not true that the proposal "assumes that upgrading the toolchain will always be beneficial and regressions will never happen." In fact I have gone out of my way in the past to explain why that's false. See for example https://research.swtch.com/vgo-mvs. Instead, the proposal, like Go modules as a whole, puts the user in control of which toolchain they use. There are no gremlins going around updating the Go toolchain behind your back. You only get a new go toolchain when you explicitly make a change to your go.mod file to indicate that. This is the same behavior as other modules: you keep getting the one listed in go.mod, even if there are newer ones, until you explicitly upgrade.

rsc avatar Dec 06 '22 13:12 rsc

Re: potential confusion

A few people, including @ianlancetaylor, mentioned potential confusion. That potential definitely exists (indeed, many people on this issue are confused about the details, which suggests I did not present this well enough). We have to make it easy for people to understand what toolchain they are using and why. The way to do that has not changed: run go version and it will tell you.

There is only one time when go version does not help you answer the question, and that is when you are using go install path@version, which does not use the local go.mod. In this case, if the go command does not use its bundled toolchain, we should print a message, like:

$ go install path@version
go: installing path@version using go1.25.1
$ 

That message combined with the ability to run go version should take care of understanding what is happening. It does not directly address understanding why. For that, we need to document the rules clearly. Perhaps go help version would be a good place to document that. I would write something like:

The Go distribution consists of a go command and a bundled Go toolchain, meaning the standard library as well as the compiler, assembler, and other tools. The go command can use that bundled Go toolchain as well as other versions that it downloads as needed. Running “go version” prints the version of the bundled Go toolchain that is being used, which depends on both the GOTOOLCHAIN environment variable and the go.mod file for the current work module.

These are the rules the go command follows to decide which Go toolchain to use. The rules are listed in priority order, from highest to lowest: the first matching rule wins.

  • If GOTOOLCHAIN=local, then the go command uses its bundled toolchain.
  • If GOTOOLCHAIN is set to a Go version, such as GOTOOLCHAIN=go1.23.4, then the go command always uses that specific version of Go.
  • If GOTOOLCHAIN=auto, then the go command consults the work module's go.mod file.
  • If GOTOOLCHAIN is unset, it defaults to auto in release builds and to local when building from Go's development branches.
  • If the go.mod file contains a “toolchain” line, the go command uses the indicated Go toolchain. The line “toolchain local” means to use the go command's bundled toolchain. Otherwise the “toolchain” line must name a Go version, like “toolchain go1.23.4”, and the go command uses that specific version.
  • If the go.mod file contains a “go” line, the toolchain decision depends on whether that line names a version of Go older or newer than the bundled toolchain. If the named Go version is newer than the bundled toolchain, the go command uses that specific newer version. Otherwise, the go command uses its bundled toolchain, emulating the old toolchain version as needed.
  • Finally, if GOTOOLCHAIN is unset and the go.mod has no “toolchain” or “go” line, the go command uses its bundled toolchain.

rsc avatar Dec 06 '22 14:12 rsc

Re: losing control over which Go toolchain is used

@zikaeroh raised a concern about whether Linux distributions would “lose control of the toolchain in use”, and @dominikh wrote that he thought “some Linux distributions will patch their Go packages to disable the automatic upgrading.” @ianlancetaylor said something similar to me directly. @zikaeroh seemed to imply that even Homebrew/Chocolatey/etc might apply such a patch. I of course cannot predict what these systems will do, but I would encourage them not to start deleting or disabling features in the software they package. I expect toolchain version selection to become a standard part of the Go developer workflow, and it would be a shame for these packagers to make it hard for Go users to get their work done. In the end, it's easy enough to override with go env -w GOTOOLCHAIN=auto (unless the code is removed entirely!), but it would still be best for Go to be the same out of the box for all users, no matter which box it comes out of.

I used this framing in my previous comment:

The Go distribution consists of a go command and a bundled Go toolchain, meaning the standard library as well as the compiler, assembler, and other tools. The go command can use that bundled Go toolchain as well as other versions that it downloads as needed.

I think that framing is useful when thinking about Linux distributions and other packagers too. Just as the go command can fetch specific code dependencies for use in a build, it would now be able to fetch specific toolchain dependencies for use in a build.

To address what might be the concerns, being able to fetch specific toolchain dependencies for a specific build, does not overwrite the existing bundled toolchain provided by the operating system. Nor does it change what version of Go you get when you create an empty directory, run go mod init, and start hacking: you get the bundled toolchain provided by the packager. But if a user explicitly indicates that they want to build source code that is too new for the bundled toolchain (for example, the go.mod explicitly says go 1.25 and the bundled toolchain is only go 1.21), then instead of a build failure, the go command fetches and uses an appropriate newer toolchain. Similarly, if a user explicitly indicates that they want a specific toolchain by setting GOTOOLCHAIN=go1.19.2 or writing toolchain go1.19.2 in go.mod, the go command will fetch and use that toolchain as requested.

Linux distributions packaging commands written in Go already have to decide how to handle those commands' module dependencies:

  • One option is to trust in Go's high-fidelity, reproducible builds and let the go command fetch the dependencies directly. I would hope that systems that take this approach are also comfortable letting the go command fetch any toolchain dependency as well, since the toolchain fetches have the same high-fidelity, reproducible behavior as module dependency fetches.
  • The other option is to insist on managing and providing all the source code that goes into the build. This is a completely understandable choice for a packager, and I assume they have built up mechanisms for adjusting the build environment to do that. For example perhaps they set GOPROXY=off to ensure there are no downloads and they supply an appropriate go.work or go.mod filled with replace statements to provide all the dependencies. Even packagers with those custom builds don't default GOPROXY to off for ordinary use by users, at least as far as I know. They simply run their own builds in a non-standard environment. I would hope that systems that take this approach would continue to customize their own build environment but not leak those details into the packaging of Go for end users. It is also worth pointing out that setting GOPROXY=off to disable module dependency fetches will also disable toolchain dependency fetches, since toolchains are treated as modules.

In either case, I don't think the decisions that distributions packaging Go commands make for their own builds should leak into their own packaging of Go itself. Setting a default GOTOOLCHAIN=local in a packaged Go distribution would make builds break when the go line is too new, and it would also make the go command ignore any toolchain line in the current work module's go.mod. Both of these would be confusing to Go users, who will expect the behavior explained in Go's own documentation as well as any new books about Go that are written.

As I've noted before, it wouldn't really make sense to me for a distribution to default GOTOOLCHAIN=local unless they are also defaulting GOPROXY=off and also disallowing packages like rustup and nvm, both of which do the same thing that this proposal would have the go command do: manage a collection of toolchains and run the version specified by the user's configuration for a specific build.

I am only speculating at the concerns of the Linux and other packagers. I would be happy to hear from them directly about any concerns and work with them to address those in a way that works for them and for Go. On the Linux side, perhaps @stapelberg has some thoughts? And if anyone working on Homebrew, Chocolatey, or others would be inclined to default GOTOOLCHAIN=local in those packagers, could you please get in touch? Commenting here is fine, or [email protected]. Thanks.

rsc avatar Dec 06 '22 14:12 rsc