Skip to content

Commit 6237783

Browse files
committed
fix: exempt local project introspection from allow-directory
1 parent 5fc9bc0 commit 6237783

2 files changed

Lines changed: 51 additions & 1 deletion

File tree

workspaces/libnpmexec/lib/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,10 @@ const missingFromTree = async ({ spec, tree, flatOptions, isNpxTree, shallow })
8787
}
8888

8989
// see if the package.json at `path` has an entry that matches `cmd`
90+
// the path is a known-local directory, not a user-supplied dep, so
91+
// allow-directory must not gate this introspection
9092
const hasPkgBin = (path, cmd, flatOptions) =>
91-
pacote.manifest(path, flatOptions)
93+
pacote.manifest(path, { ...flatOptions, allowDirectory: 'all' })
9294
.then(manifest => manifest?.bin?.[cmd]).catch(() => null)
9395

9496
const exec = async (opts) => {
@@ -147,6 +149,8 @@ const exec = async (opts) => {
147149
// we have to install the local package into the npx cache so that its
148150
// bin links get set up
149151
flatOptions.installLinks = false
152+
// self-execution of a local bin, not a directory dep install
153+
flatOptions.allowDirectory = 'all'
150154
// args[0] will exist when the package is installed
151155
packages.push(p)
152156
yes = true

workspaces/libnpmexec/test/local.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,3 +423,49 @@ t.test('global scoped pkg', async t => {
423423
created: 'global/node_modules/@npmcli/create-test/bin-file.js',
424424
})
425425
})
426+
427+
// Regression: local bin lookup must not be gated by allow-directory,
428+
// even when the policy is `none` or `root`.
429+
for (const allowDirectory of ['none', 'root']) {
430+
t.test(`local bin still resolves with allow-directory=${allowDirectory}`, async t => {
431+
const { pkg, fixtures } = createPkg({
432+
version: '1.0.0',
433+
name: '@npmcli/local-pkg-allow-directory-test',
434+
bin: {
435+
a: 'local-bin-test.js',
436+
},
437+
files: {
438+
'local-bin-test.js': { key: 'local-bin', value: 'LOCAL PKG' },
439+
},
440+
})
441+
442+
const { exec, chmod, readOutput, path } = setup(t, {
443+
pkg,
444+
testdir: merge(
445+
fixtures.packages[`@npmcli-local-pkg-allow-directory-test-1.0.0`],
446+
{
447+
node_modules: {
448+
'@npmcli': {
449+
'some-other-pkg-with-same-scope': {},
450+
},
451+
},
452+
}
453+
),
454+
})
455+
456+
const localBin = resolve(path, 'node_modules', '.bin')
457+
458+
await chmod('local-bin-test.js')
459+
460+
await exec({
461+
localBin,
462+
args: ['a', 'argument-a'],
463+
allowDirectory,
464+
})
465+
466+
t.match(await readOutput('local-bin'), {
467+
value: 'LOCAL PKG',
468+
args: ['argument-a'],
469+
})
470+
})
471+
}

0 commit comments

Comments
 (0)