fix(arborist): honor omit flags for deps of linked-in packages (#9624)#9685
Open
Sanjays2402 wants to merge 1 commit into
Open
fix(arborist): honor omit flags for deps of linked-in packages (#9624)#9685Sanjays2402 wants to merge 1 commit into
Sanjays2402 wants to merge 1 commit into
Conversation
) Node#shouldOmit bailed out whenever a node's `top` was not the project root or a workspace, returning false unconditionally. For nodes inside a `npm link`-ed package, `top` is the link target, which lives at its own filesystem root and is therefore neither isProjectRoot nor isWorkspace. The omit gate never ran, so `npm audit --production` (omit=dev) surfaced the linked dependency's own devDependencies even though they were correctly flagged as dev by calcDepFlags. Detect this case by checking that the node's `.root` still resolves to the consuming project (or workspace) and that `top.linksIn` is non-empty, then fall through to the usual dev/optional/peer flag check.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
npm audit --production(andnpm audit --omit=dev/--omit=optional/--omit=peer) was reporting vulnerabilities in the dev dependencies ofnpm link-ed packages.Reproduction from issue #9624:
foowherenpm auditfails on its dev deps.npm linkinsidefoo.foo, runnpm link foo.npm audit --production.Expected: the linked package's dev deps are ignored, the same way they are for direct dev deps of the consumer.
Actual: the linked package's dev deps were audited and reported.
The cause is in
Node#shouldOmit(workspaces/arborist/lib/node.js). The method bails out early whenever the node'stopis not the project root or a workspace:For a node that lives inside a
npm link-ed package,topis the link target, which is its own filesystem root (isTop === true,parent === null) and is therefore neitherisProjectRootnorisWorkspace. The omit gate never runs, soshouldOmitreturnsfalsefor every flavor of omit andAuditReport#prepareBulkDataincludes the linked package's devDependencies in the bulk request even when the caller asked foromit=['dev'].calcDepFlagsalready classifies those nodes correctly (devdep.dev === true), so this was strictly the gate that was wrong.The fix keeps the original gate but adds one more accepted case: if the
topis a link target whose.rootstill resolves to the consuming project root (or a workspace), and the link is actually wired into the project (top.linksIn.size > 0), fall through to the usualdev/optional/peer/devOptionalflag check instead of bailing out. The dep flags themselves are unchanged, soomit=devnow correctly hides the linked package's devDependencies while still keeping its prod deps.Existing Issue
Fixes #9624
Screenshots
N/A, behavior change is observable through the audit bulk payload and the report itself.
AI Disclosure
This change was written with the assistance of Claude. I have reviewed the diff and the test and take responsibility for the code.
Test Coverage
Added a regression test in
workspaces/arborist/test/audit-report.jsthat builds a small tree directly withNodeandLink:foovia aLinkfootarget withproddep(prod) anddevdep(dev) childrencalcDepFlagsthen checksAuditReport#prepareBulkData()with and withoutomit: ['dev']The test verifies all four directions:
omit=['dev']+ linked dev dep => omitted (the bug)omit=['dev']+ linked prod dep => still audited (regression guard)Verified the test fails on
latestat exactly the new assertion (linked package's dev dep is omitted under omit=dev) and passes with the patchedNode#shouldOmit. Also ran the fullworkspaces/arborist/test/audit-report.js,workspaces/arborist/test/arborist/audit.js, andworkspaces/arborist/test/node.jssuites locally and they all pass with the change in place.