Skip to content

fix(misc/npm-source): annotate packages when scanning the project directory - #2495

Open
Thanatos9404 wants to merge 1 commit into
google:mainfrom
Thanatos9404:fix/npm-source-project-root
Open

Thanatos9404 wants to merge 1 commit into
google:mainfrom
Thanatos9404:fix/npm-source-project-root

Conversation

@Thanatos9404

@Thanatos9404 Thanatos9404 commented Oct 19, 2026 •

Copy link
Copy Markdown

When scalibr is run with the project directory as the scan root, the misc/npm-source annotator doesn't set the package source. Paths then look like node_modules/foo/package.json, but npmProjectRootDirectory only looks for /node_modules/ with a leading slash. Top level packages get no project root and end up with ?source=UNKNOWN, and nested ones look for a lockfile inside their parent package. Scanning the parent directory works.

This change maps a path that starts with node_modules/ to the scan root (.), so ResolvedFromLockfile reads the lockfile from there. Paths with a directory in front of node_modules are handled as before.

Tests: new cases in TestMapNPMProjectRootsToPackages and TestAnnotate_LockfileV2 for scans from the project directory. Those cases fail without the fix and pass with it; the other new cases guard the existing behaviour.

Fixes #1486

…ectory

npmProjectRootDirectory looked for "/node_modules/" in the package.json
path to find the project root. When the scan root is the project
directory itself, the paths start with "node_modules/" and have no
leading slash. Top level packages were then skipped, and nested packages
were matched against a directory inside node_modules where no lockfile
exists. In both cases the source from the project lockfile was lost.

Map a path that starts with "node_modules/" to the scan root (".") so
the lockfile next to node_modules is used.
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.

misc/npm-source plugin fails to annotate when scalibr is run in the same directory as package-lock.json

1 participant