Skip to content

Commit

Permalink
feat: changing the way we name package dependency roots
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

This major version bump is mostly cautionary, as existing consumers of this plugin ought not to encounter errors. However, it is a change in how the makeup of the dependency graph works, and therefore could be argued to require this version bump. Thus we err on the side of caution.
  • Loading branch information
dotkas committed Nov 16, 2023
1 parent 285b3f4 commit 3edf270
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 32 deletions.
50 changes: 19 additions & 31 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,14 @@ import * as tmp from 'tmp';
import * as pMap from 'p-map';
import * as chalk from 'chalk';
import { DepGraph } from '@snyk/dep-graph';
import debugModule = require('debug');
import { legacyCommon, legacyPlugin as api } from '@snyk/cli-interface';

import { MissingSubProjectError } from './errors';
import { getGradleAttributesPretty } from './gradle-attributes-pretty';
import { buildGraph, SnykGraph } from './graph';
import type {
CoordinateMap,
PomCoords,
Sha1Map,
SnykHttpClient,
} from './types';
import type { CoordinateMap, PomCoords, Sha1Map, SnykHttpClient, } from './types';
import { getMavenPackageInfo } from './search';
import debugModule = require('debug');

type ScannedProject = legacyCommon.ScannedProject;

Expand Down Expand Up @@ -98,12 +93,12 @@ export async function inspect(
): Promise<api.InspectResult> {
debugLog(
'Gradle inspect called with: ' +
JSON.stringify({
root,
targetFile,
allSubProjects: (options as any)?.allSubProjects,
subProject: (options as any)?.subProject,
}),
JSON.stringify({
root,
targetFile,
allSubProjects: (options as any)?.allSubProjects,
subProject: (options as any)?.subProject,
}),
);

if (!options) {
Expand Down Expand Up @@ -216,13 +211,13 @@ function extractJsonFromScriptOutput(stdoutText: string): JsonDepsScriptResult {
if (jsonLine === null) {
throw new Error(
'No line prefixed with "JSONDEPS " was returned; full output:\n' +
stdoutText,
stdoutText,
);
}
debugLog(
'The command produced JSONDEPS output of ' +
jsonLine!.length +
' characters',
jsonLine!.length +
' characters',
);
return JSON.parse(jsonLine!);
}
Expand Down Expand Up @@ -527,7 +522,6 @@ async function getAllDeps(
});
}
return await processProjectsInExtractedJSON(
root,
extractedJSON,
coordinateMap,
);
Expand Down Expand Up @@ -568,8 +562,8 @@ You have several options to make dependency resolution rules more specific:
1. Run Snyk CLI tool with an attribute filter, e.g.:
${chalk.whiteBright(
'snyk test --all-sub-projects --configuration-attributes=buildtype:release,usage:java-runtime',
)}
'snyk test --all-sub-projects --configuration-attributes=buildtype:release,usage:java-runtime',
)}
The filter will select matching attributes from those found in your configurations, use them
to select matching configuration(s) to be used to resolve dependencies. Any sub-string of the full
Expand All @@ -586,17 +580,17 @@ ${jsonAttrsPretty}
2. Run Snyk CLI tool for specific configuration(s), e.g.:
${chalk.whiteBright(
"snyk test --gradle-sub-project=my-app --configuration-matching='^releaseRuntimeClasspath$'",
)}
"snyk test --gradle-sub-project=my-app --configuration-matching='^releaseRuntimeClasspath$'",
)}
(note that some configurations won't be present in every your subproject)
3. Converting your subproject dependency specifications from the form of
${chalk.whiteBright("implementation project(':mymodule')")}
to
${chalk.whiteBright(
"implementation project(path: ':mymodule', configuration: 'default')",
)}`;
"implementation project(path: ':mymodule', configuration: 'default')",
)}`;
}

error.message = `${chalk.red.bold(
Expand All @@ -614,7 +608,6 @@ ${chalk.red.bold(mainErrorMessage)}`;
}

export async function processProjectsInExtractedJSON(
root: string,
extractedJSON: JsonDepsScriptResult,
coordinateMap?: CoordinateMap,
) {
Expand All @@ -626,16 +619,11 @@ export async function processProjectsInExtractedJSON(
continue;
}

const invalidValues = [null, undefined, ''];
const isValidRootDir = invalidValues.indexOf(root) === -1;
const isSubProject = projectId !== defaultProjectKey;

let projectName = isValidRootDir ? path.basename(root) : defaultProject;

let projectName = defaultProject;
if (isSubProject) {
projectName = isValidRootDir
? `${path.basename(root)}/${projectId}`
: `${defaultProject}/${projectId}`;
projectName = `${defaultProject}/${projectId}`;
}

extractedJSON.projects[projectId].depGraph = await buildGraph(
Expand Down
1 change: 0 additions & 1 deletion test/manual/gradle-stdout.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ describe('findProjectsInExtractedJSON', () => {

const { defaultProject, projects, allSubProjectNames } =
await processProjectsInExtractedJSON(
rootDir,
jsonExtractedFromGradleStdout,
);

Expand Down

0 comments on commit 3edf270

Please sign in to comment.