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
39 changes: 24 additions & 15 deletions src/installer/reshim.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"common/settings"
"fmt"
"os"
"strings"

"nvm/bootstrap"
)
Expand All @@ -21,30 +22,33 @@ 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
1) Enable Developer Mode (symlink privilege), then:
nvm reshim --force
https://docs.nvm-windows.com/features/modes
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.
// finalizeInstallShims runs reshim after version installs in shim mode,
// discarding backups only on success. On failure it rolls back newly installed
// dirs and hard-fails. Link mode skips reshim here — ActivateVersion points
// .nodejs at .link/nodejs and does not require package-manager proxy shims.
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)
if operatingInShimMode() {
// 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))
}
return status.Abort(fmt.Errorf("reshim failed: %w", err))
}

discardInstallBackups(txns)
Expand All @@ -61,6 +65,11 @@ func finalizeInstallShims(txns []*Transaction, status *Status, modulesInstalled
return nil
}

func operatingInShimMode() bool {
mode := strings.TrimSpace(strings.ToLower(settings.Global().Mode))
return mode == "" || mode == "shim"
}

func discardInstallBackups(txns []*Transaction) {
for _, txn := range txns {
if txn != nil && txn.installBackup != "" && !txn.backupDiscarded {
Expand Down
47 changes: 47 additions & 0 deletions src/installer/reshim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ func TestReshimWrapsUtilityFailure(t *testing.T) {

func TestFinalizeInstallShimsRollsBackOnReshimFailure(t *testing.T) {
setupReshimTestProfile(t)
if err := settings.Put("mode", "shim"); err != nil {
t.Fatalf("Put(mode) error = %v", err)
}
settings.Load(true)

installDir := filepath.Join(t.TempDir(), "v22.0.0")
if err := os.MkdirAll(installDir, 0o755); err != nil {
Expand Down Expand Up @@ -201,3 +205,46 @@ func TestFinalizeInstallShimsRollsBackOnReshimFailure(t *testing.T) {
t.Fatal("status.cancelMessage empty, want rollback cancel message")
}
}

func TestFinalizeInstallShimsSkipsReshimInLinkMode(t *testing.T) {
setupReshimTestProfile(t)
if err := settings.Put("mode", "link"); err != nil {
t.Fatalf("Put(mode) error = %v", err)
}
settings.Load(true)

installDir := filepath.Join(t.TempDir(), "v22.0.0")
if err := os.MkdirAll(installDir, 0o755); err != nil {
t.Fatalf("MkdirAll(%q) error = %v", installDir, err)
}

orig := runReshim
t.Cleanup(func() { runReshim = orig })
called := false
runReshim = func() error {
called = true
return fmt.Errorf("reshim should not run in link mode")
}

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,
}

if err := finalizeInstallShims([]*Transaction{txn}, status, false, true); err != nil {
t.Fatalf("finalizeInstallShims error = %v, want nil in link mode", err)
}
if called {
t.Fatal("runReshim was called in link mode")
}
if _, err := os.Stat(installDir); err != nil {
t.Fatalf("install dir should remain after link-mode finalize: %v", err)
}
}
Loading