Skip to content
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

fix: account for npx package-name with no spec #6374

Merged
merged 2 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mock-registry/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class MockRegistry {
// mocked with a 404, 500, etc.
// XXX: this is opt-in currently because it breaks some existing CLI
// tests. We should work towards making this the default for all tests.
t.fail(`Unmatched request: ${JSON.stringify(req.options, null, 2)}`)
t.fail(`Unmatched request: ${JSON.stringify(req, null, 2)}`)
}
if (debug) {
console.error('NO MATCH', t.name, req.options ? req.options : req.path)
Expand Down
50 changes: 50 additions & 0 deletions smoke-tests/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ t.test('basic', async t => {
'package.json': { name: 'promise-all-reject-late', version: '5.0.0' },
'index.js': 'module.exports = null',
},
'exec-test-1.0.0': {
'package.json': { name: 'exec-test', version: '1.0.0', bin: { 'exec-test': 'run.sh' } },
'index.js': 'module.exports = "1.0.0"',
'run.sh': 'echo 1.0.0',
},
'exec-test-1.0.1': {
'package.json': { name: 'exec-test', version: '1.0.1', bin: { 'exec-test': 'run.sh' } },
'index.js': 'module.exports = "1.0.1"',
'run.sh': 'echo 1.0.1',
},
},
},
})
Expand Down Expand Up @@ -332,4 +342,44 @@ t.test('basic', async t => {
t.equal(err.code, 1)
t.matchSnapshot(err.stderr, 'should throw mismatch deps in lock file error')
})

await t.test('npm exec', async t => {
if (process.platform === 'win32') {
t.skip()
return
}
// First run finds package
{
const packument = registry.packument({
name: 'exec-test', version: '1.0.0', bin: { 'exec-test': 'run.sh' },
})
const manifest = registry.manifest({ name: 'exec-test', packuments: [packument] })
await registry.package({
times: 2,
manifest,
tarballs: {
'1.0.0': join(paths.root, 'packages', 'exec-test-1.0.0'),
},
})

const o = await npm('exec', 'exec-test')
t.match(o.trim(), '1.0.0')
}
// Second run finds newer version
{
const packument = registry.packument({
name: 'exec-test', version: '1.0.1', bin: { 'exec-test': 'run.sh' },
})
const manifest = registry.manifest({ name: 'exec-test', packuments: [packument] })
await registry.package({
times: 2,
manifest,
tarballs: {
'1.0.1': join(paths.root, 'packages', 'exec-test-1.0.1'),
},
})
const o = await npm('exec', 'exec-test')
t.match(o.trim(), '1.0.1')
}
})
})
20 changes: 14 additions & 6 deletions workspaces/libnpmexec/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ const getManifest = async (spec, flatOptions) => {

// Returns the required manifest if the spec is missing from the tree
// Returns the found node if it is in the tree
const missingFromTree = async ({ spec, tree, flatOptions }) => {
if (spec.registry && spec.type !== 'tag') {
const missingFromTree = async ({ spec, tree, flatOptions, isNpxTree }) => {
// If asking for a spec by name only (spec.raw === spec.name):
// - In local or global mode go with anything in the tree that matches
// - If looking in the npx cache check if a newer version is available
const npxByNameOnly = isNpxTree && spec.name === spec.raw
if (spec.registry && spec.type !== 'tag' && !npxByNameOnly) {
// registry spec that is not a specific tag.
const nodesBySpec = tree.inventory.query('packageName', spec.name)
for (const node of nodesBySpec) {
// package requested by name only (or name@*)
if (spec.rawSpec === '*') {
return { node }
}
Expand All @@ -56,8 +59,8 @@ const missingFromTree = async ({ spec, tree, flatOptions }) => {
const manifest = await getManifest(spec, flatOptions)
return { manifest }
} else {
// non-registry spec, or a specific tag. Look up manifest and check
// resolved to see if it's in the tree.
// non-registry spec, or a specific tag, or name only in npx tree. Look up
// manifest and check resolved to see if it's in the tree.
const manifest = await getManifest(spec, flatOptions)
if (spec.type === 'directory') {
return { manifest }
Expand Down Expand Up @@ -224,7 +227,12 @@ const exec = async (opts) => {
})
const npxTree = await npxArb.loadActual()
await Promise.all(needInstall.map(async ({ spec }) => {
const { manifest } = await missingFromTree({ spec, tree: npxTree, flatOptions })
const { manifest } = await missingFromTree({
spec,
tree: npxTree,
flatOptions,
isNpxTree: true,
})
if (manifest) {
// Manifest is not in npxCache, we need to install it there
if (!spec.registry) {
Expand Down