Skip to content

Commit

Permalink
fix: handle yarn aliased packages (#615)
Browse files Browse the repository at this point in the history
Resolves #588
Follows up #610

Because of how PNPM structures its lockfile, aliases are already
supported there
  • Loading branch information
G-Rath committed Oct 30, 2023
1 parent ffa5425 commit 419a945
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 1 deletion.
18 changes: 18 additions & 0 deletions pkg/lockfile/fixtures/yarn/with-aliases.v1.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"ansi-regex-cjs@npm:ansi-regex@^5.0.0":
version "5.0.1"
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-5.0.1.tgz#082cb2c89c9fe8659a311a53bd6a4dc5301db304"
integrity sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==

ansi-regex@^6.0.0:
version "6.0.1"
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-6.0.1.tgz#3183e38fae9a65d7cb5e53945cd5897d0260a06a"
integrity sha512-n5M855fKb2SsfMIiFFoVrABHJC8QtHwVx+mHWP3QcEqBHYienj5dHSgjbxtC0WEZXYt4wcD6zrQElDPhFuZgfA==

"babel-hvi@npm:@babel/helper-validator-identifier@^7.0.0":
version "7.22.20"
resolved "https://registry.yarnpkg.com/@babel/helper-validator-identifier/-/helper-validator-identifier-7.22.20.tgz#c4ae002c61d2879e724581d96665583dbc1dc0e0"
integrity sha512-Y4OZ+ytlatR8AI+8KZfKuL5urKp7qey08ha31L8b3BwewJAoJamTzyvxPR/5D+KkdJCGPq/+8TukHBlY10FX9A==
37 changes: 37 additions & 0 deletions pkg/lockfile/fixtures/yarn/with-aliases.v2.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This file is generated by running "yarn install" inside your project.
# Manual changes might be lost - proceed with caution!

__metadata:
version: 8
cacheKey: 10c0

"ansi-regex-cjs@npm:ansi-regex@^5.0.0":
version: 5.0.1
resolution: "ansi-regex@npm:5.0.1"
checksum: 9a64bb8627b434ba9327b60c027742e5d17ac69277960d041898596271d992d4d52ba7267a63ca10232e29f6107fc8a835f6ce8d719b88c5f8493f8254813737
languageName: node
linkType: hard

"ansi-regex@npm:^6.0.0":
version: 6.0.1
resolution: "ansi-regex@npm:6.0.1"
checksum: cbe16dbd2c6b2735d1df7976a7070dd277326434f0212f43abf6d87674095d247968209babdaad31bb00882fa68807256ba9be340eec2f1004de14ca75f52a08
languageName: node
linkType: hard

"babel-hvi@npm:@babel/helper-validator-identifier@^7.0.0":
version: 7.22.20
resolution: "@babel/helper-validator-identifier@npm:7.22.20"
checksum: dcad63db345fb110e032de46c3688384b0008a42a4845180ce7cd62b1a9c0507a1bed727c4d1060ed1a03ae57b4d918570259f81724aaac1a5b776056f37504e
languageName: node
linkType: hard

"mine@workspace:.":
version: 0.0.0-use.local
resolution: "mine@workspace:."
dependencies:
ansi-regex: "npm:^6.0.0"
ansi-regex-cjs: "npm:ansi-regex@^5.0.0"
babel-hvi: "npm:@babel/helper-validator-identifier@^7.0.0"
languageName: unknown
linkType: soft
31 changes: 31 additions & 0 deletions pkg/lockfile/parse-yarn-lock-v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,3 +444,34 @@ func TestParseYarnLock_v1_Files(t *testing.T) {
},
})
}

func TestParseYarnLock_v1_WithAliases(t *testing.T) {
t.Parallel()

packages, err := lockfile.ParseYarnLock("fixtures/yarn/with-aliases.v1.lock")

if err != nil {
t.Errorf("Got unexpected error: %v", err)
}

expectPackages(t, packages, []lockfile.PackageDetails{
{
Name: "@babel/helper-validator-identifier",
Version: "7.22.20",
Ecosystem: lockfile.YarnEcosystem,
CompareAs: lockfile.YarnEcosystem,
},
{
Name: "ansi-regex",
Version: "6.0.1",
Ecosystem: lockfile.YarnEcosystem,
CompareAs: lockfile.YarnEcosystem,
},
{
Name: "ansi-regex",
Version: "5.0.1",
Ecosystem: lockfile.YarnEcosystem,
CompareAs: lockfile.YarnEcosystem,
},
})
}
37 changes: 37 additions & 0 deletions pkg/lockfile/parse-yarn-lock-v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,40 @@ func TestParseYarnLock_v2_Files(t *testing.T) {
},
})
}

func TestParseYarnLock_v2_WithAliases(t *testing.T) {
t.Parallel()

packages, err := lockfile.ParseYarnLock("fixtures/yarn/with-aliases.v2.lock")

if err != nil {
t.Errorf("Got unexpected error: %v", err)
}

expectPackages(t, packages, []lockfile.PackageDetails{
{
Name: "@babel/helper-validator-identifier",
Version: "7.22.20",
Ecosystem: lockfile.YarnEcosystem,
CompareAs: lockfile.YarnEcosystem,
},
{
Name: "ansi-regex",
Version: "6.0.1",
Ecosystem: lockfile.YarnEcosystem,
CompareAs: lockfile.YarnEcosystem,
},
{
Name: "ansi-regex",
Version: "5.0.1",
Ecosystem: lockfile.YarnEcosystem,
CompareAs: lockfile.YarnEcosystem,
},
{
Name: "mine",
Version: "0.0.0-use.local",
Ecosystem: lockfile.YarnEcosystem,
CompareAs: lockfile.YarnEcosystem,
},
})
}
7 changes: 6 additions & 1 deletion pkg/lockfile/parse-yarn-lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,19 @@ func groupYarnPackageLines(scanner *bufio.Scanner) [][]string {

func extractYarnPackageName(str string) string {
str = strings.TrimPrefix(str, "\"")
str, _, _ = strings.Cut(str, ",")

isScoped := strings.HasPrefix(str, "@")

if isScoped {
str = strings.TrimPrefix(str, "@")
}

name := strings.SplitN(str, "@", 2)[0]
name, right, _ := strings.Cut(str, "@")

if strings.HasPrefix(right, "npm:") && strings.Contains(right, "@") {
return extractYarnPackageName(strings.TrimPrefix(right, "npm:"))
}

if isScoped {
name = "@" + name
Expand Down

0 comments on commit 419a945

Please sign in to comment.