-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/go/internal/toolchain: make a best effort to parse 'go run' and '…
…go install' flags When the argument to 'go install' or 'go run' looks like a versioned package, we make a best effort to switch to a toolchain compatible with the module containing that package, by fetching its go.mod file and checking the go version it specifies. At this point in the code, we have not yet parsed the arguments given on the command line: instead, we just make a best effort to find one we can use to select a toolchain version. Since that toolchain may be newer, the command to install it may also include flags that are only supported by that Go version — and we don't want to fail due to an error that would be resolved by switching to a more appropriate toolchain. So at this point in the code we can't parse the flags in a way that will surface errors, but we want to make a best effort to parse the ones that we know about. It turns out that “parse the flags we know about” is already a familiar problem: that's also what we do in 'go test', so we can reuse the cmdflag library from that to do the best-effort pass of parsing. If it turns out that we don't need to switch toolchains after all, cmd/go's main function will parse the flags again, and will report any errors at that point. This fixes a regression, introduced in CL 497879, which caused 'go install -modcacherw pkg@version' to unset the write bit for directories created while selecting the toolchain to use. Fixes golang#64282. Updates golang#57001. Change-Id: Icc409c57858aa15c7d58a97a61964b4bc2560547 Cq-Include-Trybots: luci.golang.try:gotip-linux-amd64-longtest,gotip-windows-amd64-longtest Reviewed-on: https://go-review.googlesource.com/c/go/+/546635 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Bryan Mills <bcmills@google.com> Reviewed-by: Michael Matloob <matloob@golang.org>
- Loading branch information
Showing
10 changed files
with
91 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/cmd/go/testdata/script/install_modcacherw_issue64282.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Regression test for https://go.dev/issue/64282: | ||
# 'go install' and 'go run' with pkg@version arguments should make | ||
# a best effort to parse flags before they download modules to | ||
# identify which toolchain version to use, because those flags | ||
# may affect the downloaded contents. | ||
|
||
# However, the best-effort flag parsing should not interfere with | ||
# actual flag parsing if we don't switch toolchains. In particular, | ||
# unrecognized flags should still be diagnosed after the module for | ||
# the requested package has been downloaded and checked for toolchain | ||
# upgrades. | ||
|
||
! go install -cake=delicious -modcacherw example.com/printversion@v0.1.0 | ||
stderr '^flag provided but not defined: -cake$' | ||
|
||
[!short] go install -modcacherw example.com/printversion@v0.1.0 | ||
# Because the -modcacherw flag was set, we should be able to modify the contents | ||
# of a directory within the module cache. | ||
cp $WORK/extraneous.txt $GOPATH/pkg/mod/example.com/printversion@v0.1.0/extraneous_file.go | ||
|
||
|
||
# We should also apply flags from GOFLAGS at this step. | ||
|
||
go clean -modcache | ||
env GOFLAGS=-modcacherw | ||
! go install -cake=delicious example.com/printversion@v0.1.0 | ||
stderr '^flag provided but not defined: -cake$' | ||
cp $WORK/extraneous.txt $GOPATH/pkg/mod/example.com/printversion@v0.1.0/extraneous_file.go | ||
|
||
|
||
-- $WORK/extraneous.txt -- | ||
This is not a Go source file. |
Oops, something went wrong.