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
12 changes: 9 additions & 3 deletions src/installer/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
45 changes: 45 additions & 0 deletions src/installer/npm_modules_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading