Skip to content

Commit 2ac6424

Browse files
committed
fix package loop breaking, do some renaming
1 parent ef7efca commit 2ac6424

9 files changed

+63
-35
lines changed

integration-tests/apply-multiple-patches/__snapshots__/apply-multiple-patches.test.ts.snap

+10-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ exports[`Test apply-multiple-patches: patch-package fails when a patch in the se
99
1010
This error was caused because patch-package cannot apply the following patch file:
1111
12-
patches/left-pad+1.3.0+03+broken.patch
12+
patches/left-pad+1.3.0+02+broken.patch
1313
1414
Try removing node_modules and trying again. If that doesn't work, maybe there was
1515
an accidental change made to the patch file? Try recreating it by manually
@@ -33,6 +33,14 @@ exports[`Test apply-multiple-patches: patch-package happily applies both good pa
3333
patch-package 0.0.0
3434
Applying patches...
3535
[email protected] (1 hello) ✔
36-
[email protected] (2 world) ✔
36+
[email protected] (3 world) ✔
37+
END SNAPSHOT"
38+
`;
39+
40+
exports[`Test apply-multiple-patches: patch-package only applies the first patch if the second of three is invalid 1`] = `
41+
"SNAPSHOT: patch-package only applies the first patch if the second of three is invalid
42+
patch-package 0.0.0
43+
Applying patches...
44+
[email protected] (1 hello) ✔
3745
END SNAPSHOT"
3846
`;

integration-tests/apply-multiple-patches/apply-multiple-patches.sh

+7
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,10 @@ then
2020
exit 1
2121
fi
2222
(>&2 echo "END SNAPSHOT")
23+
24+
echo "SNAPSHOT: patch-package only applies the first patch if the second of three is invalid"
25+
if patch-package
26+
then
27+
exit 1
28+
fi
29+
echo "END SNAPSHOT"

src/applyPatches.ts

+28-15
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ export function applyPatchesForApp({
121121
details.sort((a, b) => {
122122
return (a.sequenceNumber ?? 0) - (b.sequenceNumber ?? 0)
123123
})
124-
packageLoop: for (const packageDetails of details) {
124+
packageLoop: for (const patchDetails of details) {
125125
try {
126126
const {
127127
name,
@@ -130,7 +130,7 @@ export function applyPatchesForApp({
130130
pathSpecifier,
131131
isDevOnly,
132132
patchFilename,
133-
} = packageDetails
133+
} = patchDetails
134134

135135
const installedPackageVersion = getInstalledPackageVersion({
136136
appPath,
@@ -140,7 +140,10 @@ export function applyPatchesForApp({
140140
isDevOnly ||
141141
// check for direct-dependents in prod
142142
(process.env.NODE_ENV === "production" &&
143-
packageIsDevDependency({ appPath, packageDetails })),
143+
packageIsDevDependency({
144+
appPath,
145+
patchDetails,
146+
})),
144147
patchFilename,
145148
})
146149
if (!installedPackageVersion) {
@@ -157,7 +160,7 @@ export function applyPatchesForApp({
157160
applyPatch({
158161
patchFilePath: resolve(patchesDirectory, patchFilename) as string,
159162
reverse,
160-
packageDetails,
163+
patchDetails,
161164
patchDir,
162165
})
163166
) {
@@ -175,10 +178,10 @@ export function applyPatchesForApp({
175178
)
176179
}
177180
const sequenceString =
178-
packageDetails.sequenceNumber != null
179-
? ` (${packageDetails.sequenceNumber}${
180-
packageDetails.sequenceName
181-
? " " + packageDetails.sequenceName
181+
patchDetails.sequenceNumber != null
182+
? ` (${patchDetails.sequenceNumber}${
183+
patchDetails.sequenceName
184+
? " " + patchDetails.sequenceName
182185
: ""
183186
})`
184187
: ""
@@ -198,6 +201,9 @@ export function applyPatchesForApp({
198201
path,
199202
}),
200203
)
204+
// in case the package has multiple patches, we need to break out of this inner loop
205+
// because we don't want to apply more patches on top of the broken state
206+
break packageLoop
201207
} else {
202208
errors.push(
203209
createPatchApplicationFailureError({
@@ -209,21 +215,24 @@ export function applyPatchesForApp({
209215
pathSpecifier,
210216
}),
211217
)
218+
// in case the package has multiple patches, we need to break out of this inner loop
219+
// because we don't want to apply more patches on top of the broken state
220+
break packageLoop
212221
}
213222
} catch (error) {
214223
if (error instanceof PatchApplicationError) {
215224
errors.push(error.message)
216225
} else {
217226
errors.push(
218227
createUnexpectedError({
219-
filename: packageDetails.patchFilename,
228+
filename: patchDetails.patchFilename,
220229
error: error as Error,
221230
}),
222231
)
223232
}
224-
if (details.length > 0) {
225-
continue packageLoop
226-
}
233+
// in case the package has multiple patches, we need to break out of this inner loop
234+
// because we don't want to apply more patches on top of the broken state
235+
break packageLoop
227236
}
228237
}
229238
}
@@ -265,15 +274,19 @@ export function applyPatchesForApp({
265274
export function applyPatch({
266275
patchFilePath,
267276
reverse,
268-
packageDetails,
277+
patchDetails,
269278
patchDir,
270279
}: {
271280
patchFilePath: string
272281
reverse: boolean
273-
packageDetails: PackageDetails
282+
patchDetails: PackageDetails
274283
patchDir: string
275284
}): boolean {
276-
const patch = readPatch({ patchFilePath, packageDetails, patchDir })
285+
const patch = readPatch({
286+
patchFilePath,
287+
patchDetails,
288+
patchDir,
289+
})
277290
try {
278291
executeEffects(reverse ? reversePatch(patch) : patch, { dryRun: false })
279292
} catch (e) {

src/packageIsDevDependency.test.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe(packageIsDevDependency, () => {
1111
expect(
1212
packageIsDevDependency({
1313
appPath,
14-
packageDetails: getPackageDetailsFromPatchFilename(
14+
patchDetails: getPackageDetailsFromPatchFilename(
1515
"typescript+3.0.1.patch",
1616
)!,
1717
}),
@@ -21,9 +21,7 @@ describe(packageIsDevDependency, () => {
2121
expect(
2222
packageIsDevDependency({
2323
appPath,
24-
packageDetails: getPackageDetailsFromPatchFilename(
25-
"chalk+3.0.1.patch",
26-
)!,
24+
patchDetails: getPackageDetailsFromPatchFilename("chalk+3.0.1.patch")!,
2725
}),
2826
).toBe(false)
2927
})
@@ -32,7 +30,7 @@ describe(packageIsDevDependency, () => {
3230
expect(
3331
packageIsDevDependency({
3432
appPath,
35-
packageDetails: getPackageDetailsFromPatchFilename(
33+
patchDetails: getPackageDetailsFromPatchFilename(
3634
// cosmiconfig is a transitive dep of lint-staged
3735
"cosmiconfig+3.0.1.patch",
3836
)!,

src/packageIsDevDependency.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ import { existsSync } from "fs"
44

55
export function packageIsDevDependency({
66
appPath,
7-
packageDetails,
7+
patchDetails,
88
}: {
99
appPath: string
10-
packageDetails: PatchedPackageDetails
10+
patchDetails: PatchedPackageDetails
1111
}) {
1212
const packageJsonPath = join(appPath, "package.json")
1313
if (!existsSync(packageJsonPath)) {
1414
return false
1515
}
1616
const { devDependencies } = require(packageJsonPath)
17-
return Boolean(devDependencies && devDependencies[packageDetails.packageNames[0]])
17+
return Boolean(
18+
devDependencies && devDependencies[patchDetails.packageNames[0]],
19+
)
1820
}

src/patch/read.test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe(readPatch, () => {
2828
it("throws an error for basic packages", () => {
2929
readPatch({
3030
patchFilePath: "/test/root/patches/test+1.2.3.patch",
31-
packageDetails: getPackageDetailsFromPatchFilename("test+1.2.3.patch")!,
31+
patchDetails: getPackageDetailsFromPatchFilename("test+1.2.3.patch")!,
3232
patchDir: "patches/",
3333
})
3434

@@ -56,7 +56,7 @@ describe(readPatch, () => {
5656
it("throws an error for scoped packages", () => {
5757
readPatch({
5858
patchFilePath: "/test/root/patches/@david+test+1.2.3.patch",
59-
packageDetails: getPackageDetailsFromPatchFilename(
59+
patchDetails: getPackageDetailsFromPatchFilename(
6060
"@david+test+1.2.3.patch",
6161
)!,
6262
patchDir: "patches/",
@@ -87,7 +87,7 @@ describe(readPatch, () => {
8787
const patchFileName = "@david+test++react-native+1.2.3.patch"
8888
readPatch({
8989
patchFilePath: `/test/root/patches/${patchFileName}`,
90-
packageDetails: getPackageDetailsFromPatchFilename(patchFileName)!,
90+
patchDetails: getPackageDetailsFromPatchFilename(patchFileName)!,
9191
patchDir: "patches/",
9292
})
9393

@@ -116,7 +116,7 @@ describe(readPatch, () => {
116116
const patchFileName = "@david+test++react-native+1.2.3.patch"
117117
readPatch({
118118
patchFilePath: `/test/root/.cruft/patches/${patchFileName}`,
119-
packageDetails: getPackageDetailsFromPatchFilename(patchFileName)!,
119+
patchDetails: getPackageDetailsFromPatchFilename(patchFileName)!,
120120
patchDir: ".cruft/patches",
121121
})
122122

@@ -145,7 +145,7 @@ describe(readPatch, () => {
145145
const patchFileName = "@david+test++react-native+1.2.3.patch"
146146
readPatch({
147147
patchFilePath: `/test/root/packages/banana/patches/${patchFileName}`,
148-
packageDetails: getPackageDetailsFromPatchFilename(patchFileName)!,
148+
patchDetails: getPackageDetailsFromPatchFilename(patchFileName)!,
149149
patchDir: "patches/",
150150
})
151151

@@ -178,7 +178,7 @@ describe(readPatch, () => {
178178
const patchFileName = "@david+test++react-native+1.2.3.patch"
179179
readPatch({
180180
patchFilePath: `/test/root/packages/banana/.patches/${patchFileName}`,
181-
packageDetails: getPackageDetailsFromPatchFilename(patchFileName)!,
181+
patchDetails: getPackageDetailsFromPatchFilename(patchFileName)!,
182182
patchDir: ".patches/",
183183
})
184184

src/patch/read.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import { parsePatchFile, PatchFilePart } from "./parse"
77

88
export function readPatch({
99
patchFilePath,
10-
packageDetails,
10+
patchDetails,
1111
patchDir,
1212
}: {
1313
patchFilePath: string
14-
packageDetails: PackageDetails
14+
patchDetails: PackageDetails
1515
patchDir: string
1616
}): PatchFilePart[] {
1717
try {
@@ -33,7 +33,7 @@ export function readPatch({
3333
relativePatchFilePath.indexOf(patchDir),
3434
)}`,
3535
)
36-
fixupSteps.push(`npx patch-package ${packageDetails.pathSpecifier}`)
36+
fixupSteps.push(`npx patch-package ${patchDetails.pathSpecifier}`)
3737
if (patchBaseDir) {
3838
fixupSteps.push(
3939
`cd ${relative(resolve(process.cwd(), patchBaseDir), process.cwd())}`,
@@ -43,7 +43,7 @@ export function readPatch({
4343
console.error(`
4444
${chalk.red.bold("**ERROR**")} ${chalk.red(
4545
`Failed to apply patch for package ${chalk.bold(
46-
packageDetails.humanReadablePathSpecifier,
46+
patchDetails.humanReadablePathSpecifier,
4747
)}`,
4848
)}
4949

0 commit comments

Comments
 (0)