Skip to content

Commit 97f7964

Browse files
committed
clean: clean cache with -n should only print remove commands.
runClean when cleanCache should not execute remove commands when cfg.BuildN = true, but only print them. Fixes #27469.
1 parent 5188c87 commit 97f7964

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,15 @@ func runClean(cmd *base.Command, args []string) {
126126
if cfg.BuildN || cfg.BuildX {
127127
b.Showcmd("", "rm -r %s", strings.Join(subdirs, " "))
128128
}
129-
printedErrors := false
130-
for _, d := range subdirs {
131-
// Only print the first error - there may be many.
132-
// This also mimics what os.RemoveAll(dir) would do.
133-
if err := os.RemoveAll(d); err != nil && !printedErrors {
134-
printedErrors = true
135-
base.Errorf("go clean -cache: %v", err)
129+
if !cfg.BuildN {
130+
printedErrors := false
131+
for _, d := range subdirs {
132+
// Only print the first error - there may be many.
133+
// This also mimics what os.RemoveAll(dir) would do.
134+
if err := os.RemoveAll(d); err != nil && !printedErrors {
135+
printedErrors = true
136+
base.Errorf("go clean -cache: %v", err)
137+
}
136138
}
137139
}
138140
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Set up fresh GOCACHE.
2+
env GOCACHE=$WORK/gocache
3+
mkdir $GOCACHE
4+
5+
go build lib.go
6+
7+
# Would only print the remove commands
8+
go clean -cache -n
9+
stdout 'gocache/[0-9a-z][0-9a-z]'
10+
exists $WORK/gocache/01
11+
12+
# Would run the remove commands
13+
go clean -cache
14+
! exists $WORK/gocache/01
15+
16+
-- lib.go --
17+
package lib

0 commit comments

Comments
 (0)