Skip to content

Commit ec7817e

Browse files
ConradIrwinmatloob
authored andcommitted
cmd/go: add -modfile and -modcacherw to go tool
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 <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Michael Knyszek <[email protected]>
1 parent d3d4e7b commit ec7817e

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

src/cmd/go/internal/tool/tool.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func isGccgoTool(tool string) bool {
6666

6767
func init() {
6868
base.AddChdirFlag(&CmdTool.Flag)
69+
base.AddModCommonFlags(&CmdTool.Flag)
6970
CmdTool.Flag.BoolVar(&toolN, "n", false, "")
7071
}
7172

src/cmd/go/testdata/script/mod_cache_rw.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ cp $WORK/extraneous.txt $GOPATH/pkg/mod/rsc.io/[email protected]/extraneous_file.go
4242
! go mod verify
4343
[!root] ! cp $WORK/extraneous.txt $GOPATH/pkg/mod/rsc.io/[email protected]/go.mod
4444

45+
# Repeat part of the test with 'go tool'
46+
go mod edit -tool example.com/tools/cmd/hello -require example.com/[email protected]
47+
go mod download -modcacherw example.com/tools
48+
go clean -modcache
49+
go tool -modcacherw hello
50+
cp $WORK/extraneous.txt $GOPATH/pkg/mod/example.com/[email protected]/extraneous_file.go
51+
! go mod verify
52+
[!root] ! cp $WORK/extraneous.txt $GOPATH/pkg/mod/example.com/[email protected]/cmd/hello/hello.go
53+
4554

4655
-- $WORK/extraneous.txt --
4756
module oops

src/cmd/go/testdata/script/modfile_flag.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ go build -n -mod=mod .
4848
go test -n -mod=mod .
4949
go get rsc.io/quote
5050

51+
# 'go tool' and tool management should work.
52+
go get -tool example.com/tools/cmd/[email protected]
53+
grep cmd/hello go.alt.mod
54+
go tool hello
5155

5256
# 'go mod vendor' should work.
5357
go mod vendor

0 commit comments

Comments
 (0)