From 0290f21b30b0381b33d2cac7d83b4fa798100754 Mon Sep 17 00:00:00 2001 From: Corey Butler <770982+coreybutler@users.noreply.github.com> Date: Mon, 23 Nov 2026 12:52:20 -0600 Subject: [PATCH] fix(installer): hard-fail and roll back when reshim cannot create shims Run reshim before discarding install backups; on failure show the Help Enabling Symlinks toast, print guidance, roll back new version dirs, and return an error instead of reporting success (nvm#1570). Signed-off-by: Corey Butler <770982+coreybutler@users.noreply.github.com> --- src/installer/activation.go | 2 ++ src/installer/installer.go | 18 ++-------- src/installer/reshim.go | 66 +++++++++++++++++++++++++++++++++++- src/installer/reshim_test.go | 46 +++++++++++++++++++++++++ src/link/link.go | 4 +-- 5 files changed, 116 insertions(+), 20 deletions(-) diff --git a/src/installer/activation.go b/src/installer/activation.go index d84c52d..ceb262c 100644 --- a/src/installer/activation.go +++ b/src/installer/activation.go @@ -104,6 +104,8 @@ func ActivateVersion(version string) error { if mode == "shim" { if err := nvmreshim.Run(); err != nil { log.Error(err) + _ = notify.HelpEnablingSymlinks(settings.AppId) + printShimLinkGuidance() return err } } else if source, err := stringSetting("root"); err == nil { diff --git a/src/installer/installer.go b/src/installer/installer.go index 2833b18..fa464f0 100644 --- a/src/installer/installer.go +++ b/src/installer/installer.go @@ -129,22 +129,8 @@ func Install(cfg InstallConfig) error { return status.Abort("Installation cancelled.", false) } - for _, txn := range txns { - if txn != nil && txn.installBackup != "" && !txn.backupDiscarded { - os.RemoveAll(txn.installBackup) - txn.backupDiscarded = true - } - } - - if status.TotalInstalled > 0 || modulesInstalled { - if status.TotalInstalled > 0 { - if err := activateDefaultIfFirstInstall(hadInstalledVersions, status.Versions); err != nil { - status.Alert(fmt.Errorf("activation warning: %v", err)) - } - } - if err := reshim(); err != nil { - status.Alert(fmt.Errorf("reshim warning: %v", err)) - } + if err := finalizeInstallShims(txns, status, modulesInstalled, hadInstalledVersions); err != nil { + return err } if err := seedMalwareDefinitions(status); err != nil { diff --git a/src/installer/reshim.go b/src/installer/reshim.go index 1e09efe..0b12c8e 100644 --- a/src/installer/reshim.go +++ b/src/installer/reshim.go @@ -1,7 +1,71 @@ package installer -import "nvm/bootstrap" +import ( + "common/notify" + "common/settings" + "fmt" + "os" + + "nvm/bootstrap" +) func reshim() error { return bootstrap.RunReshim("--silent") } + +// printShimLinkGuidance mirrors reshim stderr when hardlink and symlink both fail. +func printShimLinkGuidance() { + fmt.Fprint(os.Stderr, ` +Shim links could not be created (hard link and symlink both failed). +Hard links require .shim and proxy.exe on the same volume; symlinks need +Developer Mode or the Create symbolic links privilege. + +Try one of: + 1) Keep Node storage on the same drive as NVM, for example: + nvm config set root=%LOCALAPPDATA%\Author Software\nvm\installs + 2) Switch to link mode: + nvm config set mode=link + nvm use link + 3) Enable Developer Mode (symlink privilege): + https://docs.nvm-windows.com/features/modes +`) +} + +// finalizeInstallShims runs reshim after version installs, discarding backups only +// on success. On failure it rolls back newly installed dirs and hard-fails. +func finalizeInstallShims(txns []*Transaction, status *Status, modulesInstalled bool, hadInstalledVersions bool) error { + if status.TotalInstalled > 0 || modulesInstalled { + // Reshim before discarding install backups / activating, so a broken + // shim set can roll back newly installed version dirs. + if err := runReshim(); err != nil { + _ = notify.HelpEnablingSymlinks(settings.AppId) + printShimLinkGuidance() + status.Cancel("Installation rolled back: shim links could not be created.") + for _, txn := range txns { + rollbackCanceledInstall(txn, status) + } + return status.Abort(fmt.Errorf("reshim failed: %w", err)) + } + + discardInstallBackups(txns) + + if status.TotalInstalled > 0 { + if err := activateDefaultIfFirstInstall(hadInstalledVersions, status.Versions); err != nil { + status.Alert(fmt.Errorf("activation warning: %v", err)) + } + } + return nil + } + + discardInstallBackups(txns) + return nil +} + +func discardInstallBackups(txns []*Transaction) { + for _, txn := range txns { + if txn != nil && txn.installBackup != "" && !txn.backupDiscarded { + os.RemoveAll(txn.installBackup) + txn.backupDiscarded = true + } + } +} diff --git a/src/installer/reshim_test.go b/src/installer/reshim_test.go index 98e2d49..230f7d5 100644 --- a/src/installer/reshim_test.go +++ b/src/installer/reshim_test.go @@ -1,6 +1,7 @@ package installer import ( + "fmt" "os" "os/exec" "path/filepath" @@ -155,3 +156,48 @@ func TestReshimWrapsUtilityFailure(t *testing.T) { t.Fatalf("reshim() error = %q, want wrapped exit status 17", got) } } + +func TestFinalizeInstallShimsRollsBackOnReshimFailure(t *testing.T) { + setupReshimTestProfile(t) + + installDir := filepath.Join(t.TempDir(), "v22.0.0") + if err := os.MkdirAll(installDir, 0o755); err != nil { + t.Fatalf("MkdirAll(%q) error = %v", installDir, err) + } + marker := filepath.Join(installDir, "node.exe") + if err := os.WriteFile(marker, []byte("node"), 0o644); err != nil { + t.Fatalf("WriteFile(%q) error = %v", marker, err) + } + + orig := runReshim + t.Cleanup(func() { runReshim = orig }) + runReshim = func() error { + return fmt.Errorf("reshim failed: exit status 1") + } + + status := newStatus() + status.TotalInstalled = 1 + status.Versions = []string{"22.0.0"} + status.running = true + + txn := &Transaction{ + version: "22.0.0", + installDir: installDir, + installedNew: true, + installed: false, + } + + err := finalizeInstallShims([]*Transaction{txn}, status, false, true) + if err == nil { + t.Fatal("finalizeInstallShims error = nil, want reshim failure") + } + if got := err.Error(); !strings.Contains(got, "reshim failed") { + t.Fatalf("finalizeInstallShims error = %q, want reshim failed", got) + } + if _, statErr := os.Stat(installDir); !os.IsNotExist(statErr) { + t.Fatalf("install dir %q still exists after reshim failure rollback", installDir) + } + if status.cancelMessage == "" { + t.Fatal("status.cancelMessage empty, want rollback cancel message") + } +} diff --git a/src/link/link.go b/src/link/link.go index 70dd18f..7a55849 100644 --- a/src/link/link.go +++ b/src/link/link.go @@ -87,9 +87,7 @@ func createSymlink(source, symlinkPath string) error { if no_devmode { // Critical setup toast: no collection (reserved nvm.setup). - notify.Send(settings.AppId, "Help Enabling Symlinks", "Developer Mode is required to use symlinks.", - notify.Action{Label: "Open Developer Settings", URL: "ms-settings:developers"}, - ) + _ = notify.HelpEnablingSymlinks(settings.AppId) } log.Errorf("permission denied creating symlink (developer mode %v): %v", val, err)