fix: patch support unpublished pkg (#9694)

This commit is contained in:
btea
2025-07-07 17:58:23 +08:00
committed by GitHub
parent 8e0db2e933
commit b656f8aea1
3 changed files with 25 additions and 3 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-patching": patch
pnpm: patch
---
When patching dependencies installed via `pkg.pr.new`, treat them as git tarball URLs [#9694](https://github.com/pnpm/pnpm/pull/9694).

View File

@@ -58,14 +58,30 @@ export async function getPatchedDependency (rawDependency: string, opts: GetPatc
}
} else {
const preferred = preferredVersions[0]
if (preferred.gitTarballUrl) {
return {
...opts,
applyToAll: false,
bareSpecifier: preferred.gitTarballUrl,
}
}
return {
...dep,
applyToAll: !dep.bareSpecifier,
bareSpecifier: preferred.gitTarballUrl ?? preferred.version,
bareSpecifier: preferred.version,
}
}
}
// https://github.com/stackblitz-labs/pkg.pr.new
// With pkg.pr.new, each of your commits and pull requests will trigger an instant preview release without publishing anything to NPM.
// This enables users to access features and bug-fixes without the need to wait for release cycles using npm or pull request merges.
// When a package is installed via pkg.pr.new and has never been published to npm,
// the version or name obtained is incorrect, and an error will occur when patching. We can treat it as a tarball url.
export function isPkgPrNewUrl (url: string): boolean {
return url.startsWith('https://pkg.pr.new/')
}
export interface LockfileVersion {
gitTarballUrl?: string
name: string
@@ -101,7 +117,7 @@ export async function getVersionsFromLockfile (dep: ParseWantedDependencyResult,
const tarball = (pkgSnapshot.resolution as TarballResolution)?.tarball ?? ''
return {
...nameVerFromPkgSnapshot(depPath, pkgSnapshot),
gitTarballUrl: isGitHostedPkgUrl(tarball) ? tarball : undefined,
gitTarballUrl: (isGitHostedPkgUrl(tarball) || isPkgPrNewUrl(tarball)) ? tarball : undefined,
}
})
.filter(({ name }) => name === pkgName)

View File

@@ -74,7 +74,7 @@ export async function handler (opts: PatchCommitCommandOptions, params: string[]
if (!applyToAll) {
gitTarballUrl = await getGitTarballUrlFromLockfile({
alias: patchedPkgManifest.name,
bareSpecifier: patchedPkgManifest.version,
bareSpecifier: patchedPkgManifest.version || undefined,
}, {
lockfileDir,
modulesDir: opts.modulesDir,