berry icon indicating copy to clipboard operation
berry copied to clipboard

Cleanup unneeded yarn packages cache when running `yarn workspaces focus --production`

Open MatsG23 opened this issue 2 years ago • 3 comments

  • [ ] I'd be willing to implement this feature (contributing guide)
  • [X] This feature is important to have in this repository; a contrib plugin wouldn't do

Describe the user story

To host my Node.js app on my personal server in production, I want to remove all development dependencies. This is what I use yarn workspaces focus --production for. I am using the Yarn Plug'n'Play feature which requires to have the .pnp.* files and .yarn/cache folder on the server as a replacement to node_modules. I noticed that, even tho I ran the yarn workspaces focus --production command, the development packages are still cached in .yarn/cache .

Describe the solution you'd like

It would make sense if yarn workspaces focus --production would cleanup the .yarn/cache folder by deleting all ZIP files (caches) of the omitted development packages.

Describe the drawbacks of your solution

I cannot think of a drawback of my solution. The yarn install command already has the logic for cleaning unneeded cache files built-in.

Describe alternatives you've considered

I do not think there is an alternative because what I am describing is an essential feature imo. I do not see a use case where you would like to have the cache files of the development packages on the production server because you do not use any development package on there. Under normal circumstances, I as a developer, would not run the yarn workspaces focus --production command on the computer I develop on because there is no need to. This would have been the only case I could come up with where you would like to switch back to using the development packages and where you would benefit from still having them cached.

MatsG23 avatar Feb 18 '23 14:02 MatsG23

One drawback for this would be CI situations that grab the .yarn/cache folder from CI caching, and then only link the necessary dependencies for that run using focus.

This is a pretty useful setup, as it allows keeping a single cache across all workflows, while retaining the link-time speed benefits of focus. Otherwise a CI cache for every possible workspace permutation would be required.

me4502 avatar Feb 21 '23 03:02 me4502

I have the same observation. For several reasons I try to clean up dev dependencies:

  • in production they are not necessary
  • they represent a security risk
  • I want to reduce the weight of my docker image

I understand the interest of the cache for the development process and CI. But the goal is production. Isn't there a way to export only the production dependencies.

guiyomh avatar May 05 '23 07:05 guiyomh

I'd propose yarn cache clean --production which would clean cache, leaving only dependencies needed in production.

wojtekmaj avatar Sep 30 '24 09:09 wojtekmaj