From 1b9d9aa0586afb5e1f4f61361f38a0136660abc2 Mon Sep 17 00:00:00 2001 From: Jake Chitel Date: Fri, 19 Jan 2018 12:49:27 -0600 Subject: [PATCH] Added exponential backoff for copying npm installation --- src/nvm.go | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/nvm.go b/src/nvm.go index 2c2ca15..3762bcd 100644 --- a/src/nvm.go +++ b/src/nvm.go @@ -8,6 +8,7 @@ import ( "io/ioutil" "regexp" "bytes" + "time" "./nvm/web" "./nvm/arch" "./nvm/file" @@ -280,13 +281,26 @@ func install(version string, cpuarch string) { os.Rename(filepath.Join(tempNpmBin, "npx.cmd"), filepath.Join(env.root, "v"+version, "npx.cmd")) } - os.Rename(filepath.Join(tempDir, "nvm-npm", "npm-"+npmv), filepath.Join(env.root, "v"+version, "node_modules", "npm")) + time.Sleep(10000*time.Millisecond) + err := os.Rename(filepath.Join(tempDir, "nvm-npm", "npm-"+npmv), filepath.Join(env.root, "v"+version, "node_modules", "npm")) + if err != nil { + // sometimes Windows can take some time to enable access to large amounts of files after unzip, use exponential backoff to wait until it is ready + for _, i := range [5]int{1, 2, 4, 8, 16} { + time.Sleep(time.Duration(i)*time.Second) + err = os.Rename(filepath.Join(tempDir, "nvm-npm", "npm-"+npmv), filepath.Join(env.root, "v"+version, "node_modules", "npm")) + if err == nil { break } + } + } - // Remove the temp directory - // may consider keep the temp files here - os.RemoveAll(tempDir) + if err == nil { + // Remove the temp directory + // may consider keep the temp files here + os.RemoveAll(tempDir) - fmt.Println("\n\nInstallation complete. If you want to use this version, type\n\nnvm use "+version) + fmt.Println("\n\nInstallation complete. If you want to use this version, type\n\nnvm use "+version) + } else { + fmt.Println("Error: Unable to install NPM: "+err.Error()); + } } else { fmt.Println("Could not download npm for node v"+version+".") fmt.Println("Please visit https://github.com/npm/npm/releases/tag/v"+npmv+" to download npm.") -- GitLab