Skip to content

Commit c0e8e40

Browse files
tpaschalisJay Conrod
authored and
Jay Conrod
committed
cmd/go: clean -cache -n should not delete cache
Uses the `cfg.BuildN` flag to avoid deleting inside the `if cleanCache` block. Introduces a test in src/cmd/go/testdata/script. Fixes #39250 Change-Id: I857c441b1d7aa7c68cfd646d6833e6eaca5b18d1 Reviewed-on: https://go-review.googlesource.com/c/go/+/235140 Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent b2ce393 commit c0e8e40

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,20 +137,27 @@ func runClean(cmd *base.Command, args []string) {
137137
if cfg.BuildN || cfg.BuildX {
138138
b.Showcmd("", "rm -r %s", strings.Join(subdirs, " "))
139139
}
140-
for _, d := range subdirs {
141-
// Only print the first error - there may be many.
142-
// This also mimics what os.RemoveAll(dir) would do.
143-
if err := os.RemoveAll(d); err != nil && !printedErrors {
144-
printedErrors = true
145-
base.Errorf("go clean -cache: %v", err)
140+
if !cfg.BuildN {
141+
for _, d := range subdirs {
142+
// Only print the first error - there may be many.
143+
// This also mimics what os.RemoveAll(dir) would do.
144+
if err := os.RemoveAll(d); err != nil && !printedErrors {
145+
printedErrors = true
146+
base.Errorf("go clean -cache: %v", err)
147+
}
146148
}
147149
}
148150
}
149151

150152
logFile := filepath.Join(dir, "log.txt")
151-
if err := os.RemoveAll(logFile); err != nil && !printedErrors {
152-
printedErrors = true
153-
base.Errorf("go clean -cache: %v", err)
153+
if cfg.BuildN || cfg.BuildX {
154+
b.Showcmd("", "rm -f %s", logFile)
155+
}
156+
if !cfg.BuildN {
157+
if err := os.RemoveAll(logFile); err != nil && !printedErrors {
158+
printedErrors = true
159+
base.Errorf("go clean -cache: %v", err)
160+
}
154161
}
155162
}
156163
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# We're testing cache behavior, so start with a clean GOCACHE.
2+
env GOCACHE=$WORK/cache
3+
4+
# Build something so that the cache gets populates
5+
go build main.go
6+
7+
# Check that cache contains directories before running
8+
exists $GOCACHE/00
9+
10+
# Run go clean -cache -n and ensure that directories weren't deleted
11+
go clean -cache -n
12+
exists $GOCACHE/00
13+
14+
# Re-run go clean cache without the -n flag go ensure that directories were properly removed
15+
go clean -cache
16+
! exists $GOCACHE/00
17+
18+
-- main.go --
19+
package main
20+
21+
import "fmt"
22+
23+
func main() {
24+
fmt.Println("hello!")
25+
}

0 commit comments

Comments
 (0)