fix(plugin-commands-patching): fix reusing existing patch when shared-workspace-file=false (#7252)

This commit is contained in:
Hiroshi Ogawa
2023-11-07 08:13:44 +09:00
committed by GitHub
parent 0ec735a6ed
commit 2dfc8c80af
3 changed files with 88 additions and 7 deletions

View 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).

View File

@@ -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}

View File

@@ -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({