Skip to content

Commit 2638a71

Browse files
committed
npm: support aliases in yarn lock v1
When parsing a `yarn.lock v1` file, ScanCode does not handle aliases properly. Aliases have the form: `<alias-package>@npm:<package>` More info about aliases: https://classic.yarnpkg.com/lang/en/docs/cli/add/#toc-yarn-add-alias The current code incorrecly parse aliases and end up throwing an exception, resulting in empty results. A single alias in a yarn.lock file is gonna make the parser return 0 packages. This patch adds the logic to handle them by simply dropping the alias part, and just keeping the package part. Test plan: updated the unit tests. Signed-off-by: Adrien Schildknecht <[email protected]>
1 parent d6a9e41 commit 2638a71

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

src/packagedcode/npm.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,10 @@ def parse(cls, location):
632632
ns_name, _, constraint = req.rpartition('@')
633633
ns, _ , name = ns_name.rpartition('/')
634634
constraint = constraint.strip("\"'")
635+
# If we have an alias, just keep the package part:
636+
# <alias-package>@npm:<package>
637+
if "@npm:" in ns:
638+
ns = ns.split(':')[1]
635639
top_requirements.append((ns, name, constraint,))
636640

637641
else:

tests/packagedcode/data/npm/yarn-lock/v1-complex/yarn.lock

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,12 @@
2525
integrity sha512-3UYcJUj9kvSLbLbUIfQTqzcy5VX7GRZ/CCDrnOaZorFFM01aXp1+GJwuFGV4NDDoAS+mOUyHcO6UD/RfqOks3Q==
2626
dependencies:
2727
"@babel/types" "^7.0.0"
28+
29+
"@react-spring/[email protected]", "react-spring@npm:@react-spring/[email protected]":
30+
version "9.7.3"
31+
resolved "https://registry.yarnpkg.com/@react-spring/web/-/web-9.7.3.tgz#da977382f91d9af4c400e4aa7dc37d3db07b87e0"
32+
integrity sha512-rEvipblmihiz8+Eo01zDp5dqWn6XfYk8q2rlN9c18YIOL4o6nuY/VplDoocUMHYfH4liurpO4o1QudKOO1nAiQ==
33+
dependencies:
34+
"@react-spring/animated" "9.7.3"
35+
"@react-spring/core" "9.7.3"
36+
"@react-spring/shared" "9.7.3"

tests/packagedcode/data/npm/yarn-lock/v1-complex/yarn.lock-expected

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,89 @@
250250
"purl": "pkg:npm/%40babel/[email protected]"
251251
},
252252
"extra_data": {}
253+
},
254+
{
255+
"purl": "pkg:npm/%40react-spring/[email protected]",
256+
"extracted_requirement": "9.7.3 9.7.3",
257+
"scope": "dependencies",
258+
"is_runtime": true,
259+
"is_optional": false,
260+
"is_resolved": true,
261+
"resolved_package": {
262+
"type": "npm",
263+
"namespace": "@react-spring",
264+
"name": "web",
265+
"version": "9.7.3",
266+
"qualifiers": {},
267+
"subpath": null,
268+
"primary_language": "JavaScript",
269+
"description": null,
270+
"release_date": null,
271+
"parties": [],
272+
"keywords": [],
273+
"homepage_url": null,
274+
"download_url": "https://registry.yarnpkg.com/@react-spring/web/-/web-9.7.3.tgz",
275+
"size": null,
276+
"sha1": "da977382f91d9af4c400e4aa7dc37d3db07b87e0",
277+
"md5": null,
278+
"sha256": null,
279+
"sha512":
280+
"ac4be2a5b9668a18b3f3e128d35cc3a7976a5a7e977d893cab6ae537d735f1820e2f8a3a9ee63f569943a2871430761f1f8962baba4ee28d50b9d28e3b59c089",
281+
"bug_tracking_url": null,
282+
"code_view_url": null,
283+
"vcs_url": null,
284+
"copyright": null,
285+
"holder": null,
286+
"declared_license_expression": null,
287+
"declared_license_expression_spdx": null,
288+
"license_detections": [],
289+
"other_license_expression": null,
290+
"other_license_expression_spdx": null,
291+
"other_license_detections": [],
292+
"extracted_license_statement": null,
293+
"notice_text": null,
294+
"source_packages": [],
295+
"file_references": [],
296+
"extra_data": {},
297+
"dependencies": [
298+
{
299+
"purl": "pkg:npm/%22%40react-spring/animated%22",
300+
"extracted_requirement": "9.7.3",
301+
"scope": "dependencies",
302+
"is_runtime": true,
303+
"is_optional": false,
304+
"is_resolved": false,
305+
"resolved_package": {},
306+
"extra_data": {}
307+
},
308+
{
309+
"purl": "pkg:npm/%22%40react-spring/core%22",
310+
"extracted_requirement": "9.7.3",
311+
"scope": "dependencies",
312+
"is_runtime": true,
313+
"is_optional": false,
314+
"is_resolved": false,
315+
"resolved_package": {},
316+
"extra_data": {}
317+
},
318+
{
319+
"purl": "pkg:npm/%22%40react-spring/shared%22",
320+
"extracted_requirement": "9.7.3",
321+
"scope": "dependencies",
322+
"is_runtime": true,
323+
"is_optional": false,
324+
"is_resolved": false,
325+
"resolved_package": {},
326+
"extra_data": {}
327+
}
328+
],
329+
"repository_homepage_url": "https://www.npmjs.com/package/@react-spring/web",
330+
"repository_download_url": "https://registry.npmjs.org/@react-spring/web/-/web-9.7.3.tgz",
331+
"api_data_url": "https://registry.npmjs.org/@react-spring%2fweb/9.7.3",
332+
"datasource_id": "yarn_lock_v1",
333+
"purl": "pkg:npm/%40react-spring/[email protected]"
334+
},
335+
"extra_data": {}
253336
}
254337
],
255338
"repository_homepage_url": null,
@@ -258,4 +341,4 @@
258341
"datasource_id": "yarn_lock_v1",
259342
"purl": null
260343
}
261-
]
344+
]

0 commit comments

Comments
 (0)