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: only the add command should fail if there is no bin directory in PATH #5842

Merged
merged 4 commits into from
Dec 27, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/spicy-panthers-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@pnpm/config": patch
"@pnpm/plugin-commands-installation": patch
"pnpm": patch
---

Only the `pnpm add --global <pkg>` command should fail if there is no global pnpm bin directory in the system PATH [#5841](https://github.com/pnpm/pnpm/issues/5841).
4 changes: 0 additions & 4 deletions config/config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,6 @@ export async function getConfig (
if (pnpmConfig.bin) {
fs.mkdirSync(pnpmConfig.bin, { recursive: true })
await checkGlobalBinDir(pnpmConfig.bin, { env, shouldAllowWrite: opts.globalDirShouldAllowWrite })
} else {
throw new PnpmError('NO_GLOBAL_BIN_DIR', 'Unable to find the global bin directory', {
hint: 'Run "pnpm setup" to create it automatically, or set the global-bin-dir setting, or the PNPM_HOME env variable. The global bin directory should be in the PATH.',
})
}
pnpmConfig.save = true
pnpmConfig.allowNew = true
Expand Down
5 changes: 5 additions & 0 deletions pkg-manager/plugin-commands-installation/src/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ export async function handler (
'If you don\'t want to see this warning anymore, you may set the ignore-workspace-root-check setting to true.'
)
}
if (opts.global && !opts.bin) {
throw new PnpmError('NO_GLOBAL_BIN_DIR', 'Unable to find the global bin directory', {
hint: 'Run "pnpm setup" to create it automatically, or set the global-bin-dir setting, or the PNPM_HOME env variable. The global bin directory should be in the PATH.',
})
}

const include = {
dependencies: opts.production !== false,
Expand Down
22 changes: 21 additions & 1 deletion pkg-manager/plugin-commands-installation/test/add.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path'
import { PnpmError } from '@pnpm/error'
import { add, remove } from '@pnpm/plugin-commands-installation'
import { prepare, preparePackages } from '@pnpm/prepare'
import { prepare, prepareEmpty, preparePackages } from '@pnpm/prepare'
import { REGISTRY_MOCK_PORT } from '@pnpm/registry-mock'
import loadJsonFile from 'load-json-file'
import tempy from 'tempy'
Expand Down Expand Up @@ -330,3 +330,23 @@ test('pnpm add automatically installs missing peer dependencies', async () => {
const lockfile = await project.readLockfile()
expect(Object.keys(lockfile.packages).length).toBe(5)
})

test('add: fail when global bin directory is not found', async () => {
prepareEmpty()

let err!: PnpmError
try {
await add.handler({
...DEFAULT_OPTIONS,
bin: undefined as any, // eslint-disable-line
dir: path.resolve('project-1'),
global: true,
linkWorkspacePackages: false,
saveWorkspaceProtocol: false,
workspace: true,
}, ['@pnpm.e2e/hello-world-js-bin'])
} catch (_err: any) { // eslint-disable-line
err = _err
}
expect(err.code).toBe('ERR_PNPM_NO_GLOBAL_BIN_DIR')
})