Skip to content

Commit 762078c

Browse files
committed
Report package in npm dep requirement #3931
Signed-off-by: Jono Yang <[email protected]>
1 parent e4318f4 commit 762078c

File tree

4 files changed

+33
-18
lines changed

4 files changed

+33
-18
lines changed

src/packagedcode/npm.py

+25-10
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,18 @@ def update_dependencies_by_purl(
376376
if '_' in metadata:
377377
requirement, _extra = metadata.split('_')
378378

379+
if ':' in requirement and '@' in requirement:
380+
# dependencies with requirements like this are aliases and should be reported
381+
aliased_package, _, constraint = requirement.rpartition('@')
382+
_, _, aliased_package_name = aliased_package.rpartition(':')
383+
sdns, _ , sdname = aliased_package_name.rpartition('/')
384+
dep_purl = PackageURL(
385+
type=cls.default_package_type,
386+
namespace=sdns,
387+
name=sdname
388+
).to_string()
389+
requirement = constraint
390+
379391
dep_package = models.DependentPackage(
380392
purl=dep_purl,
381393
scope=scope,
@@ -1014,13 +1026,12 @@ def parse(cls, location, package_only=False):
10141026
ns_name = ns_name.replace('"', '')
10151027
ns, _ , name = ns_name.rpartition('/')
10161028

1017-
# sometimes constraints appear in the form of
1018-
# wrap-ansi-cjs "npm:wrap-ansi@^7.0.0"
1019-
if '@' in constraint:
1020-
# "npm:wrap-ansi" should be appended to `name`, joined
1021-
# with an "@"
1022-
constraint_package, _, constraint = constraint.partition('@')
1023-
name = f'{name}@{constraint_package}'
1029+
if ':' in constraint and '@' in constraint:
1030+
# dependencies with requirements like this are aliases and should be reported
1031+
aliased_package, _, constraint = constraint.rpartition('@')
1032+
_, _, aliased_package_name = aliased_package.rpartition(':')
1033+
ns, _ , name = aliased_package_name.rpartition('/')
1034+
10241035
sub_dependencies.append((ns, name, constraint,))
10251036

10261037
elif line.startswith(' ' * 2):
@@ -1780,9 +1791,13 @@ def deps_mapper(deps, package, field_name, is_direct=True):
17801791
ns, name = split_scoped_package_name(fqname)
17811792
if not name:
17821793
continue
1783-
if '@' in requirement:
1784-
requirement_package, _, requirement = requirement.partition('@')
1785-
name = f'{name}@{requirement_package}'
1794+
1795+
if ':' in requirement and '@' in requirement:
1796+
# dependencies with requirements like this are aliases and should be reported
1797+
aliased_package, _, requirement = requirement.rpartition('@')
1798+
_, _, aliased_package_name = aliased_package.rpartition(':')
1799+
ns, _ , name = aliased_package_name.rpartition('/')
1800+
17861801
purl = PackageURL(type='npm', namespace=ns, name=name).to_string()
17871802

17881803
# optionalDependencies override the dependencies with the same name

tests/packagedcode/data/npm/special_extracted_requirements/package.json.expected

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
},
6969
"dependencies": [
7070
{
71-
"purl": "pkg:npm/strip-ansi-cjs%40npm:strip-ansi",
71+
"purl": "pkg:npm/strip-ansi",
7272
"extracted_requirement": "^6.0.1",
7373
"scope": "devDependencies",
7474
"is_runtime": false,

tests/packagedcode/data/npm/workspace/crystal.expected.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -9960,16 +9960,16 @@
99609960
"datasource_id": "npm_package_json"
99619961
},
99629962
{
9963-
"purl": "pkg:npm/%40localrepo/prettier2-for-jest",
9964-
"extracted_requirement": "npm:prettier@^2",
9963+
"purl": "pkg:npm/prettier",
9964+
"extracted_requirement": "^2",
99659965
"scope": "devDependencies",
99669966
"is_runtime": false,
99679967
"is_optional": true,
99689968
"is_resolved": false,
99699969
"is_direct": true,
99709970
"resolved_package": {},
99719971
"extra_data": {},
9972-
"dependency_uid": "pkg:npm/%40localrepo/prettier2-for-jest?uuid=fixed-uid-done-for-testing-5642512d1758",
9972+
"dependency_uid": "pkg:npm/prettier?uuid=fixed-uid-done-for-testing-5642512d1758",
99739973
"for_package_uid": null,
99749974
"datafile_path": "crystal/package.json",
99759975
"datasource_id": "npm_package_json"
@@ -18249,8 +18249,8 @@
1824918249
"extra_data": {}
1825018250
},
1825118251
{
18252-
"purl": "pkg:npm/%40localrepo/prettier2-for-jest",
18253-
"extracted_requirement": "npm:prettier@^2",
18252+
"purl": "pkg:npm/prettier",
18253+
"extracted_requirement": "^2",
1825418254
"scope": "devDependencies",
1825518255
"is_runtime": false,
1825618256
"is_optional": true,

tests/packagedcode/data/npm/yarn-lock/v1-other-constraint/yarn.lock-expected

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
"extra_data": {}
9696
},
9797
{
98-
"purl": "pkg:npm/string-width-cjs%40%22npm:string-width",
98+
"purl": "pkg:npm/string-width",
9999
"extracted_requirement": "^4.2.0",
100100
"scope": "dependencies",
101101
"is_runtime": true,
@@ -117,7 +117,7 @@
117117
"extra_data": {}
118118
},
119119
{
120-
"purl": "pkg:npm/strip-ansi-cjs%40%22npm:strip-ansi",
120+
"purl": "pkg:npm/strip-ansi",
121121
"extracted_requirement": "^6.0.1",
122122
"scope": "dependencies",
123123
"is_runtime": true,

0 commit comments

Comments
 (0)