Skip to content

Commit

Permalink
cmd/go: add -modfile and -modcacherw to go tool
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
ConradIrwin authored and matloob committed Dec 6, 2024
1 parent d3d4e7b commit ec7817e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/cmd/go/internal/tool/tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
}

Expand Down
9 changes: 9 additions & 0 deletions src/cmd/go/testdata/script/mod_cache_rw.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ cp $WORK/extraneous.txt $GOPATH/pkg/mod/rsc.io/[email protected]/extraneous_file.go
! go mod verify
[!root] ! cp $WORK/extraneous.txt $GOPATH/pkg/mod/rsc.io/[email protected]/go.mod

# Repeat part of the test with 'go tool'
go mod edit -tool example.com/tools/cmd/hello -require example.com/[email protected]
go mod download -modcacherw example.com/tools
go clean -modcache
go tool -modcacherw hello
cp $WORK/extraneous.txt $GOPATH/pkg/mod/example.com/[email protected]/extraneous_file.go
! go mod verify
[!root] ! cp $WORK/extraneous.txt $GOPATH/pkg/mod/example.com/[email protected]/cmd/hello/hello.go


-- $WORK/extraneous.txt --
module oops
Expand Down
4 changes: 4 additions & 0 deletions src/cmd/go/testdata/script/modfile_flag.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]
grep cmd/hello go.alt.mod
go tool hello

# 'go mod vendor' should work.
go mod vendor
Expand Down

0 comments on commit ec7817e

Please sign in to comment.