question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Correct ts-patch installation in monorepo

See original GitHub issue

Hi! 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.1
  • typescript: 4.5.2
  • postinstall script to install ts-patch: (ts-patch install) || true (we need || true part to prevent yarn workspaces focus --all --production to fail when ts-patch will 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:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
nonaracommented, Dec 3, 2021

Yarn3 doesn’t support prepare script

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.

I don’t understand your idea

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.

Agree. I moved ts-patch install to root package.json (you can check PR, if you want). Thank you for your quick and high-quality response.

Excellent! You’re very welcome. Glad you got it working.

0reactions
pkerschbaumcommented, Mar 13, 2022

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 that tsc was broken in some of the workspaces. Removing node_modules and 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 with lerna run --parallel or something.
It seems like the parallel invocations of ts-patch boiled down to the fact that some of my workspaces depend on each other (as it is often the case in monorepos). yarn install would then run multiple times in those “workspace dependencies” because npm scripts like postinstall or 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:

  • Avoid running ts-patch in any NPM script of the workspace package.json’s
  • And instead, I added this postinstall script in the root package.json:
    "postinstall": "lerna exec --parallel \"yarn ts-patch install -s\""
    

I have this workaround in place for a couple of days and not a single time I had a broken tsc anymore.
In my case, every workspace is a TypeScript project, but if not one could adapt that lerna exec command so that it does not break if it fails in any of the workspaces.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Better support for monorepos · Issue #277 · ds300/patch ...
The package a should work in isolation from the top-level folder and other packages. When I do a npm i on the top-level...
Read more >
The Case for Monorepos: A sane Workspace Setup (Part 2)
Learn how to setup dev tooling in a monorepo, run tasks efficiently, release multiple packages and overcome common DevOps challenges.
Read more >
Getting Started - monorepo guide
To setup a build process for the packages within your monorepo, execute the following commands from the root level of your project. yarn...
Read more >
Setting up a monorepo with Lerna for a TypeScript project
Learn how to setup a monorepo with Lerna and configure it for ... package requires its own tsconfig.json file for correct relative paths....
Read more >
Monorepos in JavaScript & TypeScript - Robin Wieruch
A tutorial how to use a monorepo architecture in frontend JavaScript and ... For now, navigate into the repository and install all the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found