From e34bb923bb6d4ec677446a1d0de754d49af22d36 Mon Sep 17 00:00:00 2001 From: coreybutler <770982+coreybutler@users.noreply.github.com> Date: Thu, 5 Nov 2026 12:10:49 -0600 Subject: [PATCH] fix: pass scoped globals to npm with forward slashes nvm install --from rebuilt scoped package names via filepath.Join, so Windows produced @scope\name and npm treated it as a local path (ENOENT). Use npm's @scope/name form instead. Fixes nvm-windows/nvm#1537 Signed-off-by: coreybutler <770982+coreybutler@users.noreply.github.com> --- src/installer/npm.go | 12 ++++++--- src/installer/npm_modules_test.go | 45 +++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 src/installer/npm_modules_test.go diff --git a/src/installer/npm.go b/src/installer/npm.go index 7ce23a3..0e80503 100644 --- a/src/installer/npm.go +++ b/src/installer/npm.go @@ -169,7 +169,10 @@ func npmInstall( } func getModuleList(version string) (map[string]string, error) { - root := filepath.Join(getRoot(version), "node_modules") + return listModulesAt(filepath.Join(getRoot(version), "node_modules")) +} + +func listModulesAt(root string) (map[string]string, error) { entries, err := os.ReadDir(root) if err != nil { return nil, err @@ -191,8 +194,11 @@ func getModuleList(version string) (map[string]string, error) { } for _, s := range scoped { if s.IsDir() { - modName := filepath.Join(name, s.Name()) - modules[modName] = filepath.Join(root, modName) + // npm package names always use '/', even on Windows. + // filepath.Join would produce `@scope\name`, which npm + // treats as a local path (breaks `nvm install --from`). + modName := name + "/" + s.Name() + modules[modName] = filepath.Join(root, name, s.Name()) } } continue diff --git a/src/installer/npm_modules_test.go b/src/installer/npm_modules_test.go new file mode 100644 index 0000000..0501f50 --- /dev/null +++ b/src/installer/npm_modules_test.go @@ -0,0 +1,45 @@ +package installer + +import ( + "os" + "path/filepath" + "runtime" + "testing" +) + +func TestListModulesAtScopedNamesUseSlash(t *testing.T) { + root := t.TempDir() + mustMkdir := func(parts ...string) { + t.Helper() + if err := os.MkdirAll(filepath.Join(append([]string{root}, parts...)...), 0o755); err != nil { + t.Fatalf("mkdir: %v", err) + } + } + + mustMkdir("left-pad") + mustMkdir("@opencode", "cli") + mustMkdir(".bin") + + modules, err := listModulesAt(root) + if err != nil { + t.Fatalf("listModulesAt: %v", err) + } + + if _, ok := modules["left-pad"]; !ok { + t.Fatalf("missing unscoped module; got %#v", modules) + } + if _, ok := modules["@opencode/cli"]; !ok { + t.Fatalf("missing scoped module with slash name; got %#v", modules) + } + if _, ok := modules[filepath.Join("@opencode", "cli")]; ok && runtime.GOOS == "windows" { + t.Fatalf("scoped module must not use OS separators as npm name; got %#v", modules) + } + if _, ok := modules[".bin"]; ok { + t.Fatalf(".bin must be skipped; got %#v", modules) + } + + wantPath := filepath.Join(root, "@opencode", "cli") + if got := modules["@opencode/cli"]; got != wantPath { + t.Fatalf("scoped path = %q, want %q", got, wantPath) + } +}