fix: throw a meaningful error message when applying patch fails (#6687)

close #6683
ref #5268
ref #5278
This commit is contained in:
Zoltan Kochan
2023-06-19 03:51:19 +03:00
committed by GitHub
parent c686768f06
commit 3b6930263e
9 changed files with 104 additions and 37 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/patching.apply-patch": patch
"pnpm": patch
---
Throw a meaningful error when applying a patch to a dependency fails.

View File

@@ -137,7 +137,7 @@ declare module 'nerf-dart' {
export = anything
}
declare module 'patch-package/dist/applyPatches' {
declare module '@pnpm/patch-package/dist/applyPatches' {
export function applyPatch (opts: any): boolean
}

View File

@@ -0,0 +1,12 @@
diff --git a/foo.txt b/foo.txt
index 27a8dc9..31cbe49 100644
--- a/foo.txt
+++ b/foo.txt
@@ -1,3 +1,9 @@
This is a sample file
-and this is a broken patch
+and this is a supposed modification
+
+here is another addition
++and here is a broken line that will cause a parse error

View File

@@ -0,0 +1,2 @@
module.exports = require('../../jest.config.js')

View File

@@ -12,10 +12,11 @@
"node": ">=16.14"
},
"scripts": {
"test": "pnpm run compile",
"_test": "jest",
"test": "pnpm run compile && pnpm run _test",
"prepublishOnly": "pnpm run compile",
"compile": "tsc --build && pnpm run lint --fix",
"lint": "eslint \"src/**/*.ts\""
"lint": "eslint \"src/**/*.ts\" \"test/**/*.ts\""
},
"repository": "https://github.com/pnpm/pnpm/blob/main/patching/apply-patch",
"keywords": [
@@ -30,10 +31,12 @@
"homepage": "https://github.com/pnpm/pnpm/blob/main/patching/apply-patch#readme",
"dependencies": {
"@pnpm/error": "workspace:*",
"patch-package": "^7.0.0"
"@pnpm/patch-package": "0.0.0"
},
"devDependencies": {
"@pnpm/patching.apply-patch": "workspace:*"
"@pnpm/patching.apply-patch": "workspace:*",
"@pnpm/prepare": "workspace:*",
"@pnpm/test-fixtures": "workspace:*"
},
"funding": "https://opencollective.com/pnpm",
"exports": {

View File

@@ -1,5 +1,5 @@
import { PnpmError } from '@pnpm/error'
import { applyPatch } from 'patch-package/dist/applyPatches'
import { applyPatch } from '@pnpm/patch-package/dist/applyPatches'
export interface ApplyPatchToDirOpts {
patchedDir: string
@@ -11,11 +11,19 @@ export function applyPatchToDir (opts: ApplyPatchToDirOpts) {
// However, "patch" is not available on Windows and "git apply" is hard to execute on a subdirectory of an existing repository
const cwd = process.cwd()
process.chdir(opts.patchedDir)
const success = applyPatch({
patchDir: opts.patchedDir,
patchFilePath: opts.patchFilePath,
})
process.chdir(cwd)
let success = false
try {
success = applyPatch({
patchFilePath: opts.patchFilePath,
})
} catch (err: any) { // eslint-disable-line
if (err.code === 'ENOENT') {
throw new PnpmError('PATCH_NOT_FOUND', `Patch file not found: ${opts.patchFilePath}`)
}
throw new PnpmError('INVALID_PATCH', `Applying patch "${opts.patchFilePath}" failed: ${err.message as string}`)
} finally {
process.chdir(cwd)
}
if (!success) {
throw new PnpmError('PATCH_FAILED', `Could not apply patch ${opts.patchFilePath} to ${opts.patchedDir}`)
}

View File

@@ -0,0 +1,25 @@
import { applyPatchToDir } from '@pnpm/patching.apply-patch'
import { fixtures } from '@pnpm/test-fixtures'
import { tempDir } from '@pnpm/prepare'
const f = fixtures(__dirname)
describe('applyPatchToDir()', () => {
it('should fail on invalid patch', () => {
const patchFilePath = f.find('invalid.patch')
expect(() => {
applyPatchToDir({
patchFilePath,
patchedDir: tempDir(),
})
}).toThrowError(`Applying patch "${patchFilePath}" failed: hunk header integrity check failed`)
})
it('should fail if the patch file is not found', () => {
expect(() => {
applyPatchToDir({
patchFilePath: 'does-not-exist.patch',
patchedDir: tempDir(),
})
}).toThrowError('Patch file not found')
})
})

View File

@@ -9,6 +9,12 @@
"../../__typings__/**/*.d.ts"
],
"references": [
{
"path": "../../__utils__/prepare"
},
{
"path": "../../__utils__/test-fixtures"
},
{
"path": "../../packages/error"
}

57
pnpm-lock.yaml generated
View File

@@ -1,4 +1,4 @@
lockfileVersion: '6.1'
lockfileVersion: '6.0'
settings:
autoInstallPeers: true
@@ -2575,13 +2575,19 @@ importers:
'@pnpm/error':
specifier: workspace:*
version: link:../../packages/error
patch-package:
specifier: ^7.0.0
version: 7.0.0
'@pnpm/patch-package':
specifier: 0.0.0
version: 0.0.0
devDependencies:
'@pnpm/patching.apply-patch':
specifier: workspace:*
version: 'link:'
'@pnpm/prepare':
specifier: workspace:*
version: link:../../__utils__/prepare
'@pnpm/test-fixtures':
specifier: workspace:*
version: link:../../__utils__/test-fixtures
patching/plugin-commands-patching:
dependencies:
@@ -8501,6 +8507,27 @@ packages:
validate-npm-package-name: 5.0.0
dev: true
/@pnpm/patch-package@0.0.0:
resolution: {integrity: sha512-B17ZK4hUAKHDSeSlOg0N+jd+5TuxiSB/2jJcHM0oncf+W5FXslILITpwyIWut4p2P2RCgtSgTF9cSXEX/dGYQA==}
engines: {node: '>=14', npm: '>5'}
hasBin: true
dependencies:
'@yarnpkg/lockfile': 1.1.0
chalk: 4.1.2
ci-info: 3.8.0
cross-spawn: 7.0.3
find-yarn-workspace-root: 2.0.0
fs-extra: 9.1.0
klaw-sync: 6.0.0
minimist: 1.2.8
open: 7.4.2
rimraf: 2.7.1
semver: 5.7.1
slash: 2.0.0
tmp: 0.0.33
yaml: 2.3.1
dev: false
/@pnpm/patching.apply-patch@1.0.0:
resolution: {integrity: sha512-wu44YGD7K+AnV9qR3b6nsc4OCzlSr80jn1+C6OtFC4ZYZASo5kZPUHoKW265V+jYgw0KttsUCr9V6+QUZjc0QA==}
engines: {node: '>=14.6'}
@@ -15179,27 +15206,6 @@ packages:
yaml: 2.3.1
dev: true
/patch-package@7.0.0:
resolution: {integrity: sha512-eYunHbnnB2ghjTNc5iL1Uo7TsGMuXk0vibX3RFcE/CdVdXzmdbMsG/4K4IgoSuIkLTI5oHrMQk4+NkFqSed0BQ==}
engines: {node: '>=14', npm: '>5'}
hasBin: true
dependencies:
'@yarnpkg/lockfile': 1.1.0
chalk: 4.1.2
ci-info: 3.8.0
cross-spawn: 7.0.3
find-yarn-workspace-root: 2.0.0
fs-extra: 9.1.0
klaw-sync: 6.0.0
minimist: 1.2.8
open: 7.4.2
rimraf: 2.7.1
semver: 5.7.1
slash: 2.0.0
tmp: 0.0.33
yaml: 2.3.1
dev: false
/path-absolute@1.0.1:
resolution: {integrity: sha512-gds5iRhSeOcDtj8gfWkRHLtZKTPsFVuh7utbjYtvnclw4XM+ffRzJrwqMhOD1PVqef7nBLmgsu1vIujjvAJrAw==}
engines: {node: '>=4'}
@@ -18174,7 +18180,6 @@ time:
/p-settle@4.1.1: '2020-05-29T08:07:35.109Z'
/parse-json@5.2.0: '2021-01-18T11:15:13.459Z'
/parse-npm-tarball-url@3.0.0: '2019-05-27T23:50:09.183Z'
/patch-package@7.0.0: '2023-04-25T18:02:53.677Z'
/path-absolute@1.0.1: '2018-11-28T20:25:53.253Z'
/path-exists@4.0.0: '2019-04-04T03:29:16.887Z'
/path-name@1.0.0: '2016-10-20T18:43:50.780Z'