Skip to content

fix: correctly handle scenario where prefix is the cwd #8269

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

Merged
merged 1 commit into from
May 5, 2025
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 lib/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class Install extends ArboristWorkspaceCmd {
args = args.filter(a => resolve(a) !== this.npm.prefix)

// `npm i -g` => "install this package globally"
if (where === globalTop && !args.length) {
if (isGlobalInstall && !args.length) {
args = ['.']
}

Expand Down
5 changes: 3 additions & 2 deletions test/fixtures/mock-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const setupMockNpm = async (t, {
exec = null, // optionally exec the command before returning
// test dirs
prefixDir = {},
prefixOverride = null, // sets global and local prefix to this, the same as the `--prefix` flag
homeDir = {},
cacheDir = {},
globalPrefixDir = { node_modules: {} },
Expand Down Expand Up @@ -170,9 +171,9 @@ const setupMockNpm = async (t, {

const dirs = {
testdir: dir,
prefix: path.join(dir, 'prefix'),
prefix: prefixOverride ?? path.join(dir, 'prefix'),
cache: path.join(dir, 'cache'),
globalPrefix: path.join(dir, 'global'),
globalPrefix: prefixOverride ?? path.join(dir, 'global'),
home: path.join(dir, 'home'),
other: path.join(dir, 'other'),
}
Expand Down
19 changes: 19 additions & 0 deletions test/lib/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,25 @@ t.test('exec commands', async t => {
await npm.exec('install')
})

await t.test('should not self-install package if prefix is the same as CWD', async t => {
let REIFY_CALLED_WITH = null
const { npm } = await loadMockNpm(t, {
mocks: {
'{LIB}/utils/reify-finish.js': async () => {},
'@npmcli/run-script': () => {},
'@npmcli/arborist': function () {
this.reify = (opts) => {
REIFY_CALLED_WITH = opts
}
},
},
prefixOverride: process.cwd(),
})

await npm.exec('install')
t.equal(REIFY_CALLED_WITH.add.length, 0, 'did not install current directory as a dependency')
})

await t.test('should not install invalid global package name', async t => {
const { npm } = await loadMockNpm(t, {
config: {
Expand Down
Loading