Skip to content

Commit ce03f34

Browse files
committed
throw error when patch includes symbolic link
1 parent 40da347 commit ce03f34

File tree

1 file changed

+75
-21
lines changed

1 file changed

+75
-21
lines changed

src/makePatch.ts

Lines changed: 75 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import {
2020
} from "./PackageDetails"
2121
import { resolveRelativeFileDependencies } from "./resolveRelativeFileDependencies"
2222
import { getPackageResolution } from "./getPackageResolution"
23+
import { parsePatchFile } from "./patch/parse"
24+
import { gzipSync } from "zlib"
2325

2426
function printNoPackageFoundError(
2527
packageName: string,
@@ -171,31 +173,83 @@ export function makePatch({
171173
)
172174
console.warn(`⁉️ There don't appear to be any changes.`)
173175
process.exit(1)
174-
} else {
175-
const packageNames = packageDetails.packageNames
176-
.map(name => name.replace(/\//g, "+"))
177-
.join("++")
178-
179-
// maybe delete existing
180-
getPatchFiles(patchDir).forEach(filename => {
181-
const deets = getPackageDetailsFromPatchFilename(filename)
182-
if (deets && deets.path === packageDetails.path) {
183-
unlinkSync(join(patchDir, filename))
184-
}
185-
})
176+
return
177+
}
178+
179+
try {
180+
parsePatchFile(diffResult.stdout.toString())
181+
} catch (e) {
182+
if (
183+
(e as Error).message.includes("Unexpected file mode string: 120000")
184+
) {
185+
console.error(`
186+
⛔️ ${chalk.red.bold("ERROR")}
186187
187-
const patchFileName = `${packageNames}+${packageVersion}.patch`
188+
Your changes involve creating symlinks. patch-package does not yet support
189+
symlinks.
190+
191+
️Please use ${chalk.bold("--include")} and/or ${chalk.bold(
192+
"--exclude",
193+
)} to narrow the scope of your patch if
194+
this was unintentional.
195+
`)
196+
} else {
197+
const outPath = join(process.cwd(), "patch-package-error.tar.gz")
198+
writeFileSync(
199+
outPath,
200+
gzipSync(
201+
JSON.stringify({
202+
error: { message: e.message, stack: e.stack },
203+
patch: diffResult.stdout.toString(),
204+
}),
205+
),
206+
)
207+
console.error(`
208+
⛔️ ${chalk.red.bold("ERROR")}
209+
210+
patch-package was unable to read the patch-file made by git. This should not
211+
happen.
212+
213+
A diagnostic file was written to
214+
215+
${outPath}
216+
217+
Please attach it to a github issue
218+
219+
https://github.com/ds300/patch-package/issues/new?title=New+patch+parse+failed&body=Please+attach+the+diagnostic+file+by+dragging+it+into+here+🙏
220+
221+
Note that this diagnostic file will contain code from the package you were
222+
attempting to patch.
188223
189-
const patchPath = join(patchesDir, patchFileName)
190-
if (!existsSync(dirname(patchPath))) {
191-
// scoped package
192-
mkdirSync(dirname(patchPath))
224+
`)
193225
}
194-
writeFileSync(patchPath, diffResult.stdout)
195-
console.log(
196-
`${chalk.green("✔")} Created file ${join(patchDir, patchFileName)}`,
197-
)
226+
process.exit(1)
227+
return
198228
}
229+
230+
const packageNames = packageDetails.packageNames
231+
.map(name => name.replace(/\//g, "+"))
232+
.join("++")
233+
234+
// maybe delete existing
235+
getPatchFiles(patchDir).forEach(filename => {
236+
const deets = getPackageDetailsFromPatchFilename(filename)
237+
if (deets && deets.path === packageDetails.path) {
238+
unlinkSync(join(patchDir, filename))
239+
}
240+
})
241+
242+
const patchFileName = `${packageNames}+${packageVersion}.patch`
243+
244+
const patchPath = join(patchesDir, patchFileName)
245+
if (!existsSync(dirname(patchPath))) {
246+
// scoped package
247+
mkdirSync(dirname(patchPath))
248+
}
249+
writeFileSync(patchPath, diffResult.stdout)
250+
console.log(
251+
`${chalk.green("✔")} Created file ${join(patchDir, patchFileName)}`,
252+
)
199253
} catch (e) {
200254
console.error(e)
201255
throw e

0 commit comments

Comments
 (0)