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.

[Feature] Have `yarn workspaces foreach -t` automatically include workspace dependents

See original GitHub issue
  • I’d be willing to implement this feature (contributing guide)
  • This feature is important to have in this repository; a contrib plugin wouldn’t do

Describe the user story

I have a monorepo with several private packages plus several deployable apps that consume them. Typically, when I want to build an individual app, I’d like to ensure that the packages it depends have up-to-date builds. I’m currently able to build all apps at once with

$ yarn workspaces foreach -A --topological-dev run build

This command lets me build everything while making sure dependents resolve things like TypeScript types in the correct order. However, if I want to build one individual app and its dependencies, I have to re-do all the work Yarn does in traversing the dependency graph to figure out what dependencies need built before passing them as --include arguments.

Describe the solution you’d like

Say my app is @kherock/app, and it depends on @kherock/package-a and @kherock/package-b. What I’m most interested in right now is that when I run

$ yarn workspaces foreach --include @kherock/app --topological-dev run build

Yarn will additionally run the command for the workspaces of package-a and package-b. I want to say that this is intuitive behavior given that I’m already concerned with the implications of running foreach with --topological(-dev). If I want to explicitly avoid building certain packages, I can still mark them for exclusion with --exclude.

Describe the drawbacks of your solution

This does introduce more complexity at the risk of being confusing if not documented properly. I’ll note that the old behavior can be preserved in this implementation without much effort (especially since -A is being considered for removal in v3):

$ yarn workspaces foreach -A --exclude '*' --include @kherock/app --topological-dev run build

Describe alternatives you’ve considered

I’m also considering making a plugin building off of the existing foreach command that allows for more powerful queries on the dependency graph (e.g. run x for all workspaces listing package y as a dependent or vice-versa).

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
merceyzcommented, Dec 16, 2021

It’s supported, it was implemented in https://github.com/yarnpkg/berry/pull/2835. To do what @kherock is asking for in https://github.com/yarnpkg/berry/issues/2389#issuecomment-762834916 you would run

yarn workspaces foreach --from @kherock/app-1 --from @kherock/app-2 --topological-dev --recursive run build

https://yarnpkg.com/cli/workspaces/foreach#examples

1reaction
merceyzcommented, Jan 19, 2021

Sounds a lot like the --recursive flag https://yarnpkg.com/cli/workspaces/foreach#options-R%2C-recursive

yarn workspace @kherock/app workspaces foreach --topological-dev --recursive run build
Read more comments on GitHub >

github_iconTop Results From Across the Web

`yarn workspaces foreach` | Yarn - Package Manager
If -t,--topological is set, Yarn will only run the command after all workspaces that it depends on through the dependencies field have successfully...
Read more >
A guide through The Wild Wild West of setting up a mono repo ...
Yarn Workspaces is a feature that allows users to install dependencies from multiple package.json files in subfolders of a single root package.json file,...
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 >
yarn-workspaces-foreach - npm package - Snyk
Ensure you're using the healthiest npm packages. Snyk scans all the packages in your projects for vulnerabilities and provides automated fix advice. Get...
Read more >
Introducing Yarn 2 ! - DEV Community ‍ ‍
This work couldn't have been possible without the support from many people from the open-source ... New Command: yarn workspaces foreach.
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