Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid panic when parsing file: dependencies in pnpm lockfiles #171

Merged
merged 1 commit into from
Mar 3, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 28 additions & 1 deletion pkg/lockfile/fixtures/pnpm/files.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
lockfileVersion: 5.3
lockfileVersion: 5.4

specifiers:
my-file-package: file:./projects/package-a.tgz
Expand All @@ -18,3 +18,30 @@ packages:
name: a-local-package
version: 1.0.0
dev: false

file:../a-local-package/nested:
resolution: {directory: ../a-local-package/nested, type: directory}
name: a-nested-local-package
version: 1.0.0
dev: false

file:..:
resolution: {directory: .., type: directory}
name: one-up
version: 1.0.0
dev: false

file:.._react-dom@18.2.0:
resolution: {directory: .., type: directory}
name: one-up-with-peer
version: 1.0.0
dev: false
peerDependencies:
react-dom: ^18.0.0

# file based dependencies must always have a name so this is impossible,
# but we want to ensure we don't panic just in case
file:../nameless-package:
resolution: {directory: ../nameless-package, type: directory}
version: 1.0.0
dev: false
7 changes: 7 additions & 0 deletions pkg/lockfile/parse-pnpm-lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ func startsWithNumber(str string) bool {
// extractPnpmPackageNameAndVersion parses a dependency path, attempting to
// extract the name and version of the package it represents
func extractPnpmPackageNameAndVersion(dependencyPath string) (string, string) {
// file dependencies must always have a name property to be installed,
// and their dependency path never has the version encoded, so we can
// skip trying to extract either from their dependency path
if strings.HasPrefix(dependencyPath, "file:") {
return "", ""
}

parts := strings.Split(dependencyPath, "/")
var name string

Expand Down
21 changes: 21 additions & 0 deletions pkg/lockfile/parse-pnpm-lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,5 +459,26 @@ func TestParsePnpmLock_Files(t *testing.T) {
CompareAs: lockfile.NpmEcosystem,
Commit: "",
},
{
Name: "a-nested-local-package",
Version: "1.0.0",
Ecosystem: lockfile.NpmEcosystem,
CompareAs: lockfile.NpmEcosystem,
Commit: "",
},
{
Name: "one-up",
Version: "1.0.0",
Ecosystem: lockfile.NpmEcosystem,
CompareAs: lockfile.NpmEcosystem,
Commit: "",
},
{
Name: "one-up-with-peer",
Version: "1.0.0",
Ecosystem: lockfile.NpmEcosystem,
CompareAs: lockfile.NpmEcosystem,
Commit: "",
},
})
}