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: added informative root directory install error #7596

Open
wants to merge 3 commits into
base: latest
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions lib/commands/ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ class CI extends ArboristWorkspaceCmd {
}

const where = this.npm.prefix

if (path.parse(where).root === where) {
const msg =
'The current working directory is the root directory of the filesystem.\n' +
'Package installs in root directories are not allowed.\n' +
'Please create a new folder and install your packages in there.\n'
throw new Error(msg)
}

const Arborist = require('@npmcli/arborist')
const opts = {
...this.npm.flatOptions,
Expand Down
9 changes: 9 additions & 0 deletions lib/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const pacote = require('pacote')
const checks = require('npm-install-checks')
const reifyFinish = require('../utils/reify-finish.js')
const ArboristWorkspaceCmd = require('../arborist-cmd.js')
const path = require('node:path')

class Install extends ArboristWorkspaceCmd {
static description = 'Install a package'
Expand Down Expand Up @@ -104,6 +105,14 @@ class Install extends ArboristWorkspaceCmd {
const forced = this.npm.config.get('force')
const scriptShell = this.npm.config.get('script-shell') || undefined

if (path.parse(where).root === where) {
const msg =
'The current working directory is the root directory of the filesystem.\n' +
'Package installs in root directories are not allowed.\n' +
'Please create a new folder and install your packages in there.\n'
throw new Error(msg)
}

// be very strict about engines when trying to update npm itself
const npmInstall = args.find(arg => arg.startsWith('npm@') || arg === 'npm')
if (isGlobalInstall && npmInstall) {
Expand Down
24 changes: 24 additions & 0 deletions test/lib/commands/ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,27 @@ t.test('should use --workspace flag', async t => {
assert.packageMissing('node_modules/[email protected]')
assert.packageInstalled('node_modules/[email protected]')
})

t.test('throw error on ci in root directory', async t => {
let npmError = null

const { npm } = await loadMockNpm(t, {
config: {
prefix: path.parse(process.cwd()).root,
'dry-run': true,
},
})

try {
await npm.exec('ci', ['fizzbuzz'])
} catch (error) {
npmError = error.message
}

const expectedMessage =
'The current working directory is the root directory of the filesystem.\n' +
'Package installs in root directories are not allowed.\n' +
'Please create a new folder and install your packages in there.\n'

t.strictSame(npmError, expectedMessage)
})
24 changes: 24 additions & 0 deletions test/lib/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,27 @@ t.test('should show install keeps dirty --workspace flag', async t => {
assert.packageDirty('node_modules/[email protected]')
assert.packageInstalled('node_modules/[email protected]')
})

t.test('throw error on package install in root directory', async t => {
let npmError = null

const { npm } = await loadMockNpm(t, {
config: {
prefix: path.parse(process.cwd()).root,
'dry-run': true,
},
})

try {
await npm.exec('install', ['fizzbuzz'])
} catch (error) {
npmError = error.message
}

const expectedMessage =
'The current working directory is the root directory of the filesystem.\n' +
'Package installs in root directories are not allowed.\n' +
'Please create a new folder and install your packages in there.\n'

t.strictSame(npmError, expectedMessage)
})