Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/installer/activation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 2 additions & 16 deletions src/installer/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
66 changes: 65 additions & 1 deletion src/installer/reshim.go
Original file line number Diff line number Diff line change
@@ -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
}
}
}
46 changes: 46 additions & 0 deletions src/installer/reshim_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package installer

import (
"fmt"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -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")
}
}
4 changes: 1 addition & 3 deletions src/link/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading