diff --git a/src/installer/uninstaller.go b/src/installer/uninstaller.go index 68338d0..d21f6ae 100644 --- a/src/installer/uninstaller.go +++ b/src/installer/uninstaller.go @@ -75,9 +75,12 @@ func Uninstall(cfg UninstallConfig) error { targetSet := make(map[string]struct{}, len(targets)) for _, version := range targets { - normalized := normalizeVersionSpec(version) - if normalized != "" { - targetSet[normalized] = struct{}{} + // Exact uninstalls must key by full tag (including -alpha/-rc) so + // sibling prereleases that share an x.y.z core are not treated as the + // same target (nvm-windows/nvm#1561). + canonical := canonicalVersionSpec(version) + if canonical != "" { + targetSet[canonical] = struct{}{} } } if err := prepareActiveForUninstall(targetSet, record); err != nil { @@ -121,7 +124,7 @@ var runReshim = reshim func prepareActiveForUninstall(targetSet map[string]struct{}, record func(string)) error { cfg := settings.Global() - active := normalizeVersionSpec(cfg.ActiveVersion) + active := canonicalVersionSpec(cfg.ActiveVersion) if active == "" { return nil } @@ -131,7 +134,7 @@ func prepareActiveForUninstall(targetSet map[string]struct{}, record func(string installed := scanInstalledVersions() - last := normalizeVersionSpec(cfg.LastVersion) + last := canonicalVersionSpec(cfg.LastVersion) if last != "" { if _, removingLast := targetSet[last]; !removingLast { if isInstalledInList(installed, last) { @@ -170,25 +173,25 @@ func prepareActiveForUninstall(targetSet map[string]struct{}, record func(string func nextInstalledFallback(installed []string, active string, targetSet map[string]struct{}) (string, bool) { for _, candidate := range installed { - normalized := normalizeVersionSpec(candidate) - if normalized == "" || normalized == active { + canonical := canonicalVersionSpec(candidate) + if canonical == "" || canonical == active { continue } - if _, removing := targetSet[normalized]; removing { + if _, removing := targetSet[canonical]; removing { continue } - return normalized, true + return canonical, true } return "", false } func isInstalledInList(installed []string, version string) bool { - target := normalizeVersionSpec(version) + target := canonicalVersionSpec(version) if target == "" { return false } for _, candidate := range installed { - if normalizeVersionSpec(candidate) == target { + if canonicalVersionSpec(candidate) == target { return true } } @@ -245,6 +248,12 @@ func normalizeVersionSpec(version string) string { return resolver.NormalizeVersion(version) } +// canonicalVersionSpec keeps prerelease/build metadata so exact tags like +// 27.0.0-alpha.0.0.0 and 27.0.0-alpha.0.1.0 remain distinct. +func canonicalVersionSpec(version string) string { + return resolver.CanonicalVersion(version) +} + func isRangeSpec(version string) bool { parts := strings.Split(normalizeVersionSpec(version), ".") if len(parts) == 0 || len(parts) > 2 { @@ -378,33 +387,43 @@ func matchInstalledRange(spec string, installed []string) []string { func uninstallVersion(version string, cfg UninstallConfig, wg *sync.WaitGroup, failures *atomic.Int32, record func(string)) { defer wg.Done() - requestedSpec := normalizeVersionSpec(version) + requestedCore := normalizeVersionSpec(version) + requestedCanonical := canonicalVersionSpec(version) node_version := "" - if isRangeSpec(requestedSpec) { - if latest, ok := latestInstalledPartialMatch(requestedSpec, scanInstalledVersions()); ok { - node_version = latest + if isRangeSpec(requestedCore) { + if latest, ok := latestInstalledPartialMatch(requestedCore, scanInstalledVersions()); ok { + node_version = canonicalVersionSpec(latest) } } if node_version == "" { - resolvedVersion, _, err := resolver.Find(version) - if err != nil { - // Apps uninstall can still proceed from ARP InstallLocation without mirror resolution. - if cfg.FromApps && lookupARPInstallLocation(version) != "" { - node_version = normalizeVersionSpec(version) + // Exact prerelease tags are identified by their full canonical form. + // Prefer the caller's tag over Find/Normalize stripping (nvm#1561). + if resolver.IsUnstableVersion(version) && requestedCanonical != "" { + node_version = requestedCanonical + } else { + resolvedVersion, _, err := resolver.Find(version) + if err != nil { + // Apps uninstall can still proceed from ARP InstallLocation without mirror resolution. + if cfg.FromApps && lookupARPInstallLocation(version) != "" { + node_version = requestedCanonical + if node_version == "" { + node_version = requestedCore + } + } else { + fmt.Fprintf(os.Stderr, "FAILED v%s %v\n", version, err) + log.LogSystemChanged("uninstall", version, "", log.OutcomeFailed, err.Error()) + failures.Add(1) + return + } } else { - fmt.Fprintf(os.Stderr, "FAILED v%s %v\n", version, err) - log.LogSystemChanged("uninstall", version, "", log.OutcomeFailed, err.Error()) - failures.Add(1) - return + node_version = canonicalVersionSpec(resolvedVersion) } - } else { - node_version = normalizeVersionSpec(resolvedVersion) } } - matchSpecs := []string{normalizeVersionSpec(version), normalizeVersionSpec(node_version)} + matchSpecs := []string{requestedCanonical, canonicalVersionSpec(node_version)} installDirs := findInstalledVersionDirs(matchSpecs...) if uninstallDebugEnabled() { fmt.Fprintf(os.Stderr, "[uninstall-debug] requested=%s resolved=%s root=%s matches=%v dirs=%v\n", version, node_version, expand(settings.Global().Root), matchSpecs, installDirs) @@ -512,7 +531,7 @@ func findInstalledVersionDirs(versions ...string) []string { targets := make(map[string]struct{}, len(versions)) for _, version := range versions { - n := normalizeVersionSpec(version) + n := canonicalVersionSpec(version) if n != "" { targets[n] = struct{}{} } @@ -529,7 +548,7 @@ func findInstalledVersionDirs(versions ...string) []string { continue } - if _, ok := targets[normalizeVersionSpec(entry.Name())]; ok { + if _, ok := targets[canonicalVersionSpec(entry.Name())]; ok { result = append(result, versionDir) } } @@ -554,7 +573,7 @@ func unregisterNodeVersionForInstallDirs(version string, installDirs []string) b const uninstallRootPath = `Software\Microsoft\Windows\CurrentVersion\Uninstall` const keyPrefix = "nvm4w-node-v" - targetVersion := normalizeVersionSpec(version) + targetVersion := canonicalVersionSpec(version) if targetVersion == "" { return false } @@ -596,8 +615,8 @@ func unregisterNodeVersionForInstallDirs(version string, installDirs []string) b managedBy, _, _ := entryKey.GetStringValue("ManagedBy") entryKey.Close() - nameVersion := normalizeVersionSpec(strings.TrimPrefix(subKeyName, "nvm4w-node-v")) - displayVersionNorm := normalizeVersionSpec(displayVersion) + nameVersion := canonicalVersionSpec(strings.TrimPrefix(subKeyName, "nvm4w-node-v")) + displayVersionNorm := canonicalVersionSpec(displayVersion) installLocationNorm := normalizeInstallPath(installLocation) matchVersion := nameVersion == targetVersion || displayVersionNorm == targetVersion diff --git a/src/installer/uninstaller_test.go b/src/installer/uninstaller_test.go index 92f4659..6f2bfd2 100644 --- a/src/installer/uninstaller_test.go +++ b/src/installer/uninstaller_test.go @@ -78,6 +78,55 @@ func TestPrepareActiveForUninstallChoosesLatestSemverFallback(t *testing.T) { } } +func TestFindInstalledVersionDirsKeepsSiblingPrereleasesDistinct(t *testing.T) { + root := t.TempDir() + setTestSetting(t, "root", root) + + createInstalledVersion(t, root, "27.0.0-alpha.0.0.0") + createInstalledVersion(t, root, "27.0.0-alpha.0.1.0") + + dirs := findInstalledVersionDirs("27.0.0-alpha.0.0.0") + if len(dirs) != 1 { + t.Fatalf("findInstalledVersionDirs(alpha.0.0.0) len = %d, want 1 (%v)", len(dirs), dirs) + } + if got := filepath.Base(dirs[0]); got != "v27.0.0-alpha.0.0.0" { + t.Fatalf("findInstalledVersionDirs(alpha.0.0.0) = %q, want v27.0.0-alpha.0.0.0", got) + } + + // Core-only match must not collapse distinct prerelease installs. + if dirs := findInstalledVersionDirs("27.0.0"); len(dirs) != 0 { + t.Fatalf("findInstalledVersionDirs(27.0.0) = %v, want none (alphas are distinct tags)", dirs) + } +} + +func TestPrepareActiveForUninstallLeavesSiblingPrereleaseActive(t *testing.T) { + root := t.TempDir() + setTestSetting(t, "root", root) + setTestSetting(t, "active_version", "27.0.0-alpha.0.1.0") + setTestSetting(t, "last_version", "") + + createInstalledVersion(t, root, "27.0.0-alpha.0.0.0") + createInstalledVersion(t, root, "27.0.0-alpha.0.1.0") + + var messages []string + record := func(msg string) { messages = append(messages, msg) } + + if err := prepareActiveForUninstall(map[string]struct{}{"27.0.0-alpha.0.0.0": {}}, record); err != nil { + t.Fatalf("prepareActiveForUninstall() error = %v", err) + } + + active, err := settings.Get("active_version") + if err != nil { + t.Fatalf("Get(active_version) error = %v", err) + } + if got := active.(string); got != "27.0.0-alpha.0.1.0" { + t.Fatalf("active_version = %q, want %q (sibling alpha must stay active)", got, "27.0.0-alpha.0.1.0") + } + if len(messages) != 0 { + t.Fatalf("unexpected prepare messages: %v", messages) + } +} + func setTestSetting(t *testing.T, name, value string) { t.Helper() if err := settings.Put(name, value); err != nil {