-
Notifications
You must be signed in to change notification settings - Fork 4.6k
fix(libnpmexec): honor min-release-age-exclude in npx #9768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Sanjays2402
wants to merge
2
commits into
npm:latest
Choose a base branch
from
Sanjays2402:fix/issue-9765
base: latest
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // Mirrors the semantics used by @npmcli/arborist so `npx` honors | ||
| // `min-release-age-exclude` the same way `npm install` does. | ||
| // | ||
| // When the config layer sets `flatOptions.before` from `min-release-age`, | ||
| // pacote will refuse to resolve versions published after that cutoff. | ||
| // pacote itself has no notion of `min-release-age-exclude`; arborist | ||
| // applies it per-spec by dropping `before` for matched names. libnpmexec | ||
| // calls `pacote.manifest` directly, so it has to do the same. | ||
| // | ||
| // Patterns are exact names or minimatch globs against the resolved | ||
| // package name. Only the named package is exempt; its own dependencies | ||
| // still follow the release-age policy unless they also match a pattern. | ||
| const { minimatch } = require('minimatch') | ||
|
|
||
| // Match arborist's hardened option set: `nonegate` keeps a leading `!` | ||
| // literal so a stray `!foo` exempts nothing instead of everything-but-foo, | ||
| // `nocomment` keeps `#` literal, and `noext` disables extglobs. This list | ||
| // only ever widens the exemption, so we never want a pattern feature to | ||
| // silently turn into match-all. | ||
| const minimatchOptions = { nonegate: true, nocomment: true, noext: true } | ||
|
|
||
| const isReleaseAgeExcluded = (name, patterns) => { | ||
| if (!name || !Array.isArray(patterns) || patterns.length === 0) { | ||
| return false | ||
| } | ||
| return patterns.some(pattern => | ||
| name === pattern || minimatch(name, pattern, minimatchOptions)) | ||
| } | ||
|
|
||
| // For `npm:` aliases the fetched package is the alias target, not the | ||
| // alias key, so the exemption must be keyed on the underlying name. | ||
| // Mirrors `trustedSpecName` in arborist's release-age-exclude.js. | ||
| const trustedSpecName = (spec) => { | ||
| if (!spec) { | ||
| return undefined | ||
| } | ||
| if (spec.type === 'alias' && spec.subSpec && spec.subSpec.registry) { | ||
| return spec.subSpec.name | ||
| } | ||
| return spec.name | ||
| } | ||
|
|
||
| // Return a shallow copy of `flatOptions` with `before` cleared when the | ||
| // spec's trusted name matches an exclude pattern. Non-mutating so shared | ||
| // option objects aren't clobbered for later, unrelated specs. | ||
| const applyReleaseAgeExclude = (spec, flatOptions) => { | ||
| if (!flatOptions || flatOptions.before == null) { | ||
| return flatOptions | ||
| } | ||
| if (!isReleaseAgeExcluded(trustedSpecName(spec), flatOptions.minReleaseAgeExclude)) { | ||
| return flatOptions | ||
| } | ||
| return { ...flatOptions, before: null } | ||
| } | ||
|
|
||
| module.exports = { isReleaseAgeExcluded, trustedSpecName, applyReleaseAgeExclude } |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| const t = require('tap') | ||
| const npa = require('npm-package-arg') | ||
| const { | ||
| isReleaseAgeExcluded, | ||
| trustedSpecName, | ||
| applyReleaseAgeExclude, | ||
| } = require('../lib/release-age-exclude.js') | ||
|
|
||
| t.test('isReleaseAgeExcluded: empty / invalid patterns', async t => { | ||
| t.equal(isReleaseAgeExcluded('lodash', []), false) | ||
| t.equal(isReleaseAgeExcluded('lodash', undefined), false) | ||
| t.equal(isReleaseAgeExcluded('lodash', null), false) | ||
| t.equal(isReleaseAgeExcluded(undefined, ['*']), false) | ||
| t.equal(isReleaseAgeExcluded('', ['*']), false) | ||
| }) | ||
|
|
||
| t.test('isReleaseAgeExcluded: exact and glob patterns', async t => { | ||
| t.equal(isReleaseAgeExcluded('lodash', ['lodash']), true, 'exact match') | ||
| t.equal(isReleaseAgeExcluded('lodash', ['other']), false, 'exact miss') | ||
| t.equal(isReleaseAgeExcluded('@myorg/foo', ['@myorg/*']), true, 'scope glob match') | ||
| t.equal(isReleaseAgeExcluded('@other/foo', ['@myorg/*']), false, 'scope glob miss') | ||
| }) | ||
|
|
||
| t.test('isReleaseAgeExcluded: hardened glob semantics', async t => { | ||
| // `!foo` must NOT act as a negation exempting everything but foo. | ||
| t.equal(isReleaseAgeExcluded('bar', ['!foo']), false, | ||
| 'leading ! stays literal and does not exempt unrelated names') | ||
| // `#foo` must NOT be treated as a comment (which would match nothing). | ||
| t.equal(isReleaseAgeExcluded('#foo', ['#foo']), true, | ||
| 'leading # is literal') | ||
| }) | ||
|
|
||
| t.test('trustedSpecName: registry, alias, and edge cases', async t => { | ||
| t.equal(trustedSpecName(undefined), undefined) | ||
| t.equal(trustedSpecName(npa('lodash@1.2.3')), 'lodash', 'registry spec') | ||
| // npm: alias — the fetched package is the alias target. | ||
| t.equal(trustedSpecName(npa('foo@npm:@myorg/real@1')), '@myorg/real', | ||
| 'alias resolves to underlying package name') | ||
| }) | ||
|
|
||
| t.test('applyReleaseAgeExclude: no before => passthrough', async t => { | ||
| const opts = { before: null, minReleaseAgeExclude: ['@myorg/*'] } | ||
| t.equal(applyReleaseAgeExclude(npa('@myorg/foo@1'), opts), opts, | ||
| 'returns same object when there is no cutoff to clear') | ||
| }) | ||
|
|
||
| t.test('applyReleaseAgeExclude: not excluded => passthrough', async t => { | ||
| const opts = { before: new Date('2026-01-01'), minReleaseAgeExclude: ['@myorg/*'] } | ||
| t.equal(applyReleaseAgeExclude(npa('lodash@1'), opts), opts, | ||
| 'unrelated package keeps the before cutoff') | ||
| }) | ||
|
|
||
| t.test('applyReleaseAgeExclude: excluded => before cleared, non-mutating', async t => { | ||
| const before = new Date('2026-01-01') | ||
| const opts = { before, minReleaseAgeExclude: ['@myorg/*'], other: 'x' } | ||
| const out = applyReleaseAgeExclude(npa('@myorg/foo@1.2.3'), opts) | ||
| t.not(out, opts, 'returns a copy') | ||
| t.equal(opts.before, before, 'input untouched') | ||
| t.equal(out.before, null, 'before cleared for excluded spec') | ||
| t.equal(out.other, 'x', 'other options preserved') | ||
| t.same(out.minReleaseAgeExclude, ['@myorg/*'], 'exclude list preserved') | ||
| }) | ||
|
|
||
| t.test('applyReleaseAgeExclude: alias excluded by alias-target name', async t => { | ||
| const opts = { | ||
| before: new Date('2026-01-01'), | ||
| minReleaseAgeExclude: ['@myorg/real'], | ||
| } | ||
| const out = applyReleaseAgeExclude(npa('foo@npm:@myorg/real@1'), opts) | ||
| t.equal(out.before, null, | ||
| 'alias exemption keyed on underlying package name, not alias key') | ||
| }) | ||
|
|
||
| t.test('applyReleaseAgeExclude: null / empty options', async t => { | ||
| t.equal(applyReleaseAgeExclude(npa('lodash@1'), null), null) | ||
| t.equal(applyReleaseAgeExclude(npa('lodash@1'), undefined), undefined) | ||
| const opts = {} | ||
| t.equal(applyReleaseAgeExclude(npa('lodash@1'), opts), opts, | ||
| 'no before => passthrough') | ||
| }) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding
min-release-age-excludesupport tonpx. I found one cache-lifetime issue that should be addressed before merging.manifestsis module-scoped cache and keyed only byspec.raw, whileapplyReleaseAgeExclude()runs only on a cache miss. If the firstlibnpmexeccall excludes a package, it can cache a post-cutoff manifest resolved withbefore: null. A later call for the same package without the exclusion then reuses that manifest and bypasses its release-age restriction.I reproduced this with two sequential calls: both executed the newer version, although the second call should have selected the older, cutoff-eligible version.
Could we scope the manifest cache to each
exec()invocation and pass it through all threemissingFromTree()? That preserves caching between the local and npx-tree checks while preventing policy-specific results from leaking across calls.Please also add a two-call regression test covering this scenario.