]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: clean -cache -n should not delete cache
authorPaschalis Tsilias <paschalis.tsilias@gmail.com>
Tue, 26 May 2020 14:45:11 +0000 (17:45 +0300)
committerJay Conrod <jayconrod@google.com>
Wed, 27 May 2020 13:37:56 +0000 (13:37 +0000)
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 <jayconrod@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
src/cmd/go/internal/clean/clean.go
src/cmd/go/testdata/script/clean_cache_n.txt [new file with mode: 0644]

index d5028de970c00cf7295d7dede6d5595081a278ae..99704cb2b1b8c4da0194c25b5a59ca4d9df7309a 100644 (file)
@@ -137,20 +137,27 @@ func runClean(cmd *base.Command, args []string) {
                                if cfg.BuildN || cfg.BuildX {
                                        b.Showcmd("", "rm -r %s", strings.Join(subdirs, " "))
                                }
-                               for _, d := range subdirs {
-                                       // Only print the first error - there may be many.
-                                       // This also mimics what os.RemoveAll(dir) would do.
-                                       if err := os.RemoveAll(d); err != nil && !printedErrors {
-                                               printedErrors = true
-                                               base.Errorf("go clean -cache: %v", err)
+                               if !cfg.BuildN {
+                                       for _, d := range subdirs {
+                                               // Only print the first error - there may be many.
+                                               // This also mimics what os.RemoveAll(dir) would do.
+                                               if err := os.RemoveAll(d); err != nil && !printedErrors {
+                                                       printedErrors = true
+                                                       base.Errorf("go clean -cache: %v", err)
+                                               }
                                        }
                                }
                        }
 
                        logFile := filepath.Join(dir, "log.txt")
-                       if err := os.RemoveAll(logFile); err != nil && !printedErrors {
-                               printedErrors = true
-                               base.Errorf("go clean -cache: %v", err)
+                       if cfg.BuildN || cfg.BuildX {
+                               b.Showcmd("", "rm -f %s", logFile)
+                       }
+                       if !cfg.BuildN {
+                               if err := os.RemoveAll(logFile); err != nil && !printedErrors {
+                                       printedErrors = true
+                                       base.Errorf("go clean -cache: %v", err)
+                               }
                        }
                }
        }
diff --git a/src/cmd/go/testdata/script/clean_cache_n.txt b/src/cmd/go/testdata/script/clean_cache_n.txt
new file mode 100644 (file)
index 0000000..4497b36
--- /dev/null
@@ -0,0 +1,25 @@
+# We're testing cache behavior, so start with a clean GOCACHE.
+env GOCACHE=$WORK/cache
+
+# Build something so that the cache gets populates
+go build main.go
+
+# Check that cache contains directories before running
+exists $GOCACHE/00
+
+# Run go clean -cache -n and ensure that directories weren't deleted
+go clean -cache -n
+exists $GOCACHE/00
+
+# Re-run go clean cache without the -n flag go ensure that directories were properly removed
+go clean -cache
+! exists $GOCACHE/00
+
+-- main.go --
+package main
+
+import "fmt"
+
+func main() {
+       fmt.Println("hello!")
+}