fix: use the right node exec path when pnpm is an exe (#3870)

This commit is contained in:
Zoltan Kochan
2021-10-14 15:12:07 +03:00
committed by GitHub
parent 913d97a050
commit cd980742c7
7 changed files with 31 additions and 2 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/plugin-commands-installation": patch
---
Always prefer the system-default Node.js executable when pinning a globally installed package to a Node.js version.

View File

@@ -77,6 +77,7 @@
"@pnpm/store-connection-manager": "workspace:3.0.19",
"@pnpm/types": "workspace:7.4.0",
"@zkochan/table": "^1.0.0",
"@zkochan/which": "^2.0.3",
"camelcase-keys": "^6.2.2",
"chalk": "^4.1.0",
"enquirer": "^2.3.6",

View File

@@ -19,6 +19,7 @@ import logger from '@pnpm/logger'
import { sequenceGraph } from '@pnpm/sort-packages'
import getPinnedVersion from './getPinnedVersion'
import getSaveType from './getSaveType'
import nodeExecPath from './nodeExecPath'
import recursive, { createMatcher, matchDependencies } from './recursive'
import updateToLatestSpecsFromManifest, { createLatestSpecs } from './updateToLatestSpecsFromManifest'
import { createWorkspaceSpecs, updateToWorkspacePackagesFromManifest } from './updateWorkspaceDependencies'
@@ -175,7 +176,7 @@ when running add/update with the --workspace option')
workspacePackages,
}
if (opts.global) {
installOpts['nodeExecPath'] = process.env.NODE ?? process.execPath
installOpts['nodeExecPath'] = await nodeExecPath()
}
let { manifest, writeProjectManifest } = await tryReadProjectManifest(opts.dir, opts)

View File

@@ -0,0 +1,14 @@
import { promises as fs } from 'fs'
import which from '@zkochan/which'
export default async function () {
try {
// The system default Node.js executable is prefered
// not the one used to run the pnpm CLI.
const nodeExecPath = await which('node')
return fs.realpath(nodeExecPath)
} catch (err) {
if (err['code'] !== 'ENOENT') throw err
return process.env.NODE ?? process.execPath
}
}

View File

@@ -4,6 +4,7 @@ import { add } from '@pnpm/plugin-commands-installation'
import prepare from '@pnpm/prepare'
import { REGISTRY_MOCK_PORT } from '@pnpm/registry-mock'
import tempy from 'tempy'
import nodeExecPath from '../lib/nodeExecPath'
const REGISTRY_URL = `http://localhost:${REGISTRY_MOCK_PORT}`
const tmp = tempy.directory()
@@ -49,5 +50,5 @@ test('globally installed package is linked with active version of Node.js', asyn
).toBeTruthy()
const shimContent = await fs.readFile('node_modules/.bin/hello-world-js-bin', 'utf-8')
expect(shimContent).toContain(process.env.NODE)
expect(shimContent).toContain(await nodeExecPath())
})

2
pnpm-lock.yaml generated
View File

@@ -1932,6 +1932,7 @@ importers:
'@types/sinon': ^9.0.11
'@types/zkochan__table': npm:@types/table@6.0.0
'@zkochan/table': ^1.0.0
'@zkochan/which': ^2.0.3
camelcase-keys: ^6.2.2
chalk: ^4.1.0
enquirer: ^2.3.6
@@ -1979,6 +1980,7 @@ importers:
'@pnpm/store-connection-manager': link:../store-connection-manager
'@pnpm/types': link:../types
'@zkochan/table': 1.0.0
'@zkochan/which': 2.0.3
camelcase-keys: 6.2.2
chalk: 4.1.2
enquirer: 2.3.6

5
typings/local.d.ts vendored
View File

@@ -43,6 +43,11 @@ declare module '@zkochan/npm-package-arg' {
export = anything;
}
declare module '@zkochan/which' {
const anything: any;
export = anything;
}
declare module 'anonymous-npm-registry-client' {
const anything: any;
export = anything;