bun icon indicating copy to clipboard operation
bun copied to clipboard

`bun add` or `bun install` ALWAYS hoists packages in workspace/monorepo

Open ansh opened this issue 1 year ago • 6 comments

What version of Bun is running?

1.0.15+b3bdf22eb

What platform is your computer?

Darwin 23.0.0 arm64 arm

What steps can reproduce the bug?

  1. Create a bun monorepo
{
  "name": "my-project",
  "private": true,
  "workspaces": [
    "backend",
    "frontend"
  ]
}
  1. In this example, I have a frontend and backend folder that hosts two apps. The backend is an express app and the frontend is a react-native app.
  2. If I cd backend or cd frontend into a directory and run bun add uuid (uuid is just an example package), it creates a "dependencies" object in my root package.json which doesn't make sense. The package doesn't need to be hoisted if it is just being used in one package. This happens no matter which package I try installing.

What is the expected behavior?

The package is added to frontend/package.json or backend/package.json depending on what the cwd is but not in the root package.json

What do you see instead?

The package is in the root package.json like this:

{ "name": "my-project", "private": true, "workspaces": [ "backend", "frontend" ], "dependencies": { "uuid": "^9.0.0" } }

Additional information

No response

ansh avatar Dec 09 '23 10:12 ansh

I'm getting a slightly different behaviour:

  • When running "bun install a-package" it gets added to the root package.json even when run in one of the workspaces.
  • When running "bun add a-package" it does get added to the expected (cwd) package.json. BUT then the lockfile gets messed up (seems to only contain dependencies of the (cwd) package.json after this - I need to run a new bun install to fix it). (Added a new issue #7844 for this since it could probably be considered a separate issue)

Note: I'm specifying my workspace slightly different:

"workspaces": [
  "apps/*",
  "packages/*"
],

robertherber avatar Dec 25 '23 18:12 robertherber

I can replicate this issue in a mono-repo using bun install. Any new packages added within a repo are hoisted to the root instead.

bun add works correctly though in terms of the packages being added to the repo as @robertherber mentioned.

adiologydev avatar Feb 07 '24 08:02 adiologydev

I have to add packages via bun add <pkg>, which corrupts the lockfile. Then I have to run bun i.

bun.lockb file is missing in child repos.

I use this script for now:

{
  "scripts": {
    "predev": "bun i && cp ../../bun.lockb ."
  }
}

varna avatar Feb 13 '24 15:02 varna

Related solution here until bun supports monorepos fully, use it by installing @zemble/utils and running bun add-safe <pkg-name> or simply copying the script to your project :)

robertherber avatar Feb 13 '24 18:02 robertherber

Related solution here until bun supports monorepos fully, use it by installing @zemble/utils and running bun add-safe <pkg-name> or simply copying the script to your project :)

Thank you for this @robertherber, we'll use this script for the time being.

Hoping @Jarred-Sumner has a native fix for this coming soon! 🤞🏼

adiologydev avatar Feb 13 '24 18:02 adiologydev

I don't know if it is related, but I have a monorepo with some Next.js applications. Bun is hoisting the node_modules/.bin/next directory, which causes a lot of errors when I want to build my project. In development mode, everything works. Copying the node_modules/.bin/next from another project makes the build work.

stefanosandes avatar Apr 09 '24 15:04 stefanosandes

I just wanted to also +1 this. We are using bun at the moment through turbo and honestly, getting a Docker container ready and using it dev wise has been the most painful experience ever.

XavierGeerinck avatar Apr 11 '24 07:04 XavierGeerinck