Compare commits

...

11 Commits

Author SHA1 Message Date
Zoltan Kochan
aec59b9f6c fix: fall back to pnpm shim in same directory for self-update bin/
ensureAliasLinks had a hardcoded relative path to @pnpm/exe/pnpm which
only works from node_modules/.bin/. In self-update's bin/ directory
($PNPM_HOME/bin/), that path doesn't resolve. Now falls back to using
the pnpm shim in the same directory when the package path doesn't exist.
2026-03-26 20:24:03 +01:00
Zoltan Kochan
76bfe9d01c fix: also create alias links in $PNPM_HOME/bin/
self-update links bins to $PNPM_HOME/bin/ which comes first in PATH.
Without alias links there, the broken pnx shim from self-update
(pointing to placeholder files) takes precedence over our .bin/pnx.
2026-03-26 19:14:42 +01:00
Zoltan Kochan
188c8307ce fix: point pn to pnpm binary directly, not to @pnpm/exe/pn
pnpm self-update only replaces the pnpm binary — it does not update
other files in the @pnpm/exe package (setup.js, pn, pnpx, pnx all
remain from the v10 bootstrap). So @pnpm/exe/pn may not exist at all.

Instead of relying on @pnpm/exe/pn, create the aliases directly:
- pn → symlink to the pnpm binary
- pnpx/pnx → shell scripts that exec "pnpm dlx"

Also remove the setup.js call after self-update since it's no longer
needed and would run the v10 version which doesn't know about pn.
2026-03-26 18:58:55 +01:00
Zoltan Kochan
b179ac1ba6 fix: force-replace npm's broken bin shims for pn/pnx aliases
npm creates bin shims in .bin/ that point to an isolated copy in
.bin/.tools/. After self-update, setup.js fixes the main copy in
node_modules/@pnpm/exe/ but the .tools copy retains stale placeholder
files. Always replace the bin links so they point directly to the
fixed files instead of npm's broken .tools shims.
2026-03-26 18:54:54 +01:00
Zoltan Kochan
51e56d41e9 fix: run setup.js after self-update to create pn/pnx hardlinks
self-update replaces the @pnpm/exe package files but does not re-run
preinstall scripts. This leaves pn/pnpx/pnx as placeholder files
("This file intentionally left blank") instead of hardlinks to the
actual binary. Run setup.js explicitly after self-update to fix this.
2026-03-26 18:49:59 +01:00
Zoltan Kochan
11687bb3d2 fix: handle Windows with .cmd/.ps1 shims and add tests
- Extract ensureAliasLinks to its own module for testability
- On Windows, create .cmd and .ps1 shims instead of symlinks
- On Unix, create symlinks (as before)
- Skip alias creation when targets don't exist (pnpm v10)
- Add vitest and 8 tests covering unix/windows/skip/no-overwrite
2026-03-26 18:41:05 +01:00
Zoltan Kochan
747414e7da feat: create pn and pnx alias symlinks for pnpm v11+
After self-update, create symlinks for pn, pnpx, and pnx in
node_modules/.bin if the target files exist in the installed package.
This enables the short aliases introduced in pnpm v11.
2026-03-26 18:34:35 +01:00
Zoltan Kochan
62bce64275 fix: extract pnpm version from packageManager field instead of returning undefined (#216)
When packageManager is set to e.g. "pnpm@9.1.0+sha...", strip the
"pnpm@" prefix and any "+sha..." hash suffix so the action installs
the correct version. Previously returning undefined caused failures
on Windows.
2026-03-25 13:59:54 +01:00
Zoltan Kochan
58e6119fe4 feat!: replace bundled pnpm binary with npm + lockfile bootstrap (#212)
* feat!: replace bundled pnpm binary with npm + lockfile bootstrap

Remove the 9MB bundled pnpm.cjs/worker.js and instead use npm ci with
committed package-lock.json files (~5KB) to install a bootstrap pnpm,
which then installs the target version with integrity verification via
the project's pnpm-lock.yaml.

Also switch from ncc to esbuild and modernize to ESM.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: bundle as CJS to support @actions/* packages

The @actions/* packages use CJS require() for Node.js builtins,
which fails with "Dynamic require of 'os' is not supported" when
bundled as ESM. Switch esbuild output to CJS format.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: remove "type": "module" from package.json

Node.js treats dist/index.js as ESM due to "type": "module",
but the bundle uses CJS require() calls. Remove the field so
Node.js defaults to CJS for .js files.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: remove packageManager field and fix Windows npm spawn

- Remove packageManager from package.json to avoid version conflict
  when the action tests against itself (uses: ./)
- Use shell: true on Windows so spawn can find npm.cmd

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: always use pnpm (not @pnpm/exe) for bootstrap and update lockfile

The bootstrap only needs regular pnpm to install the target package.
@pnpm/exe requires install scripts which we skip with --ignore-scripts.
Also regenerate pnpm-lock.yaml to match current package.json.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: use --no-lockfile for target install

--lockfile-dir pointing to GITHUB_WORKSPACE causes the bootstrap pnpm
to use the project's pnpm-lock.yaml (which tracks project deps, not
pnpm itself), corrupting the install. Revert to --no-lockfile for now.
Lockfile-based integrity verification can be added when pnpm v11 has
proper support for verifying the pnpm package itself.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: run bootstrap pnpm via node instead of bin shim

Use `node .../pnpm/bin/pnpm.cjs` to run the bootstrap pnpm, matching
the approach used by the old bundled pnpm.cjs. This avoids issues with
the .bin symlink on different platforms.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* refactor: use pnpm self-update instead of installing target separately

- Bootstrap pnpm via npm ci (verified by lockfile)
- Use `pnpm self-update <version>` for explicit version
- Let pnpm handle packageManager field automatically
- Remove standalone/exe-specific install logic (pnpm handles this)
- Update tests to not run pnpm install against the action repo itself

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* feat: support standalone mode with @pnpm/exe bootstrap

- When standalone=true, bootstrap with @pnpm/exe via npm ci
- When standalone=false, bootstrap with pnpm via npm ci
- Both use pnpm self-update to reach the target version
- Remove --ignore-scripts from npm ci so @pnpm/exe install scripts run
- Add standalone test back to CI

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* debug: add logging to diagnose pnpm not found on PATH

Log .bin directory contents after npm ci to understand why
pnpm binary is not found in subsequent CI steps.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: ensure pnpm bin link exists after npm ci

npm ci sometimes doesn't create the .bin/pnpm symlink for
@pnpm/exe (observed on Linux CI). Manually create the symlink
if it's missing after npm ci completes.

This fixes the case where standalone=true with no explicit version
(relying on packageManager field) — pnpm self-update wouldn't run,
leaving .bin empty and pnpm not found on PATH.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

* fix: add PNPM_HOME/bin to PATH for pnpm v11

pnpm v11 moved global binaries from PNPM_HOME to PNPM_HOME/bin.
Add the new bin subdirectory to PATH so that pnpm's global bin
directory check passes. This is backwards compatible — the extra
PATH entry is harmless for older pnpm versions.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix: add packages field to pnpm-workspace.yaml

pnpm v9 requires the packages field in pnpm-workspace.yaml.
Without it, `pnpm --version` fails with "packages field missing or empty".

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

* fix pnpm-workspace.yaml

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-21 14:02:31 +01:00
axel7083
2e223e0f0d chore(workflows): adding pr-check.yaml to validate dist folder (#213)
* chore(workflows): adding pr-check.yaml to validate dist/index.js

Signed-off-by: axel7083 <42176370+axel7083@users.noreply.github.com>

* fix: update dist/index.js

Signed-off-by: axel7083 <42176370+axel7083@users.noreply.github.com>

---------

Signed-off-by: axel7083 <42176370+axel7083@users.noreply.github.com>
2026-03-19 14:52:48 +01:00
Zoltan Kochan
fc06bc1257 feat!: run the action on Node.js 24 (#205) 2026-03-13 11:30:26 +01:00
17 changed files with 1880 additions and 237561 deletions

28
.github/workflows/pr-check.yaml vendored Normal file
View File

@@ -0,0 +1,28 @@
name: pr-check
on: [ pull_request ]
permissions:
contents: read
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}
cancel-in-progress: true
jobs:
check-dist:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
- name: Setup pnpm
uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v5.0.0
with:
run_install: true
version: 9
- name: Update dist/index.js
run: pnpm run build
- name: Check for uncommitted changes in dist
run: git diff --exit-code dist/index.js

View File

@@ -32,8 +32,16 @@ jobs:
- name: 'Test: which' - name: 'Test: which'
run: which pnpm; which pnpx run: which pnpm; which pnpx
- name: 'Test: install' - name: 'Test: version'
run: pnpm install run: pnpm --version
- name: 'Test: install in a fresh project'
run: |
mkdir /tmp/test-project
cd /tmp/test-project
pnpm init
pnpm add is-odd
shell: bash
test_dest: test_dest:
name: Test with dest name: Test with dest
@@ -62,8 +70,8 @@ jobs:
- name: 'Test: which' - name: 'Test: which'
run: which pnpm && which pnpx run: which pnpm && which pnpx
- name: 'Test: install' - name: 'Test: version'
run: pnpm install run: pnpm --version
test_standalone: test_standalone:
name: Test with standalone name: Test with standalone
@@ -74,14 +82,9 @@ jobs:
fail-fast: false fail-fast: false
matrix: matrix:
os: os:
# macos is excluded from this test because node 12 is no longer available on this platform
- ubuntu-latest - ubuntu-latest
- windows-latest - windows-latest
standalone:
- true
- false
steps: steps:
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1 - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
@@ -89,36 +92,21 @@ jobs:
uses: ./ uses: ./
with: with:
version: 9.15.0 version: 9.15.0
standalone: ${{ matrix.standalone }} standalone: true
- name: install Node.js - name: 'Test: which'
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
with:
# pnpm@7.0.0 is not compatible with Node.js 12
node-version: 12.22.12
- name: 'Test: which (pnpm)'
run: which pnpm run: which pnpm
- name: 'Test: which (pnpx)' - name: 'Test: version'
if: matrix.standalone == false run: pnpm --version
run: which pnpx
- name: 'Test: install when standalone is true' - name: 'Test: install in a fresh project'
if: matrix.standalone
run: pnpm install
- name: 'Test: install when standalone is false'
if: matrix.standalone == false
# Since the default shell on windows runner is pwsh, we specify bash explicitly
shell: bash
run: | run: |
if pnpm install; then mkdir /tmp/test-standalone
echo "pnpm install should fail" cd /tmp/test-standalone
exit 1 pnpm init
else pnpm add is-odd
echo "pnpm install failed as expected" shell: bash
fi
test_run_install: test_run_install:
name: 'Test with run_install (${{ matrix.run_install.name }}, ${{ matrix.os }})' name: 'Test with run_install (${{ matrix.run_install.name }}, ${{ matrix.os }})'
@@ -137,11 +125,6 @@ jobs:
run_install: run_install:
- name: 'null' - name: 'null'
value: 'null' value: 'null'
- name: 'empty object'
value: '{}'
- name: 'recursive'
value: |
recursive: true
- name: 'global' - name: 'global'
value: | value: |
args: args:
@@ -149,15 +132,6 @@ jobs:
- --global-dir=./pnpm-global - --global-dir=./pnpm-global
- npm - npm
- yarn - yarn
- name: 'array'
value: |
- {}
- recursive: true
- args:
- --global
- --global-dir=./pnpm-global
- npm
- yarn
steps: steps:
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1 - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
@@ -171,5 +145,5 @@ jobs:
- name: 'Test: which' - name: 'Test: which'
run: which pnpm; which pnpx run: which pnpm; which pnpx
- name: 'Test: install' - name: 'Test: version'
run: pnpm install run: pnpm --version

2
.gitignore vendored
View File

@@ -2,8 +2,6 @@ node_modules
*.log *.log
/dist/* /dist/*
!/dist/index.js !/dist/index.js
!/dist/pnpm.cjs
!/dist/worker.js
tmp tmp
temp temp
*.tmp *.tmp

View File

@@ -37,6 +37,6 @@ outputs:
bin_dest: bin_dest:
description: Location of `pnpm` and `pnpx` command description: Location of `pnpm` and `pnpx` command
runs: runs:
using: node20 using: node24
main: dist/index.js main: dist/index.js
post: dist/index.js post: dist/index.js

295
dist/index.js vendored

File diff suppressed because one or more lines are too long

220780
dist/pnpm.cjs vendored

File diff suppressed because one or more lines are too long

16625
dist/worker.js vendored

File diff suppressed because it is too large Load Diff

View File

@@ -1,10 +1,10 @@
{ {
"private": true, "private": true,
"scripts": { "scripts": {
"build:ncc": "ncc build --minify --no-source-map-register --no-cache dist/tsc/index.js --out dist/", "build:bundle": "esbuild src/index.ts --bundle --platform=node --target=node24 --format=cjs --minify --outfile=dist/index.js --loader:.json=json",
"build": "tsc && pnpm run build:ncc", "build": "pnpm run build:bundle",
"start": "pnpm run build && sh ./run.sh", "test": "vitest run",
"update-pnpm-dist": "pnpm install && cp ./node_modules/pnpm/dist/pnpm.cjs ./dist/pnpm.cjs && cp ./node_modules/pnpm/dist/worker.js ./dist/worker.js" "start": "pnpm run build && sh ./run.sh"
}, },
"dependencies": { "dependencies": {
"@actions/cache": "^4.1.0", "@actions/cache": "^4.1.0",
@@ -12,14 +12,14 @@
"@actions/exec": "^1.1.1", "@actions/exec": "^1.1.1",
"@actions/glob": "^0.5.0", "@actions/glob": "^0.5.0",
"@types/expand-tilde": "^2.0.2", "@types/expand-tilde": "^2.0.2",
"@types/node": "^20.11.5", "@types/node": "^22.0.0",
"expand-tilde": "^2.0.2", "expand-tilde": "^2.0.2",
"yaml": "^2.3.4", "yaml": "^2.3.4",
"zod": "^3.22.4" "zod": "^3.22.4"
}, },
"devDependencies": { "devDependencies": {
"@vercel/ncc": "^0.38.1", "esbuild": "^0.27.4",
"pnpm": "^8.14.3", "typescript": "^5.3.3",
"typescript": "^5.3.3" "vitest": "^4.1.2"
} }
} }

1025
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

4
pnpm-workspace.yaml Normal file
View File

@@ -0,0 +1,4 @@
packages:
- '.'
allowBuilds:
esbuild: true

View File

@@ -0,0 +1,147 @@
{
"name": "bootstrap-exe",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"dependencies": {
"@pnpm/exe": "10.32.1"
}
},
"node_modules/@pnpm/exe": {
"version": "10.32.1",
"resolved": "https://registry.npmjs.org/@pnpm/exe/-/exe-10.32.1.tgz",
"integrity": "sha512-baEtwHeZwmZAdBuuDDL6tbdGg5KpxhPxr3QFfYTGXvY6ws+Z1bN0mQ7ZjcaXBSC1HuLpVXnZ6NsBiaZ2DMv4vg==",
"hasInstallScript": true,
"license": "MIT",
"bin": {
"pnpm": "pnpm"
},
"funding": {
"url": "https://opencollective.com/pnpm"
},
"optionalDependencies": {
"@pnpm/linux-arm64": "10.32.1",
"@pnpm/linux-x64": "10.32.1",
"@pnpm/macos-arm64": "10.32.1",
"@pnpm/macos-x64": "10.32.1",
"@pnpm/win-arm64": "10.32.1",
"@pnpm/win-x64": "10.32.1"
}
},
"node_modules/@pnpm/linux-arm64": {
"version": "10.32.1",
"resolved": "https://registry.npmjs.org/@pnpm/linux-arm64/-/linux-arm64-10.32.1.tgz",
"integrity": "sha512-6uB0B+XvunQwHGzIMk2JCkl4Ur6BtM4XbJSwB/mgpWmXDoX/KTJmgx2lodcTjgJSGSySCHfIVuTR9sj/F2D4EA==",
"cpu": [
"arm64"
],
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"bin": {
"pnpm": "pnpm"
},
"funding": {
"url": "https://opencollective.com/pnpm"
}
},
"node_modules/@pnpm/linux-x64": {
"version": "10.32.1",
"resolved": "https://registry.npmjs.org/@pnpm/linux-x64/-/linux-x64-10.32.1.tgz",
"integrity": "sha512-AM2tv23Fg7h+nV+adqA/SkZKUysSIEetHfBwYFl8ArgdgkqbGoQy0rAOdKYQBb920CqfexXfI8OA8kPCzRxYng==",
"cpu": [
"x64"
],
"license": "MIT",
"optional": true,
"os": [
"linux"
],
"bin": {
"pnpm": "pnpm"
},
"funding": {
"url": "https://opencollective.com/pnpm"
}
},
"node_modules/@pnpm/macos-arm64": {
"version": "10.32.1",
"resolved": "https://registry.npmjs.org/@pnpm/macos-arm64/-/macos-arm64-10.32.1.tgz",
"integrity": "sha512-Zr4JkhRbtGVsYgbuGZO0dq/6FPOn072Pdo0ubmqWtc14cUATKgAJD7efG03yqr3MLgtwFHgdtUzZ1WsaYAtUTA==",
"cpu": [
"arm64"
],
"license": "MIT",
"optional": true,
"os": [
"darwin"
],
"bin": {
"pnpm": "pnpm"
},
"funding": {
"url": "https://opencollective.com/pnpm"
}
},
"node_modules/@pnpm/macos-x64": {
"version": "10.32.1",
"resolved": "https://registry.npmjs.org/@pnpm/macos-x64/-/macos-x64-10.32.1.tgz",
"integrity": "sha512-Yk6q3oFDu//OniXJxfTSHo+aew1LX81FcbzJAtEkcCeTQ0SLbQT6J3QiOMNikp8n8IjNhsy+bn2bdkUxaw+akA==",
"cpu": [
"x64"
],
"license": "MIT",
"optional": true,
"os": [
"darwin"
],
"bin": {
"pnpm": "pnpm"
},
"funding": {
"url": "https://opencollective.com/pnpm"
}
},
"node_modules/@pnpm/win-arm64": {
"version": "10.32.1",
"resolved": "https://registry.npmjs.org/@pnpm/win-arm64/-/win-arm64-10.32.1.tgz",
"integrity": "sha512-P8rsP5IUetpYjr2iwggoswL2qUukYrJoToXWuMyo8immn58CsYxaXsHVQ1Oq1R3XMfmGGWTXLsiJuQ7H991MRg==",
"cpu": [
"arm64"
],
"license": "MIT",
"optional": true,
"os": [
"win32"
],
"bin": {
"pnpm": "pnpm.exe"
},
"funding": {
"url": "https://opencollective.com/pnpm"
}
},
"node_modules/@pnpm/win-x64": {
"version": "10.32.1",
"resolved": "https://registry.npmjs.org/@pnpm/win-x64/-/win-x64-10.32.1.tgz",
"integrity": "sha512-i24GwbtBO8ojrhp8WWimX7NgZs0UKH1171oRt6qcRL+a+FxE0Eggv2y0KP7ZI7F3+LZMarwr3tnYsZryfciUOg==",
"cpu": [
"x64"
],
"license": "MIT",
"optional": true,
"os": [
"win32"
],
"bin": {
"pnpm": "pnpm.exe"
},
"funding": {
"url": "https://opencollective.com/pnpm"
}
}
}
}

View File

@@ -0,0 +1,28 @@
{
"name": "bootstrap-pnpm",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"dependencies": {
"pnpm": "latest"
}
},
"node_modules/pnpm": {
"version": "10.32.1",
"resolved": "https://registry.npmjs.org/pnpm/-/pnpm-10.32.1.tgz",
"integrity": "sha512-pwaTjw6JrBRWtlY+q07fHR+vM2jRGR/FxZeQ6W3JGORFarLmfWE94QQ9LoyB+HMD5rQNT/7KnfFe8a1Wc0jyvg==",
"license": "MIT",
"bin": {
"pnpm": "bin/pnpm.cjs",
"pnpx": "bin/pnpx.cjs"
},
"engines": {
"node": ">=18.12"
},
"funding": {
"url": "https://opencollective.com/pnpm"
}
}
}
}

View File

@@ -0,0 +1,175 @@
import { describe, it, expect, beforeEach } from 'vitest'
import { ensureAliasLinks } from './ensureAliasLinks'
import { mkdtemp, mkdir, writeFile, readFile, readlink } from 'fs/promises'
import { existsSync } from 'fs'
import path from 'path'
import os from 'os'
async function createTempDir (): Promise<string> {
return mkdtemp(path.join(os.tmpdir(), 'alias-links-test-'))
}
async function setupStandaloneFixture (binDir: string): Promise<void> {
const exeDir = path.join(binDir, '..', '@pnpm', 'exe')
await mkdir(exeDir, { recursive: true })
// Only the pnpm binary exists — pn/pnpx/pnx may not exist after self-update
await writeFile(path.join(exeDir, 'pnpm'), '#!/bin/sh\necho pnpm\n', { mode: 0o755 })
}
async function setupNonStandaloneFixture (binDir: string): Promise<void> {
const pnpmBinDir = path.join(binDir, '..', 'pnpm', 'bin')
await mkdir(pnpmBinDir, { recursive: true })
await writeFile(path.join(pnpmBinDir, 'pnpm.cjs'), 'console.log("pnpm")\n')
}
describe('ensureAliasLinks', () => {
let binDir: string
beforeEach(async () => {
const tmpDir = await createTempDir()
binDir = path.join(tmpDir, 'node_modules', '.bin')
await mkdir(binDir, { recursive: true })
})
describe('standalone mode', () => {
it('creates pn as symlink to pnpm binary on unix', async () => {
await setupStandaloneFixture(binDir)
await ensureAliasLinks(binDir, true, 'linux')
const pnTarget = await readlink(path.join(binDir, 'pn'))
expect(pnTarget).toBe(path.join('..', '@pnpm', 'exe', 'pnpm'))
})
it('creates pnpx and pnx as shell scripts calling pnpm dlx on unix', async () => {
await setupStandaloneFixture(binDir)
await ensureAliasLinks(binDir, true, 'linux')
for (const name of ['pnpx', 'pnx']) {
const content = await readFile(path.join(binDir, name), 'utf8')
expect(content).toContain('pnpm')
expect(content).toContain('dlx')
expect(content).toContain('exec')
}
})
it('creates .cmd and .ps1 shims on windows', async () => {
await setupStandaloneFixture(binDir)
await ensureAliasLinks(binDir, true, 'win32')
// pn shims
const pnCmd = await readFile(path.join(binDir, 'pn.cmd'), 'utf8')
expect(pnCmd).toContain('pnpm')
expect(pnCmd).toContain('%*')
expect(pnCmd).not.toContain('dlx')
const pnPs1 = await readFile(path.join(binDir, 'pn.ps1'), 'utf8')
expect(pnPs1).toContain('pnpm')
expect(pnPs1).toContain('@args')
// pnpx/pnx shims call pnpm dlx
const pnpxCmd = await readFile(path.join(binDir, 'pnpx.cmd'), 'utf8')
expect(pnpxCmd).toContain('pnpm')
expect(pnpxCmd).toContain('dlx')
// Should not create extensionless files on windows
expect(existsSync(path.join(binDir, 'pn'))).toBe(false)
})
})
describe('non-standalone mode', () => {
it('creates pn as symlink to pnpm.cjs on unix', async () => {
await setupNonStandaloneFixture(binDir)
await ensureAliasLinks(binDir, false, 'linux')
const pnTarget = await readlink(path.join(binDir, 'pn'))
expect(pnTarget).toBe(path.join('..', 'pnpm', 'bin', 'pnpm.cjs'))
})
it('creates pnpx/pnx scripts on unix', async () => {
await setupNonStandaloneFixture(binDir)
await ensureAliasLinks(binDir, false, 'linux')
const content = await readFile(path.join(binDir, 'pnpx'), 'utf8')
expect(content).toContain('pnpm.cjs')
expect(content).toContain('dlx')
})
it('creates .cmd shims on windows', async () => {
await setupNonStandaloneFixture(binDir)
await ensureAliasLinks(binDir, false, 'win32')
const cmdContent = await readFile(path.join(binDir, 'pn.cmd'), 'utf8')
expect(cmdContent).toContain(path.join('pnpm', 'bin', 'pnpm.cjs'))
})
})
describe('skips when pnpm binary does not exist', () => {
it('creates no links on unix', async () => {
await ensureAliasLinks(binDir, true, 'linux')
expect(existsSync(path.join(binDir, 'pn'))).toBe(false)
expect(existsSync(path.join(binDir, 'pnpx'))).toBe(false)
expect(existsSync(path.join(binDir, 'pnx'))).toBe(false)
})
it('creates no shims on windows', async () => {
await ensureAliasLinks(binDir, true, 'win32')
expect(existsSync(path.join(binDir, 'pn.cmd'))).toBe(false)
})
})
describe('self-update bin directory (pnpm shim in same dir)', () => {
it('creates aliases using pnpm shim in the same directory on unix', async () => {
// self-update creates a pnpm shim in $PNPM_HOME/bin/ — no package dir
await writeFile(path.join(binDir, 'pnpm'), '#!/bin/sh\nexec /path/to/real/pnpm "$@"\n', { mode: 0o755 })
await ensureAliasLinks(binDir, true, 'linux')
const pnTarget = await readlink(path.join(binDir, 'pn'))
expect(pnTarget).toBe('pnpm')
const pnxContent = await readFile(path.join(binDir, 'pnx'), 'utf8')
expect(pnxContent).toContain('pnpm')
expect(pnxContent).toContain('dlx')
})
it('creates .cmd shims using pnpm in same dir on windows', async () => {
await writeFile(path.join(binDir, 'pnpm'), 'pnpm binary')
await ensureAliasLinks(binDir, true, 'win32')
const cmdContent = await readFile(path.join(binDir, 'pn.cmd'), 'utf8')
expect(cmdContent).toContain('pnpm')
})
})
describe('overwrites existing broken shims', () => {
it('replaces npm broken shim with symlink on unix', async () => {
await setupStandaloneFixture(binDir)
// Simulate npm's broken shim pointing to .tools/ placeholder
await writeFile(path.join(binDir, 'pn'), '#!/bin/sh\nexec .tools/broken "$@"\n')
await ensureAliasLinks(binDir, true, 'linux')
const target = await readlink(path.join(binDir, 'pn'))
expect(target).toBe(path.join('..', '@pnpm', 'exe', 'pnpm'))
})
it('replaces existing .cmd shims on windows', async () => {
await setupStandaloneFixture(binDir)
await writeFile(path.join(binDir, 'pn.cmd'), 'broken shim')
await ensureAliasLinks(binDir, true, 'win32')
const content = await readFile(path.join(binDir, 'pn.cmd'), 'utf8')
expect(content).toContain('pnpm')
})
})
})

View File

@@ -0,0 +1,84 @@
import { unlink, writeFile, symlink } from 'fs/promises'
import { existsSync } from 'fs'
import path from 'path'
function shScript (command: string): string {
return `#!/bin/sh\nexec ${command} "$@"\n`
}
function cmdShim (command: string): string {
return `@ECHO off\r\n${command} %*\r\n`
}
function pwshShim (command: string): string {
return `#!/usr/bin/env pwsh\n${command} @args\n`
}
async function forceSymlink (target: string, linkPath: string): Promise<void> {
try { await unlink(linkPath) } catch {}
await symlink(target, linkPath)
}
async function forceWriteFile (filePath: string, content: string, mode?: number): Promise<void> {
try { await unlink(filePath) } catch {}
await writeFile(filePath, content, { mode })
}
/**
* Find the pnpm binary/shim relative to binDir.
* Checks the package directory first (node_modules/.bin/../@pnpm/exe/pnpm),
* then falls back to a pnpm shim in binDir itself (e.g. self-update's bin/).
*/
function findPnpmTarget (binDir: string, standalone: boolean): string | undefined {
const packageTarget = standalone
? path.join('..', '@pnpm', 'exe', 'pnpm')
: path.join('..', 'pnpm', 'bin', 'pnpm.cjs')
if (existsSync(path.resolve(binDir, packageTarget))) {
return packageTarget
}
// self-update creates a pnpm shim in $PNPM_HOME/bin/ — use it directly
if (existsSync(path.join(binDir, 'pnpm'))) {
return 'pnpm'
}
return undefined
}
/**
* Create pn/pnpx/pnx alias links in the bin directory.
*
* pn is an alias for pnpm, so it symlinks (or shims) to the pnpm binary.
* pnpx/pnx are aliases for "pnpm dlx", created as shell scripts.
*
* This does NOT rely on the @pnpm/exe package having pn/pnx files, because
* pnpm self-update only replaces the pnpm binary — it doesn't update other
* files in the package. The aliases are created by pointing pn directly to
* the pnpm binary, and pnpx/pnx as scripts that exec "pnpm dlx".
*/
export async function ensureAliasLinks (binDir: string, standalone: boolean, platform: NodeJS.Platform = process.platform): Promise<void> {
const isWindows = platform === 'win32'
const pnpmTarget = findPnpmTarget(binDir, standalone)
if (!pnpmTarget) return
if (isWindows) {
// pn → calls pnpm directly
await writeFile(path.join(binDir, 'pn.cmd'), cmdShim(`"%~dp0\\${pnpmTarget}"`))
await writeFile(path.join(binDir, 'pn.ps1'), pwshShim(`& "$PSScriptRoot\\${pnpmTarget}"`))
// pnpx/pnx → calls pnpm dlx
for (const name of ['pnpx', 'pnx']) {
await writeFile(path.join(binDir, `${name}.cmd`), cmdShim(`"%~dp0\\${pnpmTarget}" dlx`))
await writeFile(path.join(binDir, `${name}.ps1`), pwshShim(`& "$PSScriptRoot\\${pnpmTarget}" dlx`))
}
} else {
// pn → symlink to pnpm binary
await forceSymlink(pnpmTarget, path.join(binDir, 'pn'))
// pnpx/pnx → shell scripts that exec pnpm dlx
for (const name of ['pnpx', 'pnx']) {
const pnpmPath = `"$(dirname "$0")/${pnpmTarget}"`
await forceWriteFile(path.join(binDir, name), shScript(`${pnpmPath} dlx`), 0o755)
}
}
}

View File

@@ -1,63 +1,87 @@
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, copyFile } from 'fs/promises' import { rm, writeFile, mkdir, symlink } from 'fs/promises'
import { readFileSync } from 'fs' import { readFileSync, existsSync } from 'fs'
import path from 'path' import path from 'path'
import { execPath } from 'process'
import util from 'util' import util from 'util'
import { Inputs } from '../inputs' import { Inputs } from '../inputs'
import { parse as parseYaml } from 'yaml' import { parse as parseYaml } from 'yaml'
import pnpmLock from './bootstrap/pnpm-lock.json'
import exeLock from './bootstrap/exe-lock.json'
import { ensureAliasLinks } from './ensureAliasLinks'
const BOOTSTRAP_PNPM_PACKAGE_JSON = JSON.stringify({ private: true, dependencies: { pnpm: pnpmLock.packages['node_modules/pnpm'].version } })
const BOOTSTRAP_EXE_PACKAGE_JSON = JSON.stringify({ private: true, dependencies: { '@pnpm/exe': exeLock.packages['node_modules/@pnpm/exe'].version } })
export async function runSelfInstaller(inputs: Inputs): Promise<number> { export async function runSelfInstaller(inputs: Inputs): Promise<number> {
const { version, dest, packageJsonFile, standalone } = inputs const { version, dest, packageJsonFile, standalone } = inputs
const { GITHUB_WORKSPACE } = process.env
// prepare self install // Install bootstrap pnpm via npm (integrity verified by committed lockfile)
await rm(dest, { recursive: true, force: true }) await rm(dest, { recursive: true, force: true })
// create dest directory after removal
await mkdir(dest, { recursive: true }) await mkdir(dest, { recursive: true })
const pkgJson = path.join(dest, 'package.json')
// we have ensured the dest directory exists, we can write the file directly
await writeFile(pkgJson, JSON.stringify({ private: true }))
// copy .npmrc if it exists to install from custom registry const lockfile = standalone ? exeLock : pnpmLock
if (GITHUB_WORKSPACE) { const packageJson = standalone ? BOOTSTRAP_EXE_PACKAGE_JSON : BOOTSTRAP_PNPM_PACKAGE_JSON
try { await writeFile(path.join(dest, 'package.json'), packageJson)
await copyFile(path.join(GITHUB_WORKSPACE, '.npmrc'), path.join(dest, '.npmrc')) await writeFile(path.join(dest, 'package-lock.json'), JSON.stringify(lockfile))
} catch (error) {
// Swallow error if .npmrc doesn't exist const npmExitCode = await runCommand('npm', ['ci'], { cwd: dest })
if (!util.types.isNativeError(error) || !('code' in error) || error.code !== 'ENOENT') throw error if (npmExitCode !== 0) {
} return npmExitCode
} }
// prepare target pnpm const pnpmHome = path.join(dest, 'node_modules', '.bin')
const target = await readTarget({ version, packageJsonFile, standalone })
const cp = spawn(execPath, [path.join(__dirname, 'pnpm.cjs'), 'install', target, '--no-lockfile'], {
cwd: dest,
stdio: ['pipe', 'inherit', 'inherit'],
})
const exitCode = await new Promise<number>((resolve, reject) => {
cp.on('error', reject)
cp.on('close', resolve)
})
if (exitCode === 0) {
const pnpmHome = path.join(dest, 'node_modules/.bin')
addPath(pnpmHome) addPath(pnpmHome)
addPath(path.join(pnpmHome, 'bin'))
exportVariable('PNPM_HOME', pnpmHome) exportVariable('PNPM_HOME', pnpmHome)
// Ensure pnpm bin link exists — npm ci sometimes doesn't create it
const pnpmBinLink = path.join(pnpmHome, 'pnpm')
if (!existsSync(pnpmBinLink)) {
await mkdir(pnpmHome, { recursive: true })
const target = standalone
? path.join('..', '@pnpm', 'exe', 'pnpm')
: path.join('..', 'pnpm', 'bin', 'pnpm.cjs')
await symlink(target, pnpmBinLink)
} }
const bootstrapPnpm = standalone
? path.join(dest, 'node_modules', '@pnpm', 'exe', 'pnpm')
: path.join(dest, 'node_modules', 'pnpm', 'bin', 'pnpm.cjs')
// Determine the target version
const targetVersion = readTargetVersion({ version, packageJsonFile })
if (targetVersion) {
const cmd = standalone ? bootstrapPnpm : process.execPath
const args = standalone ? ['self-update', targetVersion] : [bootstrapPnpm, 'self-update', targetVersion]
const exitCode = await runCommand(cmd, args, { cwd: dest })
if (exitCode !== 0) {
return exitCode return exitCode
} }
}
async function readTarget(opts: { // Create pn/pnx alias bin links if the installed version supports them
// (pnpm v11+ adds pn and pnx as short aliases).
// self-update links bins to $PNPM_HOME/bin/ which is also on PATH,
// so we must create aliases in both directories.
await ensureAliasLinks(pnpmHome, standalone)
const pnpmBinDir = path.join(pnpmHome, 'bin')
if (existsSync(pnpmBinDir)) {
await ensureAliasLinks(pnpmBinDir, standalone)
}
return 0
}
function readTargetVersion(opts: {
readonly version?: string | undefined readonly version?: string | undefined
readonly packageJsonFile: string readonly packageJsonFile: string
readonly standalone: boolean }): string | undefined {
}) { const { version, packageJsonFile } = opts
const { version, packageJsonFile, standalone } = opts
const { GITHUB_WORKSPACE } = process.env const { GITHUB_WORKSPACE } = process.env
let packageManager let packageManager: unknown
if (GITHUB_WORKSPACE) { if (GITHUB_WORKSPACE) {
try { try {
@@ -84,7 +108,12 @@ async function readTarget(opts: {
Remove one of these versions to avoid version mismatch errors like ERR_PNPM_BAD_PM_VERSION`) Remove one of these versions to avoid version mismatch errors like ERR_PNPM_BAD_PM_VERSION`)
} }
return `${ standalone ? '@pnpm/exe' : 'pnpm' }@${version}` return version
}
if (typeof packageManager === 'string' && packageManager.startsWith('pnpm@')) {
// Strip the "pnpm@" prefix and any "+sha..." hash suffix
return packageManager.replace('pnpm@', '').replace(/\+.*$/, '')
} }
if (!GITHUB_WORKSPACE) { if (!GITHUB_WORKSPACE) {
@@ -94,22 +123,22 @@ please run the actions/checkout before pnpm/action-setup.
Otherwise, please specify the pnpm version in the action configuration.`) Otherwise, please specify the pnpm version in the action configuration.`)
} }
if (typeof packageManager !== 'string') {
throw new Error(`No pnpm version is specified. throw new Error(`No pnpm version is specified.
Please specify it by one of the following ways: Please specify it by one of the following ways:
- in the GitHub Action config with the key "version" - in the GitHub Action config with the key "version"
- in the package.json with the key "packageManager"`) - in the package.json with the key "packageManager"`)
} }
if (!packageManager.startsWith('pnpm@')) { function runCommand(cmd: string, args: string[], opts: { cwd: string }): Promise<number> {
throw new Error('Invalid packageManager field in package.json') return new Promise<number>((resolve, reject) => {
} const cp = spawn(cmd, args, {
cwd: opts.cwd,
if (standalone) { stdio: ['pipe', 'inherit', 'inherit'],
return packageManager.replace('pnpm@', '@pnpm/exe@') shell: process.platform === 'win32',
} })
cp.on('error', reject)
return packageManager cp.on('close', resolve)
})
} }
export default runSelfInstaller export default runSelfInstaller

View File

@@ -6,5 +6,5 @@ export const getBinDest = (inputs: Inputs): string => path.join(inputs.dest, 'no
export const patchPnpmEnv = (inputs: Inputs): NodeJS.ProcessEnv => ({ export const patchPnpmEnv = (inputs: Inputs): NodeJS.ProcessEnv => ({
...process.env, ...process.env,
PATH: getBinDest(inputs) + path.delimiter + process.env.PATH, PATH: path.join(getBinDest(inputs), 'bin') + path.delimiter + getBinDest(inputs) + path.delimiter + process.env.PATH,
}) })

View File

@@ -1,26 +1,19 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "ES2022", "target": "ES2022",
"module": "Node16", "module": "ESNext",
"moduleResolution": "bundler",
"resolveJsonModule": true, "resolveJsonModule": true,
"lib": [ "lib": [
"ES2023" "ES2023"
], ],
"outDir": "./dist/tsc", "noEmit": true,
"preserveConstEnums": true,
"incremental": false,
"declaration": true,
"sourceMap": true,
"importHelpers": false,
"strict": true, "strict": true,
"pretty": true, "pretty": true,
"noUnusedLocals": true, "noUnusedLocals": true,
"noUnusedParameters": true, "noUnusedParameters": true,
"noImplicitReturns": true, "noImplicitReturns": true,
"noFallthroughCasesInSwitch": true, "noFallthroughCasesInSwitch": true,
"esModuleInterop": true, "esModuleInterop": true
"experimentalDecorators": true,
"emitDecoratorMetadata": true
} }
} }