From e570adc10185bcfb7790e29d85258a9cbbf4f815 Mon Sep 17 00:00:00 2001 From: Zoltan Kochan Date: Sun, 26 Feb 2023 05:39:44 +0200 Subject: [PATCH] fix(env): fail with a meaningful error when no pnpm home dir is found (#6134) close #6095 close #3865 --- .changeset/shy-drinks-beam.md | 6 ++++++ env/plugin-commands-env/src/env.ts | 5 +++++ env/plugin-commands-env/test/env.test.ts | 14 ++++++++++++++ 3 files changed, 25 insertions(+) create mode 100644 .changeset/shy-drinks-beam.md diff --git a/.changeset/shy-drinks-beam.md b/.changeset/shy-drinks-beam.md new file mode 100644 index 000000000..4be03789a --- /dev/null +++ b/.changeset/shy-drinks-beam.md @@ -0,0 +1,6 @@ +--- +"@pnpm/plugin-commands-env": patch +"pnpm": patch +--- + +`pnpm env -g` should fail with a meaningful error message if pnpm cannot find the pnpm home directory, which is the directory into which Node.js is installed. diff --git a/env/plugin-commands-env/src/env.ts b/env/plugin-commands-env/src/env.ts index 66283ff83..7e039c504 100644 --- a/env/plugin-commands-env/src/env.ts +++ b/env/plugin-commands-env/src/env.ts @@ -87,6 +87,11 @@ export async function handler (opts: NvmNodeCommandOptions, params: string[]) { hint: help(), }) } + if (opts.global && !opts.bin) { + throw new PnpmError('CANNOT_MANAGE_NODE', 'Unable to manage Node.js because pnpm was not installed using the standalon installation script', { + hint: 'If you want to manage Node.js with pnpm, you need to remove any Node.js that was installed by other tools, then install pnpm using one of the standalone scripts that are provided on the installation page: https://pnpm.io/installation', + }) + } switch (params[0]) { case 'use': { return envUse(opts, params.slice(1)) diff --git a/env/plugin-commands-env/test/env.test.ts b/env/plugin-commands-env/test/env.test.ts index cf7333780..008464bc2 100644 --- a/env/plugin-commands-env/test/env.test.ts +++ b/env/plugin-commands-env/test/env.test.ts @@ -265,3 +265,17 @@ describe('env list', () => { expect(versions.every(version => semver.satisfies(version, '16'))).toBeTruthy() }) }) + +test('fail if there is no global bin directory', async () => { + tempDir() + + await expect( + env.handler({ + // @ts-expect-error + bin: undefined, + global: true, + pnpmHomeDir: process.cwd(), + rawConfig: {}, + }, ['use', 'lts']) + ).rejects.toEqual(new PnpmError('CANNOT_MANAGE_NODE', 'Unable to manage Node.js because pnpm was not installed using the standalon installation script')) +})