[Bug?]: Patching Typescript causes "Cannot apply hunk" error
Self-service
- [ ] I'd be willing to implement a fix
Describe the bug
I tried Yarn's native patches, but it had a lot of issues (e.g. not being able to update a patch easily or update a patched package), so I switched back to patch-package. I have a patch for Typescript, which is patched correctly using patch-package. However, when I run yarn install, I get:
➤ YN0000: ┌ Fetch step
➤ YN0066: │ typescript@patch:typescript@npm%3A5.4.2#optional!builtin<compat/typescript>::version=5.4.2&hash=e012d7: Cannot apply hunk #8
➤ YN0000: │ /*preferSymlinks*/
➤ YN0000: │ true,
➤ YN0000: │ (path, isRedirect) => {
➤ YN0028: │ - const isInNodeModules = pathContainsNodeModules(path);
➤ YN0028: │ + let isInNodeModules = pathContainsNodeModules(path);
➤ YN0028: │ + const pnpapi = getPnpApi(path);
➤ YN0028: │ + if (!isInNodeModules && pnpapi) {
➤ YN0028: │ + const fromLocator = pnpapi.findPackageLocator(importingFileName);
➤ YN0028: │ + const toLocator = pnpapi.findPackageLocator(path);
➤ YN0028: │ + if (fromLocator && toLocator && fromLocator !== toLocator) {
➤ YN0028: │ + isInNodeModules = true;
➤ YN0028: │ + }
➤ YN0028: │ + }
➤ YN0000: │ allFileNames.set(path, { path: getCanonicalFileName(path), isRedirect, isInNodeModules });
➤ YN0000: │ importedFileFromNodeModules = importedFileFromNodeModules || isInNodeModules;
➤ YN0000: │ }
I'm pretty sure this patch came from Yarn. I think Yarn is attempting to patch typescript/src/compiler/binder.ts, whereas my patches are for typescript/lib/lib.dom.d.ts, so there shouldn't be any conflicts.
To reproduce
{
"name": "test",
"version": "0.0.1",
"private": true,
"type": "module",
"node": ">=16",
"packageManager": "[email protected]",
"scripts": {
"postinstall": "patch-package",
},
"dependencies": {
"patch-package": "^8.0.0",
"postinstall-postinstall": "^2.1.0",
"typescript": "^5.4.2"
},
"license": "MIT"
}
Make any changes to node_modules/typescript/lib/lib.dom.d.ts and run npx patch-package typescript, then yarn.
Environment
System:
OS: Linux 5.15 Ubuntu 22.04.3 LTS 22.04.3 LTS (Jammy Jellyfish)
CPU: (20) x64 12th Gen Intel(R) Core(TM) i7-12700H
Binaries:
Node: 20.11.1 - /tmp/xfs-9c9d401f/node
Yarn: 4.0.2 - /tmp/xfs-9c9d401f/yarn
npm: 10.5.0 - /usr/bin/npm
Additional context
No response
I ran into something similar with Yarn 3.6.1 and Typescript 5.2.2 - 5.4.3 (tried 'em all). It didn't happen on my local machine (Mac) but happened on the build server (Ubuntu).
By rolling the build server back to Node 18.x I got the issue to stop. I'm not sure why 20.x on Ubuntu was giving me trouble, but that was how I worked around it.
+1
We are intermittently running into this on our CI in https://github.com/adobe/react-spectrum on yarn 4.2.2
➤ YN0000: ┌ Fetch step
➤ YN0066: │ typescript@patch:typescript@npm%3A5.5.2#optional!builtin<compat/typescript>::version=5.5.2&hash=b45daf: Cannot apply hunk #11
our node version on CI is 18.20.3
it does not occur locally where I'm also running the same node version
CI is Ubuntu and local is MacOS
I was also running into a similar issue using Node 20 in CI but upgrading to Berry version 4.4.1 seems to have resolved it. That release contains some changes to the typescript patch.
same issue with node 22 and ts 5.7.3
Could this explain why on one machine running yarn lint shows hundreds of import/no-unresolved eslint errors while the other is just fine? Like if yarn patch fails to apply it's own patch to typescript then tsc fails to generate imports that can be resolved!?