fix: throw an error when a patch cannot be applied

This commit is contained in:
Zoltan Kochan
2022-06-28 02:07:33 +03:00
parent fc581d371d
commit 00c12fa53d
7 changed files with 39 additions and 2 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/build-modules": patch
---
Throw an error if a patch couldn't be applied.

View File

@@ -36,6 +36,7 @@
"dependencies": {
"@pnpm/calc-dep-state": "workspace:3.0.1",
"@pnpm/core-loggers": "workspace:7.0.5",
"@pnpm/error": "workspace:3.0.1",
"@pnpm/graph-sequencer": "1.0.0",
"@pnpm/lifecycle": "workspace:13.1.1",
"@pnpm/link-bins": "workspace:7.1.5",

View File

@@ -1,6 +1,7 @@
import path from 'path'
import { calcDepState, DepsStateCache } from '@pnpm/calc-dep-state'
import { skippedOptionalDependencyLogger } from '@pnpm/core-loggers'
import PnpmError from '@pnpm/error'
import { runPostinstallHooks } from '@pnpm/lifecycle'
import linkBins, { linkBinsOfPackages } from '@pnpm/link-bins'
import logger from '@pnpm/logger'
@@ -151,11 +152,14 @@ function applyPatchToDep (patchDir: string, patchFilePath: string) {
// 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(patchDir)
applyPatch({
const success = applyPatch({
patchFilePath,
patchDir: patchDir,
})
process.chdir(cwd)
if (!success) {
throw new PnpmError('PATCH_FAILED', `Could not apply patch ${patchFilePath} to ${patchDir}`)
}
}
export async function linkBinsOfDependencies (

View File

@@ -15,6 +15,9 @@
{
"path": "../core-loggers"
},
{
"path": "../error"
},
{
"path": "../lifecycle"
},

View File

@@ -352,3 +352,25 @@ test('patch package when the patched package has no dependencies and appears mul
'/is-positive/1.0.0_jnbpamcxayl5i4ehrkoext3any',
])
})
test('patch package should fail when the patch could not be applied', async () => {
prepareEmpty()
const patchPath = path.join(f.find('patch-pkg'), 'is-positive@1.0.0.patch')
const patchedDependencies = {
'is-positive@3.1.0': path.relative(process.cwd(), patchPath),
}
const opts = await testDefaults({
fastUnpack: false,
sideEffectsCacheRead: true,
sideEffectsCacheWrite: true,
patchedDependencies,
}, {}, {}, { packageImportMethod: 'hardlink' })
await expect(install({
dependencies: {
'is-positive': '3.1.0',
},
}, opts)).rejects.toThrow(/Could not apply patch/)
expect(fs.readFileSync('node_modules/is-positive/index.js', 'utf8')).not.toContain('// patched')
})

2
pnpm-lock.yaml generated
View File

@@ -178,6 +178,7 @@ importers:
'@pnpm/build-modules': workspace:9.2.1
'@pnpm/calc-dep-state': workspace:3.0.1
'@pnpm/core-loggers': workspace:7.0.5
'@pnpm/error': workspace:3.0.1
'@pnpm/graph-sequencer': 1.0.0
'@pnpm/lifecycle': workspace:13.1.1
'@pnpm/link-bins': workspace:7.1.5
@@ -192,6 +193,7 @@ importers:
dependencies:
'@pnpm/calc-dep-state': link:../calc-dep-state
'@pnpm/core-loggers': link:../core-loggers
'@pnpm/error': link:../error
'@pnpm/graph-sequencer': 1.0.0
'@pnpm/lifecycle': link:../lifecycle
'@pnpm/link-bins': link:../link-bins

2
typings/local.d.ts vendored
View File

@@ -172,5 +172,5 @@ declare module 'mdast-util-to-string' {
}
declare module 'patch-package/dist/applyPatches' {
export function applyPatch(opts: any): void;
export function applyPatch(opts: any): boolean;
}