fix: don't crash when use-node-version is set and there is no node.js (#8785)

close #8769
This commit is contained in:
Zoltan Kochan
2024-11-19 23:55:40 +01:00
committed by GitHub
parent 8c3b6713a3
commit e476b07eb4
4 changed files with 27 additions and 3 deletions

View File

@@ -0,0 +1,7 @@
---
"@pnpm/package-is-installable": patch
"@pnpm/env.system-node-version": patch
"pnpm": patch
---
Don't crash if the `use-node-version` setting is used and the system has no Node.js installed [#8769](https://github.com/pnpm/pnpm/issues/8769).

View File

@@ -86,7 +86,7 @@ export function checkPackage (
(manifest.engines == null)
? null
: checkEngine(pkgId, manifest.engines, {
node: options.nodeVersion ?? getSystemNodeVersion(),
node: options.nodeVersion ?? getSystemNodeVersion() ?? process.version,
pnpm: options.pnpmVersion,
})
)

View File

@@ -2,9 +2,14 @@ import { detectIfCurrentPkgIsExecutable } from '@pnpm/cli-meta'
import mem from 'mem'
import * as execa from 'execa'
export function getSystemNodeVersionNonCached (): string {
export function getSystemNodeVersionNonCached (): string | undefined {
if (detectIfCurrentPkgIsExecutable()) {
return execa.sync('node', ['--version']).stdout.toString()
try {
return execa.sync('node', ['--version']).stdout.toString()
} catch {
// Node.js is not installed on the system
return undefined
}
}
return process.version
}

View File

@@ -19,3 +19,15 @@ test('getSystemNodeVersion() from a non-executable pnpm CLI', () => {
delete process['pkg']
expect(getSystemNodeVersionNonCached()).toBe(process.version)
})
test('getSystemNodeVersion() returns undefined if execa.sync throws an error', () => {
// Mock execa.sync to throw an error
(execa.sync as jest.Mock).mockImplementationOnce(() => {
throw new Error('not found: node')
})
// @ts-expect-error
process['pkg'] = {}
expect(getSystemNodeVersionNonCached()).toBeUndefined()
expect(execa.sync).toHaveBeenCalledWith('node', ['--version'])
})