Skip to content
Draft
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 12 additions & 2 deletions internal/npm/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
7 changes: 3 additions & 4 deletions internal/npm/npm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -621,19 +621,18 @@ 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)
for _, d := range res.Dependencies {
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",
Expand Down
66 changes: 66 additions & 0 deletions npm_workspace_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
}
}