Skip to content

Commit

Permalink
fix: use the system default Node.js to check package compatibility (#…
Browse files Browse the repository at this point in the history
…3974)

close #3785
close #3673
  • Loading branch information
zkochan committed Nov 12, 2021
1 parent 7af48a2 commit 783cc10
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changeset/stale-mayflies-hear.md
Original file line number Diff line number Diff line change
@@ -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).
6 changes: 5 additions & 1 deletion packages/cli-utils/src/packageIsInstallable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) ??
Expand Down
15 changes: 12 additions & 3 deletions packages/cli-utils/src/readProjectManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/find-workspace-packages/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default async (
workspaceRoot: string,
opts?: {
engineStrict?: boolean
nodeVersion?: string
patterns?: string[]
}
) => {
Expand Down
2 changes: 2 additions & 0 deletions packages/package-is-installable/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
11 changes: 11 additions & 0 deletions packages/package-is-installable/src/getSystemNodeVersion.ts
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 2 additions & 1 deletion packages/package-is-installable/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
})
)
Expand Down
19 changes: 19 additions & 0 deletions packages/package-is-installable/test/getSystemNodeVersion.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
1 change: 1 addition & 0 deletions packages/pnpm/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 783cc10

Please sign in to comment.