Skip to content

Commit c22865f

Browse files
cuiweixiegopherbot
authored andcommitted
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>
1 parent 67d85ad commit c22865f

File tree

5 files changed

+28
-0
lines changed

5 files changed

+28
-0
lines changed

src/cmd/go/internal/clean/clean.go

+17
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,23 @@ func init() {
118118
}
119119

120120
func runClean(ctx context.Context, cmd *base.Command, args []string) {
121+
if len(args) > 0 {
122+
cacheFlag := ""
123+
switch {
124+
case cleanCache:
125+
cacheFlag = "-cache"
126+
case cleanTestcache:
127+
cacheFlag = "-testcache"
128+
case cleanFuzzcache:
129+
cacheFlag = "-fuzzcache"
130+
case cleanModcache:
131+
cacheFlag = "-modcache"
132+
}
133+
if cacheFlag != "" {
134+
base.Fatalf("go: clean %s cannot be used with package arguments", cacheFlag)
135+
}
136+
}
137+
121138
// golang.org/issue/29925: only load packages before cleaning if
122139
// either the flags and arguments explicitly imply a package,
123140
// or no other target (such as a cache) was requested to be cleaned.

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

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ exists $GOCACHE/00
1515
go clean -cache
1616
! exists $GOCACHE/00
1717

18+
! go clean -cache .
19+
stderr 'go: clean -cache cannot be used with package arguments'
20+
1821
-- main.go --
1922
package main
2023

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

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ go test x_test.go
88
go clean -testcache
99
go test x_test.go
1010
! stdout 'cached'
11+
! go clean -testcache ../x
12+
stderr 'go: clean -testcache cannot be used with package arguments'
1113

1214
# golang.org/issue/29100: 'go clean -testcache' should succeed
1315
# if the cache directory doesn't exist at all.

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

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ go clean -modcache
3535
! stderr 'finding rsc.io'
3636
go mod edit -droprequire rsc.io/quote
3737

38+
! go clean -modcache m
39+
stderr 'go: clean -modcache cannot be used with package arguments'
40+
3841
-- go.mod --
3942
module m
4043
-- m.go --

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

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ go test -fuzz=FuzzEmpty -fuzztime=2x .
3737
stdout 'new interesting: 0'
3838
stdout 'total: 1'
3939

40+
! go clean -fuzzcache example.com/y
41+
stderr 'go: clean -fuzzcache cannot be used with package arguments'
42+
4043
-- go.mod --
4144
module example.com/y
4245

0 commit comments

Comments
 (0)