cmd/go: go clean should not accept flags like -modcache with packages

For #53725

Change-Id: I99a85b437d5f918dba74c4eccefcf8087193646a
Reviewed-on: https://go-review.googlesource.com/c/go/+/425874
Run-TryBot: Bryan Mills <bcmills@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
This commit is contained in:
cuiweixie 2022-08-26 15:11:40 +08:00 committed by Gopher Robot
parent 67d85ad00f
commit c22865fcfa
5 changed files with 28 additions and 0 deletions

View File

@ -118,6 +118,23 @@ func init() {
}
func runClean(ctx context.Context, cmd *base.Command, args []string) {
if len(args) > 0 {
cacheFlag := ""
switch {
case cleanCache:
cacheFlag = "-cache"
case cleanTestcache:
cacheFlag = "-testcache"
case cleanFuzzcache:
cacheFlag = "-fuzzcache"
case cleanModcache:
cacheFlag = "-modcache"
}
if cacheFlag != "" {
base.Fatalf("go: clean %s cannot be used with package arguments", cacheFlag)
}
}
// golang.org/issue/29925: only load packages before cleaning if
// either the flags and arguments explicitly imply a package,
// or no other target (such as a cache) was requested to be cleaned.

View File

@ -15,6 +15,9 @@ exists $GOCACHE/00
go clean -cache
! exists $GOCACHE/00
! go clean -cache .
stderr 'go: clean -cache cannot be used with package arguments'
-- main.go --
package main

View File

@ -8,6 +8,8 @@ go test x_test.go
go clean -testcache
go test x_test.go
! stdout 'cached'
! go clean -testcache ../x
stderr 'go: clean -testcache cannot be used with package arguments'
# golang.org/issue/29100: 'go clean -testcache' should succeed
# if the cache directory doesn't exist at all.

View File

@ -35,6 +35,9 @@ go clean -modcache
! stderr 'finding rsc.io'
go mod edit -droprequire rsc.io/quote
! go clean -modcache m
stderr 'go: clean -modcache cannot be used with package arguments'
-- go.mod --
module m
-- m.go --

View File

@ -37,6 +37,9 @@ go test -fuzz=FuzzEmpty -fuzztime=2x .
stdout 'new interesting: 0'
stdout 'total: 1'
! go clean -fuzzcache example.com/y
stderr 'go: clean -fuzzcache cannot be used with package arguments'
-- go.mod --
module example.com/y