Skip to content

Commit 732d096

Browse files
committed
fix: don't prompt on npm exec [directory]
Local directories have to be "installed" so that their bins are linked and set up and callable, the user shouldn't need to be prompted to do that. Note that this does NOT affect anything passed via the `--package` param, because that may also contain non-directory specs so the existing behavior needs to be preserved. This is a small QOL improvement for the isolated use case of "npm exec [directory]"
1 parent 99ed4c9 commit 732d096

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

workspaces/libnpmexec/lib/index.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,16 @@ const exec = async (opts) => {
132132
packages.push(args[0])
133133
}
134134

135-
// TODO We need any directiories in packages to pass through path.resolve
135+
// Resolve any directory specs so that the npx directory is unique to the
136+
// resolved directory, not the potentially relative one (i.e. "npx .")
137+
for (const i in packages) {
138+
const pkg = packages[i]
139+
const spec = npa(pkg)
140+
if (spec.type === 'directory') {
141+
packages[i] = spec.fetchSpec
142+
}
143+
}
144+
136145
const localArb = new Arborist({ ...flatOptions, path })
137146
const localTree = await localArb.loadActual()
138147

@@ -184,7 +193,15 @@ const exec = async (opts) => {
184193
throw new Error('Must provide a valid npxCache path')
185194
}
186195
const hash = crypto.createHash('sha512')
187-
.update(packages.sort((a, b) => a.localeCompare(b, 'en')).join('\n'))
196+
.update(packages.map(p => {
197+
// Keeps the npx directory unique to the resolved directory, not the
198+
// potentially relative one (i.e. "npx .")
199+
const spec = npa(p)
200+
if (spec.type === 'directory') {
201+
return spec.fetchSpec
202+
}
203+
return p
204+
}).sort((a, b) => a.localeCompare(b, 'en')).join('\n'))
188205
.digest('hex')
189206
.slice(0, 16)
190207
const installDir = resolve(npxCache, hash)

workspaces/libnpmexec/test/index.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,16 @@ require('fs').writeFileSync(process.argv.slice(2)[0], 'LOCAL PKG')`,
482482
const executable = resolve(path, 'a/index.js')
483483
fs.chmodSync(executable, 0o775)
484484

485-
await libexec({
485+
const mockexec = t.mock('../lib/index.js', {
486+
'@npmcli/ci-detect': () => true,
487+
'proc-log': {
488+
warn (title, msg) {
489+
t.fail('should not warn about local file package install')
490+
},
491+
},
492+
})
493+
494+
await mockexec({
486495
...baseOpts,
487496
args: [`file:${resolve(path, 'a')}`, 'resfile'],
488497
cache,

0 commit comments

Comments
 (0)