From 1bc8170a3a07423a78518fbcb1af565ed4b908cd Mon Sep 17 00:00:00 2001 From: Corey Butler <770982+coreybutler@users.noreply.github.com> Date: Wed, 25 Nov 2026 11:56:59 -0600 Subject: [PATCH] fix: treat incomplete version dirs as not installed Require node.exe for install skip, releases installed marks, and env version counts. Hollow dirs from interrupted migration are removed and reinstalled instead of SKIPPED (nvm#1580). Fixes nvm-windows/nvm#1580 Signed-off-by: Corey Butler <770982+coreybutler@users.noreply.github.com> --- src/commands/env.go | 10 +++++-- src/commands/list/common.go | 19 +++++++++---- src/commands/list/common_test.go | 47 ++++++++++++++++++++++++++++++++ src/installer/installer.go | 43 ++++++++++++++++++++++++----- src/installer/installer_test.go | 28 +++++++++++++++++++ 5 files changed, 133 insertions(+), 14 deletions(-) create mode 100644 src/commands/list/common_test.go diff --git a/src/commands/env.go b/src/commands/env.go index 8a2514b..9c2da83 100644 --- a/src/commands/env.go +++ b/src/commands/env.go @@ -748,9 +748,15 @@ func installedVersionCount(root string) int { } name := strings.ToLower(entry.Name()) - if strings.HasPrefix(name, "v") { - count++ + if !strings.HasPrefix(name, "v") { + continue + } + nodeExe := filepath.Join(root, entry.Name(), "node.exe") + info, statErr := os.Stat(nodeExe) + if statErr != nil || info.IsDir() { + continue } + count++ } return count diff --git a/src/commands/list/common.go b/src/commands/list/common.go index f107d60..4ec9903 100644 --- a/src/commands/list/common.go +++ b/src/commands/list/common.go @@ -4,6 +4,7 @@ import ( "common/settings" "nvm/commands/cache" "os" + "path/filepath" "strconv" "strings" ) @@ -86,6 +87,7 @@ func getCachedVersionsMap() map[string]bool { } // getInstalledVersionsMap returns a map of installed version numbers (with "v" prefix). +// A version directory counts only when node.exe is present (same rule as nvm list / use). func getInstalledVersionsMap() map[string]bool { installed := make(map[string]bool) @@ -101,12 +103,19 @@ func getInstalledVersionsMap() map[string]bool { } for _, entry := range entries { - if entry.IsDir() { - name := strings.ToLower(entry.Name()) - if strings.HasPrefix(name, "v") { - installed[name] = true - } + if !entry.IsDir() { + continue + } + name := strings.ToLower(entry.Name()) + if !strings.HasPrefix(name, "v") { + continue + } + nodeExe := filepath.Join(root, entry.Name(), "node.exe") + info, statErr := os.Stat(nodeExe) + if statErr != nil || info.IsDir() { + continue } + installed[name] = true } return installed diff --git a/src/commands/list/common_test.go b/src/commands/list/common_test.go new file mode 100644 index 0000000..fc70ccc --- /dev/null +++ b/src/commands/list/common_test.go @@ -0,0 +1,47 @@ +package list + +import ( + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + + prefs "common/preferences" + "common/settings" +) + +func TestGetInstalledVersionsMapRequiresNodeExe(t *testing.T) { + root := t.TempDir() + key := `HKCU/Software/NVMTest/list_installed_map/` + strings.ReplaceAll(t.Name(), "/", "_") + prefs.ROOT = key + prefs.ROOTS = []string{key} + settings.Load() + t.Cleanup(func() { + _ = exec.Command("reg", "delete", `HKCU\Software\NVMTest\list_installed_map`, "/f").Run() + }) + + complete := filepath.Join(root, "v24.22.3") + hollow := filepath.Join(root, "v22.0.0") + if err := os.MkdirAll(complete, 0755); err != nil { + t.Fatal(err) + } + if err := os.MkdirAll(hollow, 0755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(complete, "node.exe"), []byte("node"), 0644); err != nil { + t.Fatal(err) + } + + if err := settings.Put("root", root); err != nil { + t.Fatalf("settings.Put(root): %v", err) + } + + got := getInstalledVersionsMap() + if !got["v24.22.3"] { + t.Fatalf("expected v24.22.3 installed, got %#v", got) + } + if got["v22.0.0"] { + t.Fatalf("hollow v22.0.0 must not be marked installed, got %#v", got) + } +} diff --git a/src/installer/installer.go b/src/installer/installer.go index fa464f0..b78fefc 100644 --- a/src/installer/installer.go +++ b/src/installer/installer.go @@ -196,16 +196,29 @@ func install( } defer healInstalledVersionVisibility(txn.installDir) - if info, err := os.Stat(txn.installDir); err == nil && info.IsDir() { - txn.installed = true - } + // "Installed" means a usable tree (node.exe present), not merely a version + // directory. Hollow dirs from interrupted migration otherwise make list/use + // disagree with install skip / releases (nvm#1580). + txn.installed = versionTreeComplete(txn.installDir) if txn.cacheFile != "" { if _, err := os.Stat(txn.cacheFile); err == nil { txn.cached = true } } - if !cfg.Force && !cfg.CacheOnly { + if !cfg.Force && !cfg.CacheOnly && txn.installed { + if trustErr := fs.CheckVersionDirTrust(txn.installDir); trustErr != nil { + status.Alert(fmt.Errorf("FAILED: v%s %v", nodeVersion, trustErr), false) + log.LogSystemChanged("install", nodeVersion, txn.installDir, log.OutcomeFailed, trustErr.Error()) + markFailed() + return + } + status.Alert(fmt.Errorf("SKIPPED: v%s is already installed", nodeVersion), false) + log.LogSystemChanged("install", nodeVersion, txn.installDir, log.OutcomeSkipped, "already installed") + return + } + + if !cfg.CacheOnly && !txn.installed { if info, err := os.Stat(txn.installDir); err == nil && info.IsDir() { if trustErr := fs.CheckVersionDirTrust(txn.installDir); trustErr != nil { status.Alert(fmt.Errorf("FAILED: v%s %v", nodeVersion, trustErr), false) @@ -213,9 +226,14 @@ func install( markFailed() return } - status.Alert(fmt.Errorf("SKIPPED: v%s is already installed", nodeVersion), false) - log.LogSystemChanged("install", nodeVersion, txn.installDir, log.OutcomeSkipped, "already installed") - return + if remErr := os.RemoveAll(txn.installDir); remErr != nil { + status.Alert(fmt.Errorf("FAILED: v%s incomplete install at %s (missing node.exe); could not remove: %w", nodeVersion, txn.installDir, remErr), false) + log.LogSystemChanged("install", nodeVersion, txn.installDir, log.OutcomeFailed, remErr.Error()) + markFailed() + return + } + status.Alert(fmt.Errorf("INCOMPLETE: v%s had no node.exe; reinstalling", nodeVersion), false) + log.LogSystemChanged("install", nodeVersion, txn.installDir, log.OutcomeSkipped, "incomplete version directory removed") } } @@ -670,6 +688,17 @@ func getRoot(version string) string { return filepath.Join(target, fmt.Sprintf("v%s", version)) } +// versionTreeComplete reports whether installDir is a usable Node.js version +// tree (node.exe present). An empty or partial directory is not installed. +func versionTreeComplete(installDir string) bool { + if strings.TrimSpace(installDir) == "" { + return false + } + nodeExe := filepath.Join(installDir, "node.exe") + info, err := os.Stat(nodeExe) + return err == nil && !info.IsDir() +} + func expand(path string) string { re := regexp.MustCompile(`%([^%]+)%`) return re.ReplaceAllStringFunc(path, func(match string) string { diff --git a/src/installer/installer_test.go b/src/installer/installer_test.go index f1a31f7..69ad4d8 100644 --- a/src/installer/installer_test.go +++ b/src/installer/installer_test.go @@ -342,3 +342,31 @@ func TestCleanupCurrentUserAppDataRemovesNodeWindowsAppsEntries(t *testing.T) { key.Close() } } + +func TestVersionTreeCompleteRequiresNodeExe(t *testing.T) { + root := t.TempDir() + complete := filepath.Join(root, "v24.22.3") + hollow := filepath.Join(root, "v22.0.0") + if err := os.MkdirAll(complete, 0755); err != nil { + t.Fatal(err) + } + if err := os.MkdirAll(hollow, 0755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(complete, "node.exe"), []byte("node"), 0644); err != nil { + t.Fatal(err) + } + + if !versionTreeComplete(complete) { + t.Fatal("expected complete tree with node.exe") + } + if versionTreeComplete(hollow) { + t.Fatal("hollow version dir must not count as installed") + } + if versionTreeComplete(filepath.Join(root, "missing")) { + t.Fatal("missing dir must not count as installed") + } + if versionTreeComplete("") { + t.Fatal("empty path must not count as installed") + } +}