Skip to content

feat: honor --usage and --only-used in multi-folder scans - #904

Open
Ayush7614 wants to merge 2 commits into
OWASP:mainfrom
Ayush7614:feat/multi-folder-usage
Open

feat: honor --usage and --only-used in multi-folder scans#904
Ayush7614 wants to merge 2 commits into
OWASP:mainfrom
Ayush7614:feat/multi-folder-usage

Conversation

@Ayush7614

@Ayush7614 Ayush7614 commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Multi-folder scans now run the same per-folder source usage annotation as single-folder scanProject when --usage / --only-used are set.
  • --only-used filters each subfolder’s findings to packages actually imported in that folder’s source.
  • Previously these flags were accepted (and exposed in the GitHub Action) but silently ignored in multi-folder mode.

Why

Package-based monorepos (no root lockfile, ≥2 nested lockfiles) route through handleMultiFolderScan, which never called scanProjectForPackageUsage. Teams enabling usage via CLI or Action inputs got unannotated / unfiltered results with no warning.

Test plan

  • npm test -- tests/multi-folder-scan.test.ts (usage annotate, only-used filter, no-op without flag)
  • npm run build
  • Manual: cve-lite examples/nx-package-based --usage shows usage on findings; --only-used reduces the set

@Ayush7614
Ayush7614 requested a review from sonukapoor as a code owner July 27, 2026 19:27

@sonukapoor sonukapoor left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice fix @Ayush7614, thanks for tracking this down. I traced the loop and the per-folder scoping is right: subfolderAbs is already computed per iteration for readDirectDependencyNames, and you reuse it for scanProjectForPackageUsage, so there is no risk of one folder's findings getting filtered against another folder's imports.

One thing before merging: none of the three new tests exercise more than one subfolder, so nothing proves the per-folder isolation this PR is about. The --check-overrides tests just above yours already do this (two subfolders, a and b, asserting the mock is called twice with the right args). Could you add a similar one, ideally with a package imported in one folder and not the other, so we get a regression test for the actual bug rather than just the annotate/filter mechanics?

Two small things while you are in there: the if (!params.options.json) guard around the logInfo call is redundant, since logInfo already no-ops when options.json is set (which is why the single-folder call site does not wrap it). And longer-term (not blocking) it would be worth pulling the annotate-then-filter block into a shared helper used by both index.ts and multi-folder-scan.ts. This repo keeps hitting the "single-folder has X, multi-folder quietly does not" pattern (#866, #878, #879), and this logic living in two files is exactly how that happens again.

Also worth linking an issue in the PR body if one exists. Happy to open one on our side if you did not file it.

@Ayush7614

Copy link
Copy Markdown
Collaborator Author

Addressed review feedback:

  1. Added a two-subfolder isolation test: same package in packages/a and packages/b, imported only in a — with --only-used, a keeps the finding and b is empty. Also asserts scanProjectForPackageUsage is called once per folder with the correct absolute paths.
  2. Removed the redundant if (!params.options.json) guard around logInfo (matches single-folder).

No issue was filed for this gap when the PR was opened — happy for you to open one on your side if you want it linked; otherwise this can land as a direct parity fix.

Flags were accepted but never wired: multi-folder skipped the source
usage scan that single-folder runs. Annotate and filter findings per
subfolder so Action/CLI usage inputs work on monorepos.
Add a two-folder regression where lodash is imported only in packages/a
so --only-used keeps it there and drops it from packages/b. Also drop
the redundant json guard around logInfo (it already no-ops).
@Ayush7614
Ayush7614 force-pushed the feat/multi-folder-usage branch from ac63534 to 4f1b251 Compare July 28, 2026 12:09
@Ayush7614

Copy link
Copy Markdown
Collaborator Author

@sonukapoor thanks for the review — addressed:

  1. Added a two-subfolder isolation test: same package in packages/a and packages/b, imported only in a — with --only-used, a keeps the finding and b is empty. Also asserts scanProjectForPackageUsage is called once per folder with the correct absolute paths.
  2. Removed the redundant if (!params.options.json) guard around logInfo (matches single-folder).

No issue was filed for this gap when the PR was opened — happy for you to open one on your side if you want it linked.

Also rebased this branch onto latest main.

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.

2 participants