fix(patch): saved path file should be relative path (#9403)

* fix(patch): saved path file should be relative path

* fix: update relative dir

* fix: update

* fix: paths in patchedDependencies should be normalized

---------

Co-authored-by: Zoltan Kochan <z@kochan.io>
This commit is contained in:
btea
2025-04-14 17:42:14 +08:00
committed by GitHub
parent 9397a518a2
commit 17b7e9fbb3
5 changed files with 36 additions and 13 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-patching": patch
"@pnpm/config.config-writer": patch
---
The patch file path saved by the pnpm `patch-commit` and `patch-remove` commands should be a relative path [#9403](https://github.com/pnpm/pnpm/pull/9403).

View File

@@ -3,12 +3,14 @@ import { tryReadProjectManifest } from '@pnpm/read-project-manifest'
import { updateWorkspaceManifest } from '@pnpm/workspace.manifest-writer'
import equals from 'ramda/src/equals'
export async function writeSettings (opts: {
export interface WriteSettingsOptions {
updatedSettings: PnpmSettings
rootProjectManifest?: ProjectManifest
rootProjectManifestDir: string
workspaceDir: string
}): Promise<void> {
}
export async function writeSettings (opts: WriteSettingsOptions): Promise<void> {
if (opts.rootProjectManifest?.pnpm != null) {
const { manifest, writeProjectManifest } = await tryReadProjectManifest(opts.rootProjectManifestDir)
if (manifest) {

View File

@@ -2,7 +2,6 @@ import fs from 'fs'
import path from 'path'
import { docsUrl } from '@pnpm/cli-utils'
import { type Config, types as allTypes } from '@pnpm/config'
import { writeSettings } from '@pnpm/config.config-writer'
import { createShortHash } from '@pnpm/crypto.hash'
import { PnpmError } from '@pnpm/error'
import { packlist } from '@pnpm/fs.packlist'
@@ -23,6 +22,7 @@ import { type WritePackageOptions, writePackage } from './writePackage'
import { type ParseWantedDependencyResult, parseWantedDependency } from '@pnpm/parse-wanted-dependency'
import { type GetPatchedDependencyOptions, getVersionsFromLockfile } from './getPatchedDependency'
import { readEditDirState } from './stateFile'
import { updatePatchedDependencies } from './updatePatchedDependencies'
export const rcOptionsTypes = cliOptionsTypes
@@ -104,12 +104,9 @@ export async function handler (opts: PatchCommitCommandOptions, params: string[]
...opts.patchedDependencies,
[patchKey]: `${patchesDirName}/${patchFileName}.patch`,
}
await writeSettings({
await updatePatchedDependencies(patchedDependencies, {
...opts,
workspaceDir: opts.workspaceDir ?? opts.rootProjectManifestDir,
updatedSettings: {
patchedDependencies,
},
})
return install.handler({

View File

@@ -1,13 +1,13 @@
import path from 'path'
import fs from 'fs/promises'
import { docsUrl } from '@pnpm/cli-utils'
import { writeSettings } from '@pnpm/config.config-writer'
import { install } from '@pnpm/plugin-commands-installation'
import { type Config, types as allTypes } from '@pnpm/config'
import { PnpmError } from '@pnpm/error'
import renderHelp from 'render-help'
import { prompt } from 'enquirer'
import pick from 'ramda/src/pick'
import { updatePatchedDependencies } from './updatePatchedDependencies'
export function rcOptionsTypes (): Record<string, unknown> {
return pick([], allTypes)
@@ -72,13 +72,9 @@ export async function handler (opts: PatchRemoveCommandOptions, params: string[]
}
} catch {}
}))
await writeSettings({
await updatePatchedDependencies(patchedDependencies, {
...opts,
workspaceDir: opts.workspaceDir ?? opts.rootProjectManifestDir,
updatedSettings: {
patchedDependencies: Object.keys(patchedDependencies).length ? patchedDependencies : undefined,
},
})
return install.handler(opts)

View File

@@ -0,0 +1,22 @@
import path from 'path'
import normalizePath from 'normalize-path'
import { writeSettings, type WriteSettingsOptions } from '@pnpm/config.config-writer'
export async function updatePatchedDependencies (
patchedDependencies: Record<string, string>,
opts: Omit<WriteSettingsOptions, 'updatedSettings'>
): Promise<void> {
const workspaceDir = opts.workspaceDir ?? opts.rootProjectManifestDir
for (const [patchName, patchPath] of Object.entries(patchedDependencies)) {
if (path.isAbsolute(patchPath)) {
patchedDependencies[patchName] = normalizePath(path.relative(workspaceDir, patchPath))
}
}
await writeSettings({
...opts,
workspaceDir,
updatedSettings: {
patchedDependencies: Object.keys(patchedDependencies).length ? patchedDependencies : undefined,
},
})
}