mirror of
https://github.com/pnpm/action-setup.git
synced 2026-04-04 11:20:11 +08:00
fix: copy pnpm.exe to .bin/ on Windows for standalone mode
The .cmd wrapper approach didn't work because CMD doesn't properly wait for extensionless PE binaries. Instead, copy the actual .exe (and .cmd for pnpx) from @pnpm/exe into .bin/ so PATHEXT finds pnpm.exe directly, bypassing npm's broken node-wrapping shim.
This commit is contained in:
207
dist/index.js
vendored
207
dist/index.js
vendored
File diff suppressed because one or more lines are too long
@@ -1,6 +1,6 @@
|
|||||||
import { addPath, exportVariable } from '@actions/core'
|
import { addPath, exportVariable } from '@actions/core'
|
||||||
import { spawn } from 'child_process'
|
import { spawn } from 'child_process'
|
||||||
import { rm, writeFile, mkdir, symlink } from 'fs/promises'
|
import { rm, writeFile, mkdir, symlink, link, copyFile } from 'fs/promises'
|
||||||
import { readFileSync, existsSync } from 'fs'
|
import { readFileSync, existsSync } from 'fs'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
import util from 'util'
|
import util from 'util'
|
||||||
@@ -41,17 +41,23 @@ export async function runSelfInstaller(inputs: Inputs): Promise<number> {
|
|||||||
|
|
||||||
// On Windows, npm creates .cmd wrappers that invoke bin entries through
|
// On Windows, npm creates .cmd wrappers that invoke bin entries through
|
||||||
// `node`, but @pnpm/exe bins are native executables — not JavaScript.
|
// `node`, but @pnpm/exe bins are native executables — not JavaScript.
|
||||||
// Overwrite the wrappers to invoke the binaries directly.
|
// Place pnpm.exe directly in .bin/ so that PATHEXT resolution finds the
|
||||||
|
// .exe before the broken .cmd wrapper.
|
||||||
if (process.platform === 'win32' && standalone) {
|
if (process.platform === 'win32' && standalone) {
|
||||||
|
const exeDir = path.join(dest, 'node_modules', '@pnpm', 'exe')
|
||||||
for (const name of ['pnpm', 'pnpx']) {
|
for (const name of ['pnpm', 'pnpx']) {
|
||||||
await writeFile(
|
const exe = path.join(exeDir, `${name}.exe`)
|
||||||
path.join(pnpmHome, `${name}.cmd`),
|
const cmd = path.join(exeDir, `${name}.cmd`)
|
||||||
`@"%~dp0\\..\\@pnpm\\exe\\${name}" %*\r\n`,
|
if (existsSync(exe)) {
|
||||||
)
|
await copyFile(exe, path.join(pnpmHome, `${name}.exe`))
|
||||||
|
} else if (existsSync(cmd)) {
|
||||||
|
await copyFile(cmd, path.join(pnpmHome, `${name}.cmd`))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure pnpm bin link exists — npm ci sometimes doesn't create it
|
// Ensure pnpm bin link exists — npm ci sometimes doesn't create it
|
||||||
|
if (process.platform !== 'win32') {
|
||||||
const pnpmBinLink = path.join(pnpmHome, 'pnpm')
|
const pnpmBinLink = path.join(pnpmHome, 'pnpm')
|
||||||
if (!existsSync(pnpmBinLink)) {
|
if (!existsSync(pnpmBinLink)) {
|
||||||
await mkdir(pnpmHome, { recursive: true })
|
await mkdir(pnpmHome, { recursive: true })
|
||||||
@@ -60,30 +66,18 @@ export async function runSelfInstaller(inputs: Inputs): Promise<number> {
|
|||||||
: path.join('..', 'pnpm', 'bin', 'pnpm.mjs')
|
: path.join('..', 'pnpm', 'bin', 'pnpm.mjs')
|
||||||
await symlink(target, pnpmBinLink)
|
await symlink(target, pnpmBinLink)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const bootstrapPnpm = standalone
|
const bootstrapPnpm = standalone
|
||||||
? path.join(dest, 'node_modules', '@pnpm', 'exe', 'pnpm')
|
? path.join(dest, 'node_modules', '@pnpm', 'exe', process.platform === 'win32' ? 'pnpm.exe' : 'pnpm')
|
||||||
: path.join(dest, 'node_modules', 'pnpm', 'bin', 'pnpm.mjs')
|
: path.join(dest, 'node_modules', 'pnpm', 'bin', 'pnpm.mjs')
|
||||||
|
|
||||||
// Determine the target version
|
// Determine the target version
|
||||||
const targetVersion = readTargetVersion({ version, packageJsonFile })
|
const targetVersion = readTargetVersion({ version, packageJsonFile })
|
||||||
|
|
||||||
if (targetVersion) {
|
if (targetVersion) {
|
||||||
let cmd: string
|
const cmd = standalone ? bootstrapPnpm : process.execPath
|
||||||
let args: string[]
|
const args = standalone ? ['self-update', targetVersion] : [bootstrapPnpm, 'self-update', targetVersion]
|
||||||
if (standalone) {
|
|
||||||
if (process.platform === 'win32') {
|
|
||||||
// Use the corrected .cmd wrapper on Windows
|
|
||||||
cmd = path.join(pnpmHome, 'pnpm.cmd')
|
|
||||||
args = ['self-update', targetVersion]
|
|
||||||
} else {
|
|
||||||
cmd = bootstrapPnpm
|
|
||||||
args = ['self-update', targetVersion]
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
cmd = process.execPath
|
|
||||||
args = [bootstrapPnpm, 'self-update', targetVersion]
|
|
||||||
}
|
|
||||||
const exitCode = await runCommand(cmd, args, { cwd: dest })
|
const exitCode = await runCommand(cmd, args, { cwd: dest })
|
||||||
if (exitCode !== 0) {
|
if (exitCode !== 0) {
|
||||||
return exitCode
|
return exitCode
|
||||||
|
|||||||
Reference in New Issue
Block a user