yarnpkg / berry

📦🐈 Active development trunk for Yarn ⚒
https://yarnpkg.com
BSD 2-Clause "Simplified" License
7.37k stars 1.1k forks source link

[Feature]: `yarn workspaces focus` should make all subsequent operations "focused" #2129

Open SimenB opened 3 years ago

SimenB commented 3 years ago

Describe the user story

Essentially https://twitter.com/VLecrubier/status/1301620546589913088.

Our current workflow when building a module/artifact on CI (with yarn v1, fwiw) is:

That last step is still needed if we were to migrate to Yarn v2 as we cannot do yarn workspaces foreach -pt run build as yarn will run that in all workspaces rather than the ones focus has... focused.

Describe the solution you'd like

After running yarn workspace focus all further yarn workspaces * commands should respect the fact we're currently "focused" and not do work on all workspaces. I'm specifically after yarn workspaces foreach run, but I think other commands like yarn why or yarn up should also only operate on the "focused" workspace and its dependencies.

As an aside, for this I think it'd make more sense if focus just "focused" without installing anything. So yarn workspaces focus foobar would just set some somewhere in .yarn/ I guess, and when we then run yarn install afterwards, only foobar (and its dependencies) would be "installed".

Describe the drawbacks of your solution

Not sure... It would probably mean more forking of logic ("all workspaces" vs "some workspaces") in multiple places, but I don't think there's any drawbacks beyond the implementation aspect of it?

Describe alternatives you've considered

Not use yarn workspaces focus at all as it doesn't work for our use cases and keep using a manual approach.

Additional context

Happy to chat on Discord about what other stuff we do on CI to build if applicable.

andreialecu commented 3 years ago

Just for reference: there's a plugin at https://gitlab.com/Larry1123/yarn-contrib/-/tree/master/packages/plugin-production-install that can probably handle this scenario.

It can build dependent workspaces using yarn pack in the correct order, as oao would do. So all you need to do is to define a prepack script in all workspaces with their build command, and use package.json/files or .npmignore to define what the built artifacts are.

Depending on what your actual requirements are, you may be able to use it as a better approach than using focus. I've been using it successfully in several projects and favor it greatly vs the workspace focus command, for building/deploying.

SimenB commented 3 years ago

Thanks for the link! I would prefer if Yarn core made the focus command more useful, but using custom plugins definitely is an option. Although unlikely one we'll pursue over sticking to Yarn v1 as the workflow we have there works pretty good.

focus might not be enough for us regardless as we run jest and eslint from the root of the repo, and since focus leaves all other files in the repo, those commands will potentially fail as not all packages will be built. So we'll probably keep using our script which does rm -rf. That's a thing that's weird about our setup tho, and could be worked around by us asking yarn "what workspaces are installed" and provide a filter based on that.

Anyways, digressing 🙂

arcanis commented 3 years ago

The problem with improving focus is that:

So all in all I'd prefer to see this live in a contrib plugin (such as @Larry1123's one), and only moved into our org once a consistent workflow has been refined.

(Regarding yarn workspaces foreach in particular, cf the new -R,--recursive flag which is likely what you need)

andreialecu commented 3 years ago

My 2 cents, since this is also an use-case I've been interested in. I think the best "catch-all" solution here would be to have a parameter to the focus command:

I vaguely remember suggesting this before in some other issue but I can't find it.

yarn workspaces focus --outDir /some/new/path would copy the focused workspace and all dependencies to the given path. This can easily be implemented as a plugin as well though.

SimenB commented 3 years ago

Fair enough! In that case I'd argue that focus should be renamed to focus-install or something - it's only purpose is installation, it's not really a generic "focus" on a workspace at all. The docs state this, but I think being more precise with the name of the command itself better communicates what it does (and, more importantly, what it doesn't do).

Or possibly split up into more subcommands? So just yarn workspaces focus foobar is an error, the user should do yarn workspaces focus foobar install. That way a separate plugin could hook into it and expose more commands than install - such as run etc. Not sure, very half baked idea.

andreialecu commented 3 years ago

@SimenB it seems that your request already works, but it's not yet released and you need to install the plugin from sources.

yarn plugin import from sources plugin-workspace-tools
cd packages/my-workspace && yarn workspaces focus && yarn workspaces foreach -Rt run build

build will only run on my-workspace and its workspace dependencies, in topological order.