Skip to content

npm workspace filtering#1798

Open
dterrybd wants to merge 9 commits into
masterfrom
dev/dterry/IDETECT-5076-npm-workspace-filtering-review
Open

npm workspace filtering#1798
dterrybd wants to merge 9 commits into
masterfrom
dev/dterry/IDETECT-5076-npm-workspace-filtering-review

Conversation

@dterrybd

@dterrybd dterrybd commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

This PR adds two new configuration properties — detect.npm.excluded.workspaces and detect.npm.included.workspaces that let users control which npm workspaces are included in a Detect scan.

Workspaces are identified by their path relative to the project root (e.g. packages/react-components), matching the existing convention used by the equivalent Yarn properties (detect.yarn.excluded.workspaces / detect.yarn.included.workspaces). Both the lockfile path (NpmLockfilePackager) and the CLI path (NpmCliParser) respect the filter, and glob wildcards are supported (e.g. packages/internal-*). By default all workspaces are included; the exclude list takes precedence over the include list when both are specified.

note: based on review feedback I have added detect.npm.ignore.all.workspaces. When set to true this will ignore all workspaces and takes precedence over other settings.

filtering takes place primarily in:
package json detector: detectable/src/main/java/com/blackduck/integration/detectable/detectables/npm/packagejson/CombinedPackageJsonExtractor.java

shrinkwrap and lockfile detectors: detectable/src/main/java/com/blackduck/integration/detectable/detectables/npm/lockfile/parse/NpmLockfilePackager.java

cli detector:
detectable/src/main/java/com/blackduck/integration/detectable/detectables/npm/cli/parse/NpmCliParser.java

@dterrybd
dterrybd marked this pull request as draft July 9, 2026 15:10
@dterrybd
dterrybd requested a review from zahidblackduck July 10, 2026 18:21
@dterrybd
dterrybd marked this pull request as ready for review July 10, 2026 18:21
}

populateChildren(graph, child, children, directWorkspaceDependency, combinedPackageJson, aliasMapping);
// When a workspace is excluded by the filter, do not traverse its children —

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dterrybd I believe this is the core logic right? rest is just plumbing code if I'm not wrong?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the npm CLI detector that is correct. This cuts off processing the children of a workspace that is being excluded and is the main logic addition for this detector. There are changes for the shrinkwrap/lockfile detector and the package json detector (see the bottom of the description of this PR for the specific classes) but the changes were smaller there. We mostly read workspace data into specific structures so if a workspace is excluded we could easily filter out that data.

String workspaceJsonString
= FileUtils.readFileToString(new File(workspaceJsonPath.toString()), StandardCharsets.UTF_8);


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dterrybd this is just idenifying the workspace paths so the downstream extractor can apply the workspace filters correctly?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, the CLI and shrinkwrap/lockfile detectors will use knowledge from the package.json to do their calculations so there is a little bit more logic in this class than just powering the package json detector

@bd-samratmuk

Copy link
Copy Markdown
Contributor

Can we improve the overall class and function comments in the npm detector package overall? Like this PR has nice comments! 👍

@dterrybd

Copy link
Copy Markdown
Contributor Author

Can we improve the overall class and function comments in the npm detector package overall? Like this PR has nice comments! 👍

There's an internal document on npm when it was restructured for the new lockfiles a few years back. I'll file a ticket to bring some of that knowledge into the codebase as comments where it makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants