]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: add -modfile and -modcacherw to go tool
authorConrad Irwin <conrad.irwin@gmail.com>
Sun, 1 Dec 2024 06:22:49 +0000 (23:22 -0700)
committerMichael Matloob <matloob@golang.org>
Fri, 6 Dec 2024 18:28:57 +0000 (18:28 +0000)
When adding support for module tools, we added the ability for `go tool`
to edit the module cache. For users with `GOFLAGS=-modcacherw` this
could have led to a situation where some of the files in the mod cache
were unexpectedly not deletable.

We also allow -modfile so that people can select which module they are
working in when looking for tools.

We still do not support arbitrary build flags for tools with `go tool`.
If you want those, use `go run` or `go build`, etc. instead.

Updates #48429

Change-Id: Ic3c56bb8b6ba46114196465ca6ee2dcb08b9dcc0
Reviewed-on: https://go-review.googlesource.com/c/go/+/632575
Reviewed-by: Michael Matloob <matloob@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
src/cmd/go/internal/tool/tool.go
src/cmd/go/testdata/script/mod_cache_rw.txt
src/cmd/go/testdata/script/modfile_flag.txt

index 9fd917e0cb1a66b09b78fdaa7b8323bc460d4c91..2b18a4238436b3610633c08d7568e54809b6f8b0 100644 (file)
@@ -66,6 +66,7 @@ func isGccgoTool(tool string) bool {
 
 func init() {
        base.AddChdirFlag(&CmdTool.Flag)
+       base.AddModCommonFlags(&CmdTool.Flag)
        CmdTool.Flag.BoolVar(&toolN, "n", false, "")
 }
 
index 87f27e87de5872620627326779c8bc44f4007543..151bdfb791f6b289240e510c361f6a1b509e342f 100644 (file)
@@ -42,6 +42,15 @@ cp $WORK/extraneous.txt $GOPATH/pkg/mod/rsc.io/quote@v1.5.2/extraneous_file.go
 ! go mod verify
 [!root] ! cp $WORK/extraneous.txt $GOPATH/pkg/mod/rsc.io/quote@v1.5.2/go.mod
 
+# Repeat part of the test with 'go tool'
+go mod edit -tool example.com/tools/cmd/hello -require example.com/tools@v1.0.0
+go mod download -modcacherw example.com/tools
+go clean -modcache
+go tool -modcacherw hello
+cp $WORK/extraneous.txt $GOPATH/pkg/mod/example.com/tools@v1.0.0/extraneous_file.go
+! go mod verify
+[!root] ! cp $WORK/extraneous.txt $GOPATH/pkg/mod/example.com/tools@v1.0.0/cmd/hello/hello.go
+
 
 -- $WORK/extraneous.txt --
 module oops
index 6d28759849c962bf710514a746e81157d8c3768a..7260cf656b1f1dbdf48301cf0ec7e531d2a9cc10 100644 (file)
@@ -48,6 +48,10 @@ go build -n -mod=mod .
 go test -n -mod=mod .
 go get rsc.io/quote
 
+# 'go tool' and tool management should work.
+go get -tool example.com/tools/cmd/hello@v1.0.0
+grep cmd/hello go.alt.mod
+go tool hello
 
 # 'go mod vendor' should work.
 go mod vendor