Skip to content

Commit 6b06ce2

Browse files
G-Rathjulieqiu
authored andcommitted
fix: avoid panic when parsing file: dependencies in pnpm lockfiles (google#259)
Resolves google#256
1 parent 8f5594e commit 6b06ce2

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

pkg/lockfile/fixtures/pnpm/files.yaml

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
lockfileVersion: 5.3
1+
lockfileVersion: 5.4
22

33
specifiers:
44
my-file-package: file:./projects/package-a.tgz
@@ -18,3 +18,30 @@ packages:
1818
name: a-local-package
1919
version: 1.0.0
2020
dev: false
21+
22+
file:../a-local-package/nested:
23+
resolution: {directory: ../a-local-package/nested, type: directory}
24+
name: a-nested-local-package
25+
version: 1.0.0
26+
dev: false
27+
28+
file:..:
29+
resolution: {directory: .., type: directory}
30+
name: one-up
31+
version: 1.0.0
32+
dev: false
33+
34+
35+
resolution: {directory: .., type: directory}
36+
name: one-up-with-peer
37+
version: 1.0.0
38+
dev: false
39+
peerDependencies:
40+
react-dom: ^18.0.0
41+
42+
# file based dependencies must always have a name so this is impossible,
43+
# but we want to ensure we don't panic just in case
44+
file:../nameless-package:
45+
resolution: {directory: ../nameless-package, type: directory}
46+
version: 1.0.0
47+
dev: false

pkg/lockfile/parse-pnpm-lock.go

+7
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ func startsWithNumber(str string) bool {
3838
// extractPnpmPackageNameAndVersion parses a dependency path, attempting to
3939
// extract the name and version of the package it represents
4040
func extractPnpmPackageNameAndVersion(dependencyPath string) (string, string) {
41+
// file dependencies must always have a name property to be installed,
42+
// and their dependency path never has the version encoded, so we can
43+
// skip trying to extract either from their dependency path
44+
if strings.HasPrefix(dependencyPath, "file:") {
45+
return "", ""
46+
}
47+
4148
parts := strings.Split(dependencyPath, "/")
4249
var name string
4350

pkg/lockfile/parse-pnpm-lock_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -459,5 +459,26 @@ func TestParsePnpmLock_Files(t *testing.T) {
459459
CompareAs: lockfile.NpmEcosystem,
460460
Commit: "",
461461
},
462+
{
463+
Name: "a-nested-local-package",
464+
Version: "1.0.0",
465+
Ecosystem: lockfile.NpmEcosystem,
466+
CompareAs: lockfile.NpmEcosystem,
467+
Commit: "",
468+
},
469+
{
470+
Name: "one-up",
471+
Version: "1.0.0",
472+
Ecosystem: lockfile.NpmEcosystem,
473+
CompareAs: lockfile.NpmEcosystem,
474+
Commit: "",
475+
},
476+
{
477+
Name: "one-up-with-peer",
478+
Version: "1.0.0",
479+
Ecosystem: lockfile.NpmEcosystem,
480+
CompareAs: lockfile.NpmEcosystem,
481+
Commit: "",
482+
},
462483
})
463484
}

0 commit comments

Comments
 (0)