fix: use the system default Node.js to check package compatibility (#3974)

close #3785
close #3673
This commit is contained in:
Zoltan Kochan
2021-11-12 04:32:14 +02:00
committed by GitHub
parent 7af48a2da6
commit 783cc1051c
10 changed files with 63 additions and 5 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/package-is-installable": patch
"pnpm": patch
---
Use the system default Node.js version to check package compatibility [#3785](https://github.com/pnpm/pnpm/issues/3785).

View File

@@ -11,12 +11,16 @@ export function packageIsInstallable (
},
opts: {
engineStrict?: boolean
nodeVersion?: string
}
) {
const pnpmVersion = packageManager.name === 'pnpm'
? packageManager.stableVersion
: undefined
const err = checkPackage(pkgPath, pkg, { pnpmVersion })
const err = checkPackage(pkgPath, pkg, {
nodeVersion: opts.nodeVersion,
pnpmVersion,
})
if (err === null) return
if (
(err instanceof UnsupportedEngineError && err.wanted.pnpm) ??

View File

@@ -4,7 +4,10 @@ import { packageIsInstallable } from './packageIsInstallable'
export async function readProjectManifest (
projectDir: string,
opts: { engineStrict?: boolean }
opts: {
engineStrict?: boolean
nodeVersion?: string
}
): Promise<{
fileName: string
manifest: ProjectManifest
@@ -17,7 +20,10 @@ export async function readProjectManifest (
export async function readProjectManifestOnly (
projectDir: string,
opts: { engineStrict?: boolean }
opts: {
engineStrict?: boolean
nodeVersion?: string
}
): Promise<ProjectManifest> {
const manifest = await utils.readProjectManifestOnly(projectDir)
packageIsInstallable(projectDir, manifest as any, opts) // eslint-disable-line @typescript-eslint/no-explicit-any
@@ -26,7 +32,10 @@ export async function readProjectManifestOnly (
export async function tryReadProjectManifest (
projectDir: string,
opts: { engineStrict?: boolean }
opts: {
engineStrict?: boolean
nodeVersion?: string
}
): Promise<{
fileName: string
manifest: ProjectManifest | null

View File

@@ -11,6 +11,7 @@ export default async (
workspaceRoot: string,
opts?: {
engineStrict?: boolean
nodeVersion?: string
patterns?: string[]
}
) => {

View File

@@ -32,6 +32,8 @@
"@pnpm/core-loggers": "workspace:6.0.6",
"@pnpm/error": "workspace:2.0.0",
"@pnpm/types": "workspace:7.6.0",
"execa": "npm:safe-execa@^0.1.1",
"mem": "^8.0.0",
"semver": "^7.3.4"
},
"devDependencies": {

View File

@@ -0,0 +1,11 @@
import mem from 'mem'
import * as execa from 'execa'
export function getSystemNodeVersionNonCached () {
if (process['pkg'] != null) {
return execa.sync('node', ['--version']).stdout.toString()
}
return process.version
}
export const getSystemNodeVersion = mem(getSystemNodeVersionNonCached)

View File

@@ -4,6 +4,7 @@ import {
} from '@pnpm/core-loggers'
import checkEngine, { UnsupportedEngineError, WantedEngine } from './checkEngine'
import checkPlatform, { UnsupportedPlatformError } from './checkPlatform'
import { getSystemNodeVersion } from './getSystemNodeVersion'
export { Engine } from './checkEngine'
export { Platform, WantedPlatform } from './checkPlatform'
@@ -79,7 +80,7 @@ export function checkPackage (
(manifest.engines == null)
? null
: checkEngine(pkgId, manifest.engines, {
node: options.nodeVersion ?? process.version,
node: options.nodeVersion ?? getSystemNodeVersion(),
pnpm: options.pnpmVersion,
})
)

View File

@@ -0,0 +1,19 @@
import { getSystemNodeVersionNonCached } from '@pnpm/package-is-installable/lib/getSystemNodeVersion'
import * as execa from 'execa'
jest.mock('execa', () => ({
sync: jest.fn(() => ({
stdout: 'v10.0.0',
})),
}))
test('getSystemNodeVersion() executed from an executable pnpm CLI', () => {
process['pkg'] = {}
expect(getSystemNodeVersionNonCached()).toBe('v10.0.0')
expect(execa.sync).toHaveBeenCalledWith('node', ['--version'])
})
test('getSystemNodeVersion() from a non-executable pnpm CLI', () => {
delete process['pkg']
expect(getSystemNodeVersionNonCached()).toBe(process.version)
})

View File

@@ -253,6 +253,7 @@ export default async function run (inputArgv: string[]) {
if (config.useNodeVersion != null) {
const nodePath = await node.getNodeBinDir(config)
config.extraBinPaths.push(nodePath)
config.nodeVersion = config.useNodeVersion
}
let result = pnpmCmds[cmd ?? 'help'](
// TypeScript doesn't currently infer that the type of config

4
pnpm-lock.yaml generated
View File

@@ -1773,11 +1773,15 @@ importers:
'@pnpm/package-is-installable': 'link:'
'@pnpm/types': workspace:7.6.0
'@types/semver': ^7.3.4
execa: npm:safe-execa@^0.1.1
mem: ^8.0.0
semver: ^7.3.4
dependencies:
'@pnpm/core-loggers': link:../core-loggers
'@pnpm/error': link:../error
'@pnpm/types': link:../types
execa: /safe-execa/0.1.1
mem: 8.1.1
semver: 7.3.5
devDependencies:
'@pnpm/logger': 4.0.0