]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: rename go.exe if cannot delete it during clean
authorAlex Brainman <alex.brainman@gmail.com>
Fri, 6 Sep 2013 20:55:35 +0000 (16:55 -0400)
committerRuss Cox <rsc@golang.org>
Fri, 6 Sep 2013 20:55:35 +0000 (16:55 -0400)
Fixes #6179

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/12916047

src/cmd/go/build.go
src/cmd/go/clean.go

index 9c3693abb3c377febe6f9ff8c0f655abf4912c2c..1846f745dafa1db6b72ddf432c9b18c81a96e751 100644 (file)
@@ -1093,7 +1093,7 @@ func (b *builder) copyFile(a *action, dst, src string, perm os.FileMode) error {
        if err != nil && toolIsWindows {
                // Windows does not allow deletion of a binary file
                // while it is executing.  Try to move it out of the way.
-               // If the remove fails, which is likely, we'll try again the
+               // If the move fails, which is likely, we'll try again the
                // next time we do an install of this binary.
                if err := os.Rename(dst, dst+"~"); err == nil {
                        os.Remove(dst + "~")
index bfae967a76b6632a8f1027c039d18d53a3373ad1..16687f72f700f48ea92fbc78fc064dfa8bf9b67c 100644 (file)
@@ -237,7 +237,23 @@ func clean(p *Package) {
 // removeFile tries to remove file f, if error other than file doesn't exist
 // occurs, it will report the error.
 func removeFile(f string) {
-       if err := os.Remove(f); err != nil && !os.IsNotExist(err) {
-               errorf("go clean: %v", err)
+       err := os.Remove(f)
+       if err == nil || os.IsNotExist(err) {
+               return
+       }
+       // Windows does not allow deletion of a binary file while it is executing.
+       if toolIsWindows {
+               // Remove lingering ~ file from last attempt.
+               if _, err2 := os.Stat(f + "~"); err2 == nil {
+                       os.Remove(f + "~")
+               }
+               // Try to move it out of the way. If the move fails,
+               // which is likely, we'll try again the
+               // next time we do an install of this binary.
+               if err2 := os.Rename(f, f+"~"); err2 == nil {
+                       os.Remove(f + "~")
+                       return
+               }
        }
+       errorf("go clean: %v", err)
 }