From 8f877186b0b0072ae6b7ec2673a955398cea8259 Mon Sep 17 00:00:00 2001 From: sebasidian sunday <292981763+sundaysebasidian-byte@users.noreply.github.com> Date: Tue, 17 Nov 2026 09:40:09 -0800 Subject: [PATCH] Preserve npm workspace links as path sources --- README.md | 8 +++++ internal/npm/npm.go | 14 +++++++-- internal/npm/npm_test.go | 7 ++--- npm_workspace_test.go | 66 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 npm_workspace_test.go diff --git a/README.md b/README.md index 7540dfb..0b558fe 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,14 @@ func main() { | vagrant | Vagrantfile | | | vcpkg | vcpkg.json | | +### npm workspace links + +In v2/v3 `package-lock.json` and `npm-shrinkwrap.json`, linked packages retain +their literal `resolved` path in `Source` with kind `path`, not in `RegistryURL`. +Their version remains empty when the link entry has no version. Workspace +target entries are not reported as separate dependencies; packages installed +under a workspace's `node_modules` directory are still included. + ### Swift output Swift manifests are read without executing Swift, so variables, conditions, diff --git a/internal/npm/npm.go b/internal/npm/npm.go index 2d60ced..073f12f 100644 --- a/internal/npm/npm.go +++ b/internal/npm/npm.go @@ -362,6 +362,11 @@ func (e *packageLockEntry) hasContent() bool { } func (e *packageLockEntry) toDependency(directDependencies map[string]bool) (core.Dependency, bool) { + // Workspace targets describe local packages, not additional installations. + // Their own node_modules entries still represent installed dependencies. + if !strings.HasPrefix(e.Path, "node_modules/") && !strings.Contains(e.Path, "/node_modules/") { + return core.Dependency{}, false + } name := extractPackageName(e.Path) if name == "" || (e.Version == "" && !e.Link) { return core.Dependency{}, false @@ -374,14 +379,19 @@ func (e *packageLockEntry) toDependency(directDependencies map[string]bool) (cor } topLevel := !strings.Contains(strings.TrimPrefix(e.Path, "node_modules/"), "node_modules/") direct := topLevel && directDependencies[name] - return core.Dependency{ + dep := core.Dependency{ Name: name, Version: e.Version, Scope: scope, Integrity: e.Integrity, Direct: direct, RegistryURL: e.Resolved, - }, true + } + if e.Link { + dep.Source = core.Source{Kind: core.SourcePath, Value: e.Resolved} + dep.RegistryURL = "" + } + return dep, true } func parsePackageLockDirectDependencies(content []byte) map[string]bool { diff --git a/internal/npm/npm_test.go b/internal/npm/npm_test.go index 29ea1c6..6b0c39d 100644 --- a/internal/npm/npm_test.go +++ b/internal/npm/npm_test.go @@ -621,8 +621,8 @@ func TestNpmLocalFilePackageLock(t *testing.T) { t.Fatalf("Parse failed: %v", err) } - if len(res.Dependencies) != 7 { - t.Fatalf("expected 7 dependencies, got %d", len(res.Dependencies)) + if len(res.Dependencies) != 6 { + t.Fatalf("expected 6 dependencies, got %d", len(res.Dependencies)) } depMap := make(map[string]core.Dependency) @@ -630,10 +630,9 @@ func TestNpmLocalFilePackageLock(t *testing.T) { depMap[d.Name] = d } - // All 7 packages + // The local target src/other-package is not a separate installed package. expected := map[string]string{ "react": "18.3.1", - "src": "1.0.0", "js-tokens": "4.0.0", "left-pad": "1.3.0", "lodash": "4.17.21", diff --git a/npm_workspace_test.go b/npm_workspace_test.go new file mode 100644 index 0000000..3a58d21 --- /dev/null +++ b/npm_workspace_test.go @@ -0,0 +1,66 @@ +package manifests + +import ( + "bytes" + "encoding/json" + "fmt" + "reflect" + "testing" +) + +func TestNPMWorkspaceLinks(t *testing.T) { + for _, version := range []int{2, 3} { + content := []byte(fmt.Sprintf(`{ + "lockfileVersion": %d, + "packages": { + "": { + "dependencies": {"workspace-lib": "file:packages/workspace-lib", "registry-lib": "^2.0.0"}, + "devDependencies": {"@scope/local": "file:packages/local"} + }, + "node_modules/workspace-lib": {"resolved": "packages/workspace-lib", "link": true}, + "node_modules/@scope/local": {"resolved": "packages/local", "link": true, "dev": true}, + "node_modules/external": {"resolved": "../external", "link": true, "optional": true}, + "node_modules/registry-lib": {"version": "2.1.0", "resolved": "https://registry.npmjs.org/registry-lib/-/registry-lib-2.1.0.tgz", "integrity": "sha512-synthetic"}, + "packages/workspace-lib": {"version": "1.0.0"}, + "packages/local": {"version": "3.0.0"}, + "packages/some_node_modules/not-installed": {"version": "6.0.0"}, + "../external": {"version": "4.0.0"}, + "packages/workspace-lib/node_modules/nested": {"version": "5.0.0", "resolved": "https://registry.npmjs.org/nested/-/nested-5.0.0.tgz"} + } +}`, version)) + var compact, indented bytes.Buffer + if err := json.Compact(&compact, content); err != nil { + t.Fatal(err) + } + if err := json.Indent(&indented, compact.Bytes(), "", " "); err != nil { + t.Fatal(err) + } + for _, filename := range []string{"package-lock.json", "npm-shrinkwrap.json"} { + for format, input := range map[string][]byte{"compact": compact.Bytes(), "indented": indented.Bytes()} { + t.Run(fmt.Sprintf("v%d/%s/%s", version, filename, format), func(t *testing.T) { + result, err := Parse(filename, input) + if err != nil { + t.Fatal(err) + } + got := make(map[string]Dependency) + for _, dep := range result.Dependencies { + if _, duplicate := got[dep.Name]; duplicate { + t.Errorf("duplicate dependency %q", dep.Name) + } + got[dep.Name] = dep + } + want := map[string]Dependency{ + "workspace-lib": {Name: "workspace-lib", Scope: Runtime, Direct: true, Ecosystem: "npm", PURL: "pkg:npm/workspace-lib", Source: Source{Kind: SourcePath, Value: "packages/workspace-lib"}}, + "@scope/local": {Name: "@scope/local", Scope: Development, Direct: true, Ecosystem: "npm", PURL: "pkg:npm/%40scope/local", Source: Source{Kind: SourcePath, Value: "packages/local"}}, + "external": {Name: "external", Scope: Optional, Ecosystem: "npm", PURL: "pkg:npm/external", Source: Source{Kind: SourcePath, Value: "../external"}}, + "registry-lib": {Name: "registry-lib", Version: "2.1.0", Scope: Runtime, Direct: true, Ecosystem: "npm", PURL: "pkg:npm/registry-lib@2.1.0", RegistryURL: "https://registry.npmjs.org/registry-lib/-/registry-lib-2.1.0.tgz", Integrity: "sha512-synthetic"}, + "nested": {Name: "nested", Version: "5.0.0", Scope: Runtime, Ecosystem: "npm", PURL: "pkg:npm/nested@5.0.0", RegistryURL: "https://registry.npmjs.org/nested/-/nested-5.0.0.tgz"}, + } + if !reflect.DeepEqual(got, want) { + t.Errorf("dependencies = %#v\nwant %#v", got, want) + } + }) + } + } + } +}