fix: don't download node, when wanted node version is running (#8673)

close #8391

---------

Co-authored-by: Zoltan Kochan <z@kochan.io>
This commit is contained in:
Ardeshir Izadi
2024-10-21 02:53:47 +02:00
committed by GitHub
parent f066efcbd8
commit d04f7f2320
18 changed files with 169 additions and 5 deletions

View File

@@ -0,0 +1,7 @@
---
"@pnpm/plugin-commands-env": patch
---
pnpm no longer downloads the required `use-node-version` if the running node version is the same as the wanted version
The required `use-node-version` is no longer downloaded if the running Node version is the same as the wanted version [#8673](https://github.com/pnpm/pnpm/pull/8673).

View File

@@ -0,0 +1,5 @@
---
"@pnpm/env.system-node-version": major
---
Initial release.

View File

@@ -31,6 +31,7 @@
"dependencies": {
"@pnpm/cli-meta": "workspace:*",
"@pnpm/core-loggers": "workspace:*",
"@pnpm/env.system-node-version": "workspace:*",
"@pnpm/error": "workspace:*",
"@pnpm/types": "workspace:*",
"detect-libc": "catalog:",

View File

@@ -2,9 +2,9 @@ import {
installCheckLogger,
skippedOptionalDependencyLogger,
} from '@pnpm/core-loggers'
import { getSystemNodeVersion } from '@pnpm/env.system-node-version'
import { checkEngine, UnsupportedEngineError, type WantedEngine } from './checkEngine'
import { checkPlatform, UnsupportedPlatformError } from './checkPlatform'
import { getSystemNodeVersion } from './getSystemNodeVersion'
import { type SupportedArchitectures } from '@pnpm/types'
export type { Engine } from './checkEngine'

View File

@@ -12,6 +12,9 @@
{
"path": "../../cli/cli-meta"
},
{
"path": "../../env/system-node-version"
},
{
"path": "../../packages/core-loggers"
},

View File

@@ -35,6 +35,7 @@
"dependencies": {
"@pnpm/cli-utils": "workspace:*",
"@pnpm/config": "workspace:*",
"@pnpm/env.system-node-version": "workspace:*",
"@pnpm/error": "workspace:*",
"@pnpm/fetch": "workspace:*",
"@pnpm/node.fetcher": "workspace:*",

View File

@@ -2,6 +2,7 @@ import fs from 'fs'
import path from 'path'
import util from 'util'
import { type Config } from '@pnpm/config'
import { getSystemNodeVersion } from '@pnpm/env.system-node-version'
import { createFetchFromRegistry, type FetchFromRegistry } from '@pnpm/fetch'
import { globalInfo } from '@pnpm/logger'
import { fetchNode } from '@pnpm/node.fetcher'
@@ -40,7 +41,9 @@ export type NvmNodeCommandOptions = Pick<Config,
const nodeFetchPromises: Record<string, Promise<string>> = {}
export async function prepareExecutionEnv (config: NvmNodeCommandOptions, { extraBinPaths, executionEnv }: PrepareExecutionEnvOptions): Promise<PrepareExecutionEnvResult> {
if (!executionEnv?.nodeVersion) return { extraBinPaths: extraBinPaths ?? [] }
if (!executionEnv?.nodeVersion || `v${executionEnv.nodeVersion}` === getSystemNodeVersion()) {
return { extraBinPaths: extraBinPaths ?? [] }
}
let nodePathPromise = nodeFetchPromises[executionEnv.nodeVersion]
if (!nodePathPromise) {

View File

@@ -2,7 +2,7 @@ import AdmZip from 'adm-zip'
import { Response } from 'node-fetch'
import path from 'path'
import { Readable } from 'stream'
import { getNodeDir, getNodeBinDir, getNodeVersionsBaseDir, type NvmNodeCommandOptions } from '../lib/node'
import { getNodeDir, getNodeBinDir, getNodeVersionsBaseDir, type NvmNodeCommandOptions, prepareExecutionEnv } from '../lib/node'
import { tempDir } from '@pnpm/prepare'
const fetchMock = jest.fn(async (url: string) => {
@@ -84,3 +84,19 @@ test('get node version base dir', async () => {
const versionDir = getNodeVersionsBaseDir(process.cwd())
expect(versionDir).toBe(path.resolve(process.cwd(), 'nodejs'))
})
describe('prepareExecutionEnv', () => {
test('should not proceed to fetch Node.js if the process is already running in wanted node version', async () => {
fetchMock.mockImplementationOnce(() => {
throw new Error('prepareExecutionEnv should not proceed to fetch Node.js when wanted version is running')
})
await prepareExecutionEnv({
bin: '',
pnpmHomeDir: process.cwd(),
rawConfig: {},
}, {
executionEnv: { nodeVersion: process.versions.node },
})
})
})

View File

@@ -41,6 +41,9 @@
},
{
"path": "../node.resolver"
},
{
"path": "../system-node-version"
}
]
}

15
env/system-node-version/README.md vendored Normal file
View File

@@ -0,0 +1,15 @@
# @pnpm/env.system-node-version
> Detects the current system node version
[![npm version](https://img.shields.io/npm/v/@pnpm/env.system-node-version.svg)](https://www.npmjs.com/package/@pnpm/env.system-node-version)
## Installation
```sh
pnpm add @pnpm/env.system-node-version
```
## License
MIT

View File

@@ -0,0 +1,3 @@
const config = require('../../jest.config.js')
module.exports = config

44
env/system-node-version/package.json vendored Normal file
View File

@@ -0,0 +1,44 @@
{
"name": "@pnpm/env.system-node-version",
"version": "0.0.0",
"description": "Detects the current system node version",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"lib",
"!*.map"
],
"engines": {
"node": ">=18.12"
},
"scripts": {
"lint": "eslint \"src/**/*.ts\" \"test/**/*.ts\"",
"_test": "jest",
"test": "pnpm run compile && pnpm run _test",
"prepublishOnly": "pnpm run compile",
"compile": "tsc --build && pnpm run lint --fix"
},
"repository": "https://github.com/pnpm/pnpm/blob/main/env/system-node-version",
"keywords": [
"pnpm9",
"pnpm",
"env"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/pnpm/pnpm/issues"
},
"homepage": "https://github.com/pnpm/pnpm/blob/main/env/system-node-version#readme",
"dependencies": {
"@pnpm/cli-meta": "workspace:*",
"execa": "catalog:",
"mem": "catalog:"
},
"funding": "https://opencollective.com/pnpm",
"devDependencies": {
"@pnpm/env.system-node-version": "workspace:*"
},
"exports": {
".": "./lib/index.js"
}
}

View File

@@ -1,4 +1,4 @@
import { getSystemNodeVersionNonCached } from '../lib/getSystemNodeVersion'
import { getSystemNodeVersionNonCached } from '../lib'
import * as execa from 'execa'
jest.mock('execa', () => ({
@@ -18,4 +18,4 @@ test('getSystemNodeVersion() from a non-executable pnpm CLI', () => {
// @ts-expect-error
delete process['pkg']
expect(getSystemNodeVersionNonCached()).toBe(process.version)
})
})

View File

@@ -0,0 +1,17 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"noEmit": false,
"outDir": "../test.lib",
"rootDir": "."
},
"include": [
"**/*.ts",
"../../../__typings__/**/*.d.ts"
],
"references": [
{
"path": ".."
}
]
}

16
env/system-node-version/tsconfig.json vendored Normal file
View File

@@ -0,0 +1,16 @@
{
"extends": "@pnpm/tsconfig",
"compilerOptions": {
"outDir": "lib",
"rootDir": "src"
},
"include": [
"src/**/*.ts",
"../../__typings__/**/*.d.ts"
],
"references": [
{
"path": "../../cli/cli-meta"
}
]
}

View File

@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"include": [
"src/**/*.ts",
"test/**/*.ts",
"../../__typings__/**/*.d.ts"
]
}

22
pnpm-lock.yaml generated
View File

@@ -1524,6 +1524,9 @@ importers:
'@pnpm/core-loggers':
specifier: workspace:*
version: link:../../packages/core-loggers
'@pnpm/env.system-node-version':
specifier: workspace:*
version: link:../../env/system-node-version
'@pnpm/error':
specifier: workspace:*
version: link:../../packages/error
@@ -1835,6 +1838,9 @@ importers:
'@pnpm/config':
specifier: workspace:*
version: link:../../config/config
'@pnpm/env.system-node-version':
specifier: workspace:*
version: link:../system-node-version
'@pnpm/error':
specifier: workspace:*
version: link:../../packages/error
@@ -1921,6 +1927,22 @@ importers:
specifier: 'catalog:'
version: 1.0.0
env/system-node-version:
dependencies:
'@pnpm/cli-meta':
specifier: workspace:*
version: link:../../cli/cli-meta
execa:
specifier: 'catalog:'
version: safe-execa@0.1.2
mem:
specifier: 'catalog:'
version: 8.1.1
devDependencies:
'@pnpm/env.system-node-version':
specifier: workspace:*
version: 'link:'
exec/build-modules:
dependencies:
'@pnpm/calc-dep-state':