Repository navigation
fix(static): honor optionalDependencies in package.json extraction - #774
Merged
Merged
Conversation
npm installs an optionalDependencies entry in place of a same-name dependencies entry, but optionalDependencies was never read. Include it in the extracted sections (and the invalid-JSON fallback scan) and skip the overridden dependencies entry, so the version that is actually installed is the one checked. Fixes NVIDIA#770 Signed-off-by: drakeo338 <paranoyouz@gmail.com>
rng1995
approved these changes
Oct 14, 2026
rng1995
left a comment
Collaborator
There was a problem hiding this comment.
[SkillSpector Review]
@drakeo338 thank you for the focused fix, and for following npm's own precedence rule instead of just adding another section.
Value and readiness
#770 is a real SC4 false negative. Packages listed only in optionalDependencies were never checked, and a same-name override was checked at the dependencies version that npm does not install. The change reads optionalDependencies in both the JSON path and the invalid-JSON fallback. It also drops the overridden dependencies entry, which matches npm's documented rule that optional entries override same-name dependencies entries. Existing budget, pin and line-location handling is reused. Ready for final maintainer review.
Material findings
None blocking.
- Optional,
src/skillspector/nodes/analyzers/static_patterns_supply_chain.py:1382:overridden = set(optional)also includes keys whose optional value is not a string. For{"dependencies": {"chalk": "4.1.2"}, "optionalDependencies": {"chalk": 5}}, thedependenciesentry is skipped (L1391) and the optional one is skipped as a non-string, sochalkis not checked at all; main checks 4.1.2. npm treats non-string specs as invalid, so the impact is small. Still, a malformed sibling should not remove a check. Building the set from string-valued entries only, plus one test, closes it.
PIC tradeoffs
None identified.
Verification and gaps
- I read the full diff against merge-base
ef0f96d7and the surrounding extractor on main. _package_json_linesiterates_NPM_DEPENDENCY_SECTIONS, so line lookup now starts at theoptionalDependenciesheader. The skip happens before the limit counter, so budgets are unaffected._apply_locked_versionsstill applies npm lock versions to optional entries._NPM_DEPENDENCY_SECTIONSand this extractor are the onlypackage.jsondependency parsers insrc/.- The invalid-JSON fallback reports both versions when a name appears in both sections. That is conservative and acceptable, because npm would not install from an invalid manifest.
- Tests: optional-only (including a scoped name), same-name override, limit/line numbers, and fallback. The first case returns
[]on main, so it would catch a regression. - Main (
b07f181d) is 54 commits ahead;git merge-treeis clean, and the PR is also clean against #765 and #773. - CI: all 6 checks are green on
6bfca4e. I did not run PR code, per policy.
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.
Fixes #770.
The package.json extractor never read
optionalDependencies, so packages listed only there were not checked. When an entry overrode the same name independencies, the overriddendependenciesversion was checked instead of the one npm installs. This readsoptionalDependencies(including in the invalid-JSON fallback scan) and skips the overriddendependenciesentry. Tests added intests/unit/test_patterns_new.pyfail on the old code and pass now.