Repository navigation
fix: prefer locked pnpm #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MindTooth
wants to merge
2
commits into
pnpm:main
Choose a base branch
from
MindTooth:fix/prefer-locked-pnpm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import assert from 'node:assert/strict' | ||
| import fs from 'node:fs' | ||
| import os from 'node:os' | ||
| import path from 'node:path' | ||
| import { afterEach, test } from 'node:test' | ||
| import { readLockedPnpmVersion } from './locked-version.ts' | ||
|
|
||
| const directories = [] | ||
| afterEach(() => { | ||
| for (const directory of directories.splice(0)) fs.rmSync(directory, { recursive: true, force: true }) | ||
| }) | ||
|
|
||
| function fixture(specifier = '>=12.0.0 <13.0.0', version = '12.8.1') { | ||
| const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'setup-locked-pnpm-')) | ||
| directories.push(directory) | ||
| fs.writeFileSync(path.join(directory, 'pnpm-lock.yaml'), `--- | ||
| lockfileVersion: '9.0' | ||
| importers: | ||
| .: | ||
| packageManagerDependencies: | ||
| pnpm: | ||
| specifier: '${specifier}' | ||
| version: ${version} | ||
| --- | ||
| lockfileVersion: '9.0' | ||
| importers: | ||
| .: {} | ||
| `) | ||
| return directory | ||
| } | ||
|
|
||
| test('uses the locked version for a matching range, without resolving latest', () => { | ||
| assert.equal(readLockedPnpmVersion(fixture(), '>=12.0.0 <13.0.0'), '12.8.1') | ||
| }) | ||
|
|
||
| test('reuses a satisfying locked version after the manifest range changes', () => { | ||
| assert.equal(readLockedPnpmVersion(fixture('12'), '>=12.0.0 <13.0.0'), '12.8.1') | ||
| assert.equal(readLockedPnpmVersion(fixture('12.8.1'), '^12.0.0'), '12.8.1') | ||
| }) | ||
|
|
||
| test('reuses a locked prerelease with pnpm package-manager range semantics', () => { | ||
| assert.equal(readLockedPnpmVersion(fixture('12', '12.0.0-alpha.18'), '12'), '12.0.0-alpha.18') | ||
| assert.equal(readLockedPnpmVersion(fixture('12', '12.0.0-alpha.18'), '>=12.0.0'), undefined) | ||
| assert.equal(readLockedPnpmVersion(fixture('13', '13.0.0-alpha.1'), '12'), undefined) | ||
| }) | ||
|
|
||
| test('does not reuse a version outside the requested range', () => { | ||
| assert.equal(readLockedPnpmVersion(fixture('>=12.0.0 <13.0.0', '11.28.1'), '>=12.0.0 <13.0.0'), undefined) | ||
| }) | ||
|
|
||
| test('does not interpret a dist-tag as a semver range', () => { | ||
| assert.equal(readLockedPnpmVersion(fixture('latest'), 'latest'), undefined) | ||
| }) | ||
|
|
||
| test('falls back when no lockfile or package-manager document exists', () => { | ||
| const directory = fixture() | ||
| fs.unlinkSync(path.join(directory, 'pnpm-lock.yaml')) | ||
| assert.equal(readLockedPnpmVersion(directory, '12'), undefined) | ||
| fs.writeFileSync(path.join(directory, 'pnpm-lock.yaml'), "lockfileVersion: '9.0'\nimporters:\n .: {}\n") | ||
| assert.equal(readLockedPnpmVersion(directory, '12'), undefined) | ||
| }) | ||
|
|
||
| test('rejects a malformed lockfile rather than silently selecting latest', () => { | ||
| const directory = fixture() | ||
| fs.writeFileSync(path.join(directory, 'pnpm-lock.yaml'), 'importers: [') | ||
| assert.throws(() => readLockedPnpmVersion(directory, '12')) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { readFileSync } from 'fs' | ||
| import path from 'path' | ||
| import semver from 'semver' | ||
| import { parseAllDocuments } from 'yaml' | ||
|
|
||
| /** | ||
| * Reuse a compatible package-manager pin from the lockfile's environment | ||
| * document. Exact versions and registry tags bypass lockfile selection. | ||
| */ | ||
| export function readLockedPnpmVersion(directory: string, spec: string): string | undefined { | ||
| // Exact pins already identify the binary; tags need registry resolution. | ||
| if (semver.valid(spec) || !semver.validRange(spec)) return undefined | ||
|
|
||
| let content: string | ||
| try { | ||
| content = readFileSync(path.join(directory, 'pnpm-lock.yaml'), 'utf8') | ||
| } catch (error) { | ||
| if ((error as NodeJS.ErrnoException).code === 'ENOENT') return undefined | ||
| throw error | ||
| } | ||
|
|
||
| // pnpm 11+ stores package-manager dependencies in the first YAML document. | ||
| const document = parseAllDocuments(content)[0] | ||
| if (!document) return undefined | ||
| if (document.errors.length) throw document.errors[0] | ||
| const locked = document.toJSON()?.importers?.['.']?.packageManagerDependencies?.pnpm | ||
| // Match pnpm's version switching: keep a satisfying lock even after the | ||
| // specifier changes, and include prereleases when testing the range. | ||
| if (typeof locked?.version === 'string' && semver.valid(locked.version) && | ||
| semver.satisfies(locked.version, spec, { includePrerelease: true })) { | ||
| return locked.version | ||
| } | ||
| return undefined | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.