fix: env use should work on Windows (#6884)

close #6587
This commit is contained in:
Zoltan Kochan
2023-07-31 11:39:50 +03:00
committed by GitHub
parent b454bb07bf
commit 34724dd0f2
5 changed files with 58 additions and 3 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-env": patch
"pnpm": patch
---
`pnpm env use` should retry deleting the previous node.js executable [#6587](https://github.com/pnpm/pnpm/issues/6587).

View File

@@ -41,6 +41,7 @@
"@pnpm/store-path": "workspace:*",
"@zkochan/cmd-shim": "^6.0.0",
"@zkochan/rimraf": "^2.1.2",
"graceful-fs": "^4.2.11",
"is-windows": "^1.0.2",
"load-json-file": "^6.2.0",
"render-help": "^1.0.3",
@@ -53,6 +54,7 @@
"@pnpm/plugin-commands-env": "workspace:*",
"@pnpm/prepare": "workspace:*",
"@types/adm-zip": "^0.5.0",
"@types/graceful-fs": "^4.1.6",
"@types/is-windows": "^1.0.0",
"@types/semver": "7.3.13",
"adm-zip": "^0.5.10",

View File

@@ -1,4 +1,5 @@
import { promises as fs } from 'fs'
import gfs from 'graceful-fs'
import path from 'path'
import { PnpmError } from '@pnpm/error'
import { createFetchFromRegistry } from '@pnpm/fetch'
@@ -31,8 +32,10 @@ export async function envUse (opts: NvmNodeCommandOptions, params: string[]) {
const dest = getNodeExecPathInBinDir(opts.bin)
await symlinkDir(nodeDir, path.join(opts.pnpmHomeDir, CURRENT_NODE_DIRNAME))
try {
await fs.unlink(dest)
} catch (err) {}
gfs.unlinkSync(dest)
} catch (err: any) { // eslint-disable-line
if (err.code !== 'ENOENT') throw err
}
await symlinkOrHardLink(src, dest)
try {
let npmDir = nodeDir

View File

@@ -280,3 +280,41 @@ test('fail if there is no global bin directory', async () => {
}, ['use', 'lts'])
).rejects.toEqual(new PnpmError('CANNOT_MANAGE_NODE', 'Unable to manage Node.js because pnpm was not installed using the standalone installation script'))
})
test('use overrides the previous Node.js version', async () => {
tempDir()
const configDir = path.resolve('config')
await env.handler({
bin: process.cwd(),
configDir,
global: true,
pnpmHomeDir: process.cwd(),
rawConfig: {},
}, ['use', '16.4.0'])
const opts = {
env: {
[PATH]: `${process.cwd()}${path.delimiter}${process.env[PATH] as string}`,
},
extendEnv: false,
}
{
const { stdout } = execa.sync('node', ['-v'], opts)
expect(stdout.toString()).toBe('v16.4.0')
}
await env.handler({
bin: process.cwd(),
configDir,
global: true,
pnpmHomeDir: process.cwd(),
rawConfig: {},
}, ['use', '16.5.0'])
{
const { stdout } = execa.sync('node', ['-v'], opts)
expect(stdout.toString()).toBe('v16.5.0')
}
})

8
pnpm-lock.yaml generated
View File

@@ -990,6 +990,9 @@ importers:
'@zkochan/rimraf':
specifier: ^2.1.2
version: 2.1.2
graceful-fs:
specifier: ^4.2.11
version: 4.2.11(patch_hash=66ismxrei24sd5iv7rpq4zc5hq)
is-windows:
specifier: ^1.0.2
version: 1.0.2
@@ -1018,6 +1021,9 @@ importers:
'@types/adm-zip':
specifier: ^0.5.0
version: 0.5.0
'@types/graceful-fs':
specifier: ^4.1.6
version: 4.1.6
'@types/is-windows':
specifier: ^1.0.0
version: 1.0.0
@@ -9104,7 +9110,7 @@ packages:
resolution: {integrity: sha512-IO+MJPVhoqz+28h1qLAcBEH2+xHMK6MTyHJc7MTnnYb6wsoLR29POVGJ7LycmVXIqyy/4/2ShP5sUwTXuOwb/w==}
dependencies:
'@types/minimatch': 5.1.2
'@types/node': 20.4.5
'@types/node': 14.18.54
dev: true
/@types/graceful-fs@4.1.6: