mirror of
https://github.com/pnpm/pnpm.git
synced 2025-12-25 08:08:14 -05:00
fix(plugin-commands-patching): fix reusing existing patch when shared-workspace-file=false (#7252)
This commit is contained in:
6
.changeset/big-seas-exist.md
Normal file
6
.changeset/big-seas-exist.md
Normal file
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"pnpm": patch
|
||||
"@pnpm/plugin-commands-patching": patch
|
||||
---
|
||||
|
||||
`pnpm patch` should reuse existing patch when `shared-workspace-file=false` [#7252](https://github.com/pnpm/pnpm/pull/7252).
|
||||
@@ -14,6 +14,7 @@ import { PnpmError } from '@pnpm/error'
|
||||
import { type ParseWantedDependencyResult } from '@pnpm/parse-wanted-dependency'
|
||||
import { writePackage } from './writePackage'
|
||||
import { getPatchedDependency } from './getPatchedDependency'
|
||||
import { tryReadProjectManifest } from '@pnpm/read-project-manifest'
|
||||
|
||||
export function rcOptionsTypes () {
|
||||
return pick([], allTypes)
|
||||
@@ -59,6 +60,7 @@ export type PatchCommandOptions = Pick<Config,
|
||||
| 'lockfileDir'
|
||||
| 'modulesDir'
|
||||
| 'virtualStoreDir'
|
||||
| 'sharedWorkspaceLockfile'
|
||||
> & CreateStoreControllerOptions & {
|
||||
editDir?: string
|
||||
reporter?: (logObj: LogBase) => void
|
||||
@@ -81,13 +83,23 @@ export async function handler (opts: PatchCommandOptions, params: string[]) {
|
||||
})
|
||||
|
||||
await writePackage(patchedDep, editDir, opts)
|
||||
if (!opts.ignoreExisting && opts.rootProjectManifest?.pnpm?.patchedDependencies) {
|
||||
tryPatchWithExistingPatchFile({
|
||||
patchedDep,
|
||||
patchedDir: editDir,
|
||||
patchedDependencies: opts.rootProjectManifest.pnpm.patchedDependencies,
|
||||
lockfileDir,
|
||||
})
|
||||
|
||||
if (!opts.ignoreExisting) {
|
||||
let rootProjectManifest = opts.rootProjectManifest
|
||||
if (!opts.sharedWorkspaceLockfile) {
|
||||
const { manifest } = await tryReadProjectManifest(lockfileDir)
|
||||
if (manifest) {
|
||||
rootProjectManifest = manifest
|
||||
}
|
||||
}
|
||||
if (rootProjectManifest?.pnpm?.patchedDependencies) {
|
||||
tryPatchWithExistingPatchFile({
|
||||
patchedDep,
|
||||
patchedDir: editDir,
|
||||
patchedDependencies: rootProjectManifest.pnpm.patchedDependencies,
|
||||
lockfileDir,
|
||||
})
|
||||
}
|
||||
}
|
||||
return `You can now edit the following folder: ${editDir}
|
||||
|
||||
|
||||
@@ -658,6 +658,69 @@ describe('patch and commit in workspaces', () => {
|
||||
expect(fs.existsSync('../project-2/node_modules/is-positive/license')).toBe(true)
|
||||
})
|
||||
|
||||
test('reusing existing patch file should work with shared-workspace-lockfile=false', async () => {
|
||||
const { allProjects, allProjectsGraph, selectedProjectsGraph } = await readProjects(process.cwd(), [])
|
||||
await install.handler({
|
||||
...DEFAULT_OPTS,
|
||||
cacheDir,
|
||||
storeDir,
|
||||
allProjects,
|
||||
allProjectsGraph,
|
||||
dir: process.cwd(),
|
||||
lockfileDir: undefined,
|
||||
selectedProjectsGraph,
|
||||
workspaceDir: process.cwd(),
|
||||
saveLockfile: true,
|
||||
sharedWorkspaceLockfile: false,
|
||||
})
|
||||
|
||||
// patch project-1
|
||||
process.chdir('./project-1')
|
||||
let output = await patch.handler({
|
||||
...defaultPatchOption,
|
||||
dir: process.cwd(),
|
||||
}, ['is-positive@1.0.0'])
|
||||
let patchDir = getPatchDirFromPatchOutput(output)
|
||||
|
||||
// modify index.js and remove license
|
||||
fs.appendFileSync(path.join(patchDir, 'index.js'), '// test patching', 'utf8')
|
||||
fs.unlinkSync(path.join(patchDir, 'license'))
|
||||
|
||||
// patch-commit
|
||||
await patchCommit.handler({
|
||||
...DEFAULT_OPTS,
|
||||
allProjects,
|
||||
allProjectsGraph,
|
||||
selectedProjectsGraph,
|
||||
dir: process.cwd(),
|
||||
rootProjectManifestDir: process.cwd(),
|
||||
cacheDir,
|
||||
storeDir,
|
||||
lockfileDir: process.cwd(),
|
||||
workspaceDir: process.cwd(),
|
||||
saveLockfile: true,
|
||||
frozenLockfile: false,
|
||||
fixLockfile: true,
|
||||
sharedWorkspaceLockfile: false,
|
||||
}, [patchDir])
|
||||
|
||||
// verify committed patch
|
||||
expect(fs.readFileSync('./node_modules/is-positive/index.js', 'utf8')).toContain('// test patching')
|
||||
expect(fs.existsSync('./node_modules/is-positive/license')).toBe(false)
|
||||
|
||||
// re-patch project-1
|
||||
output = await patch.handler({
|
||||
...defaultPatchOption,
|
||||
dir: process.cwd(),
|
||||
}, ['is-positive@1.0.0'])
|
||||
patchDir = getPatchDirFromPatchOutput(output)
|
||||
expect(fs.existsSync(patchDir)).toBe(true)
|
||||
|
||||
// verify temporary patch is reusing last committed patch
|
||||
expect(fs.readFileSync(path.join(patchDir, 'index.js'), 'utf8')).toContain('// test patching')
|
||||
expect(fs.existsSync(path.join(patchDir, 'license'))).toBe(false)
|
||||
})
|
||||
|
||||
test('patch and patch-commit for git hosted dependency', async () => {
|
||||
const { allProjects, allProjectsGraph, selectedProjectsGraph } = await readProjects(process.cwd(), [])
|
||||
await install.handler({
|
||||
|
||||
Reference in New Issue
Block a user