Correct ts-patch installation in monorepo
See original GitHub issueHi! We have a monorepo build upon on yarn 3 with node-modules linker (you can explorer it here https://github.com/Byndyusoft/nest-template, if you want). In each package.json (except root package.json with workspaces) we have:
ts-patch: 2.0.1typescript: 4.5.2postinstallscript to install ts-patch:(ts-patch install) || true(we need|| truepart to preventyarn workspaces focus --all --productionto fail whents-patchwill not be found)
yarn 2/3 run postinstall in parallel (see here), so different launches of ts-patch will be concurrent for modify same files (you can view incorrect patch here). I think it is heisenbug and hard to reproduce, re run job can resolve fail build.
I think the best way to solve this issue is use a lock files (as in Linux package managers e.g. apt or pacman) or any other mutex to prevent multiple launches for same typescript package.
Maybe we should install ts-patch differently and this installation way should be added to documentation.
@nonara what do you think?
Thanks for any help!
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)

Top Related StackOverflow Question
Ah, ok. Thanks. I remember someone else mentioning something along those lines. Looking forward to familiarizing myself more with y3 when I can finish the rewrite.
Not sure if it will fit your situation, but I’ve taken to using
npx <cli>whenever I am not sure that a package will be there (ie. if yarn install hasn’t happened yet or I might have wiped the node_modules)Essentially, it guarantees that something runs, whether or not it’s been locally installed. It prefers the local install, when it exists, otherwise it installs to a temp location for a one-shot run. That may not be what you’re looking for, though. I don’t know what your build / pipeline process does. But thought I’d share in case it helps.
Excellent! You’re very welcome. Glad you got it working.
In case anyone helps this:
I am working in a monorepo using Yarn v1 and I also had the problem that sometimes, after
yarn install, it seemed thattscwas broken in some of the workspaces. Removingnode_modulesand installing again would then sometimes solve the issue, but not always.I think this was because of parallel invocations of
ts-patch, but not because I ran it withlerna run --parallelor something.It seems like the parallel invocations of
ts-patchboiled down to the fact that some of my workspaces depend on each other (as it is often the case in monorepos).yarn installwould then run multiple times in those “workspace dependencies” because npm scripts likepostinstallor similar run every time the workspace is “installed” as a dependency for another workspace.And by chance this would happen at the same time (since Yarn v1 does install packages in parallel to speed up things).
My workaround, which is sufficient for my monorepo project:
ts-patchin any NPM script of the workspacepackage.json’spostinstallscript in the rootpackage.json:I have this workaround in place for a couple of days and not a single time I had a broken
tscanymore.In my case, every workspace is a TypeScript project, but if not one could adapt that
lerna execcommand so that it does not break if it fails in any of the workspaces.