-
-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathapplyPatches.ts
591 lines (530 loc) Β· 16 KB
/
applyPatches.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
import chalk from "chalk"
import { writeFileSync } from "fs"
import { existsSync } from "fs-extra"
import { posix } from "path"
import semver from "semver"
import { hashFile } from "./hash"
import { logPatchSequenceError } from "./makePatch"
import { PackageDetails, PatchedPackageDetails } from "./PackageDetails"
import { packageIsDevDependency } from "./packageIsDevDependency"
import { executeEffects } from "./patch/apply"
import { readPatch } from "./patch/read"
import { reversePatch } from "./patch/reverse"
import { getGroupedPatches } from "./patchFs"
import { join, relative } from "./path"
import {
clearPatchApplicationState,
getPatchApplicationState,
PatchState,
savePatchApplicationState,
} from "./stateFile"
class PatchApplicationError extends Error {
constructor(msg: string) {
super(msg)
}
}
function getInstalledPackageVersion({
appPath,
path,
pathSpecifier,
isDevOnly,
patchFilename,
}: {
appPath: string
path: string
pathSpecifier: string
isDevOnly: boolean
patchFilename: string
}): null | string {
const packageDir = join(appPath, path)
if (!existsSync(packageDir)) {
if (process.env.NODE_ENV === "production" && isDevOnly) {
return null
}
let err =
`${chalk.red("Error:")} Patch file found for package ${posix.basename(
pathSpecifier,
)}` + ` which is not present at ${relative(".", packageDir)}`
if (!isDevOnly && process.env.NODE_ENV === "production") {
err += `
If this package is a dev dependency, rename the patch file to
${chalk.bold(patchFilename.replace(".patch", ".dev.patch"))}
`
}
throw new PatchApplicationError(err)
}
const { version } = require(join(packageDir, "package.json"))
// normalize version for `npm ci`
const result = semver.valid(version, { loose: true })
if (result === null) {
throw new PatchApplicationError(
`${chalk.red(
"Error:",
)} Version string '${version}' cannot be parsed from ${join(
packageDir,
"package.json",
)}`,
)
}
return result as string
}
function logPatchApplication(patchDetails: PatchedPackageDetails) {
const sequenceString =
patchDetails.sequenceNumber != null
? ` (${patchDetails.sequenceNumber}${
patchDetails.sequenceName ? " " + patchDetails.sequenceName : ""
})`
: ""
console.log(
`${chalk.bold(patchDetails.pathSpecifier)}@${
patchDetails.version
}${sequenceString} ${chalk.green("β")}`,
)
}
export function applyPatchesForApp({
appPath,
reverse,
patchDir,
shouldExitWithError,
shouldExitWithWarning,
bestEffort,
}: {
appPath: string
reverse: boolean
patchDir: string
shouldExitWithError: boolean
shouldExitWithWarning: boolean
bestEffort: boolean
}): void {
const patchesDirectory = join(appPath, patchDir)
const groupedPatches = getGroupedPatches(patchesDirectory)
if (groupedPatches.numPatchFiles === 0) {
console.log(chalk.blueBright("No patch files found"))
return
}
const errors: string[] = []
const warnings: string[] = [...groupedPatches.warnings]
for (const patches of Object.values(
groupedPatches.pathSpecifierToPatchFiles,
)) {
applyPatchesForPackage({
patches,
appPath,
patchDir,
reverse,
warnings,
errors,
bestEffort,
})
}
for (const warning of warnings) {
console.log(warning)
}
for (const error of errors) {
console.log(error)
}
const problemsSummary = []
if (warnings.length) {
problemsSummary.push(chalk.yellow(`${warnings.length} warning(s)`))
}
if (errors.length) {
problemsSummary.push(chalk.red(`${errors.length} error(s)`))
}
if (problemsSummary.length) {
console.log("---")
console.log("patch-package finished with", problemsSummary.join(", ") + ".")
}
if (errors.length && shouldExitWithError) {
process.exit(1)
}
if (warnings.length && shouldExitWithWarning) {
process.exit(1)
}
process.exit(0)
}
export function applyPatchesForPackage({
patches,
appPath,
patchDir,
reverse,
warnings,
errors,
bestEffort,
}: {
patches: PatchedPackageDetails[]
appPath: string
patchDir: string
reverse: boolean
warnings: string[]
errors: string[]
bestEffort: boolean
}) {
const pathSpecifier = patches[0].pathSpecifier
const state = patches.length > 1 ? getPatchApplicationState(patches[0]) : null
const unappliedPatches = patches.slice(0)
const appliedPatches: PatchedPackageDetails[] = []
// if there are multiple patches to apply, we can't rely on the reverse-patch-dry-run behavior to make this operation
// idempotent, so instead we need to check the state file to see whether we have already applied any of the patches
// todo: once this is battle tested we might want to use the same approach for single patches as well, but it's not biggie since the dry run thing is fast
if (unappliedPatches && state) {
for (let i = 0; i < state.patches.length; i++) {
const patchThatWasApplied = state.patches[i]
if (!patchThatWasApplied.didApply) {
break
}
const patchToApply = unappliedPatches[0]
const currentPatchHash = hashFile(
join(appPath, patchDir, patchToApply.patchFilename),
)
if (patchThatWasApplied.patchContentHash === currentPatchHash) {
// this patch was applied we can skip it
appliedPatches.push(unappliedPatches.shift()!)
} else {
console.log(
chalk.red("Error:"),
`The patches for ${chalk.bold(pathSpecifier)} have changed.`,
`You should reinstall your node_modules folder to make sure the package is up to date`,
)
process.exit(1)
}
}
}
if (reverse && state) {
// if we are reversing the patches we need to make the unappliedPatches array
// be the reversed version of the appliedPatches array.
// The applied patches array should then be empty because it is used differently
// when outputting the state file.
unappliedPatches.length = 0
unappliedPatches.push(...appliedPatches)
unappliedPatches.reverse()
appliedPatches.length = 0
}
if (appliedPatches.length) {
// some patches have already been applied
appliedPatches.forEach(logPatchApplication)
}
if (!unappliedPatches.length) {
return
}
let failedPatch: PatchedPackageDetails | null = null
packageLoop: for (const patchDetails of unappliedPatches) {
try {
const { name, version, path, isDevOnly, patchFilename } = patchDetails
const installedPackageVersion = getInstalledPackageVersion({
appPath,
path,
pathSpecifier,
isDevOnly:
isDevOnly ||
// check for direct-dependents in prod
(process.env.NODE_ENV === "production" &&
packageIsDevDependency({
appPath,
patchDetails,
})),
patchFilename,
})
if (!installedPackageVersion) {
// it's ok we're in production mode and this is a dev only package
console.log(
`Skipping dev-only ${chalk.bold(
pathSpecifier,
)}@${version} ${chalk.blue("β")}`,
)
continue
}
if (
applyPatch({
patchFilePath: join(appPath, patchDir, patchFilename) as string,
reverse,
patchDetails,
patchDir,
cwd: process.cwd(),
bestEffort,
})
) {
appliedPatches.push(patchDetails)
// yay patch was applied successfully
// print warning if version mismatch
if (installedPackageVersion !== version) {
warnings.push(
createVersionMismatchWarning({
packageName: name,
actualVersion: installedPackageVersion,
originalVersion: version,
pathSpecifier,
path,
}),
)
}
logPatchApplication(patchDetails)
} else if (patches.length > 1) {
logPatchSequenceError({ patchDetails })
// in case the package has multiple patches, we need to break out of this inner loop
// because we don't want to apply more patches on top of the broken state
failedPatch = patchDetails
break packageLoop
} else if (installedPackageVersion === version) {
// completely failed to apply patch
// TODO: propagate useful error messages from patch application
errors.push(
createBrokenPatchFileError({
packageName: name,
patchFilename,
pathSpecifier,
path,
}),
)
break packageLoop
} else {
errors.push(
createPatchApplicationFailureError({
packageName: name,
actualVersion: installedPackageVersion,
originalVersion: version,
patchFilename,
path,
pathSpecifier,
}),
)
// in case the package has multiple patches, we need to break out of this inner loop
// because we don't want to apply more patches on top of the broken state
break packageLoop
}
} catch (error) {
if (error instanceof PatchApplicationError) {
errors.push(error.message)
} else {
errors.push(
createUnexpectedError({
filename: patchDetails.patchFilename,
error: error as Error,
}),
)
}
// in case the package has multiple patches, we need to break out of this inner loop
// because we don't want to apply more patches on top of the broken state
break packageLoop
}
}
if (patches.length > 1) {
if (reverse) {
if (!state) {
throw new Error("unexpected state: no state file found while reversing")
}
// if we removed all the patches that were previously applied we can delete the state file
if (appliedPatches.length === patches.length) {
clearPatchApplicationState(patches[0])
} else {
// We failed while reversing patches and some are still in the applied state.
// We need to update the state file to reflect that.
// appliedPatches is currently the patches that were successfully reversed, in the order they were reversed
// So we need to find the index of the last reversed patch in the original patches array
// and then remove all the patches after that. Sorry for the confusing code.
const lastReversedPatchIndex = patches.indexOf(
appliedPatches[appliedPatches.length - 1],
)
if (lastReversedPatchIndex === -1) {
throw new Error(
"unexpected state: failed to find last reversed patch in original patches array",
)
}
savePatchApplicationState({
packageDetails: patches[0],
patches: patches.slice(0, lastReversedPatchIndex).map((patch) => ({
didApply: true,
patchContentHash: hashFile(
join(appPath, patchDir, patch.patchFilename),
),
patchFilename: patch.patchFilename,
})),
isRebasing: false,
})
}
} else {
const nextState = appliedPatches.map(
(patch): PatchState => ({
didApply: true,
patchContentHash: hashFile(
join(appPath, patchDir, patch.patchFilename),
),
patchFilename: patch.patchFilename,
}),
)
if (failedPatch) {
nextState.push({
didApply: false,
patchContentHash: hashFile(
join(appPath, patchDir, failedPatch.patchFilename),
),
patchFilename: failedPatch.patchFilename,
})
}
savePatchApplicationState({
packageDetails: patches[0],
patches: nextState,
isRebasing: !!failedPatch,
})
}
if (failedPatch) {
process.exit(1)
}
}
}
export function applyPatch({
patchFilePath,
reverse,
patchDetails,
patchDir,
cwd,
bestEffort,
}: {
patchFilePath: string
reverse: boolean
patchDetails: PackageDetails
patchDir: string
cwd: string
bestEffort: boolean
}): boolean {
const patch = readPatch({
patchFilePath,
patchDetails,
patchDir,
})
const forward = reverse ? reversePatch(patch) : patch
try {
if (!bestEffort) {
executeEffects(forward, { dryRun: true, cwd, bestEffort: false })
}
const errors: string[] | undefined = bestEffort ? [] : undefined
executeEffects(forward, { dryRun: false, cwd, bestEffort, errors })
if (errors?.length) {
console.log(
"Saving errors to",
chalk.cyan.bold("./patch-package-errors.log"),
)
writeFileSync("patch-package-errors.log", errors.join("\n\n"))
process.exit(0)
}
} catch (e) {
try {
const backward = reverse ? patch : reversePatch(patch)
executeEffects(backward, {
dryRun: true,
cwd,
bestEffort: false,
})
} catch (e) {
return false
}
}
return true
}
function createVersionMismatchWarning({
packageName,
actualVersion,
originalVersion,
pathSpecifier,
path,
}: {
packageName: string
actualVersion: string
originalVersion: string
pathSpecifier: string
path: string
}) {
return `
${chalk.yellow("Warning:")} patch-package detected a patch file version mismatch
Don't worry! This is probably fine. The patch was still applied
successfully. Here's the deets:
Patch file created for
${packageName}@${chalk.bold(originalVersion)}
applied to
${packageName}@${chalk.bold(actualVersion)}
At path
${path}
This warning is just to give you a heads-up. There is a small chance of
breakage even though the patch was applied successfully. Make sure the package
still behaves like you expect (you wrote tests, right?) and then run
${chalk.bold(`patch-package ${pathSpecifier}`)}
to update the version in the patch file name and make this warning go away.
`
}
function createBrokenPatchFileError({
packageName,
patchFilename,
path,
pathSpecifier,
}: {
packageName: string
patchFilename: string
path: string
pathSpecifier: string
}) {
return `
${chalk.red.bold("**ERROR**")} ${chalk.red(
`Failed to apply patch for package ${chalk.bold(packageName)} at path`,
)}
${path}
This error was caused because patch-package cannot apply the following patch file:
patches/${patchFilename}
Try removing node_modules and trying again. If that doesn't work, maybe there was
an accidental change made to the patch file? Try recreating it by manually
editing the appropriate files and running:
patch-package ${pathSpecifier}
If that doesn't work, then it's a bug in patch-package, so please submit a bug
report. Thanks!
https://github.com/ds300/patch-package/issues
`
}
function createPatchApplicationFailureError({
packageName,
actualVersion,
originalVersion,
patchFilename,
path,
pathSpecifier,
}: {
packageName: string
actualVersion: string
originalVersion: string
patchFilename: string
path: string
pathSpecifier: string
}) {
return `
${chalk.red.bold("**ERROR**")} ${chalk.red(
`Failed to apply patch for package ${chalk.bold(packageName)} at path`,
)}
${path}
This error was caused because ${chalk.bold(packageName)} has changed since you
made the patch file for it. This introduced conflicts with your patch,
just like a merge conflict in Git when separate incompatible changes are
made to the same piece of code.
Maybe this means your patch file is no longer necessary, in which case
hooray! Just delete it!
Otherwise, you need to generate a new patch file.
To generate a new one, just repeat the steps you made to generate the first
one.
i.e. manually make the appropriate file changes, then run
patch-package ${pathSpecifier}
Info:
Patch file: patches/${patchFilename}
Patch was made for version: ${chalk.green.bold(originalVersion)}
Installed version: ${chalk.red.bold(actualVersion)}
`
}
function createUnexpectedError({
filename,
error,
}: {
filename: string
error: Error
}) {
return `
${chalk.red.bold("**ERROR**")} ${chalk.red(
`Failed to apply patch file ${chalk.bold(filename)}`,
)}
${error.stack}
`
}