Skip to content

Commit

Permalink
fix: crash when analyzing node package with a local dependency (#5235)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylong authored Jan 10, 2023
2 parents ffa5a26 + 0cbbe57 commit 66e583b
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,14 @@ private void processDependencies(JsonObject json, File baseDir, File rootFile,

if (entry.getValue() instanceof JsonObject) {
jo = (JsonObject) entry.getValue();

// Ignore/skip linked entries (as they don't have "version" and
// later logic will crash)
if (jo.getBoolean("link", false)) {
LOGGER.warn("Skipping `" + name + "` because it is a link dependency");
continue;
}

version = jo.getString("version");
optional = jo.getBoolean("optional", false);
isDev = jo.getBoolean("dev", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,26 @@ public void testPackageLockV3() throws AnalysisException, InvalidSettingExceptio
analyzer.analyze(packageLockJson, engine);
assertEquals("Expected 1 dependencies", 6, engine.getDependencies().length);
}

/**
* Test of analysis of package with a local package as a dependency.
*
* This test crashes with an NPE if issue #1947 isn't resolved.
*
* @throws AnalysisException if there was a problem with the analysis
*/
@Test
public void testLocalPackageDependency() throws AnalysisException, InvalidSettingException {
Assume.assumeThat(getSettings().getBoolean(Settings.KEYS.ANALYZER_NODE_PACKAGE_ENABLED), is(true));
final Dependency packageJson = new Dependency(BaseTest.getResourceAsFile(this,
"nodejs/local_package/package.json"));
final Dependency packageLockJson = new Dependency(BaseTest.getResourceAsFile(this,
"nodejs/local_package/package-lock.json"));
engine.addDependency(packageJson);
engine.addDependency(packageLockJson);
analyzer.analyze(packageJson, engine);
assertEquals("Expected 1 dependencies", 1, engine.getDependencies().length);
analyzer.analyze(packageLockJson, engine);
assertEquals("Expected 2 dependencies", 2, engine.getDependencies().length);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "mypackage",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions core/src/test/resources/nodejs/local_package/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions core/src/test/resources/nodejs/local_package/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "local_package",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"mypackage": "file:./mypackage"
}
}

0 comments on commit 66e583b

Please sign in to comment.