Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support bun run --workspace #2232

Closed
Tracked by #2450
swissspidy opened this issue Feb 28, 2023 · 19 comments · Fixed by #8185
Closed
Tracked by #2450

Support bun run --workspace #2232

swissspidy opened this issue Feb 28, 2023 · 19 comments · Fixed by #8185
Labels
enhancement New feature or request npm Something that relates to the npm-compatible client

Comments

@swissspidy
Copy link

What version of Bun is running?

0.5.7

What platform is your computer?

Darwin 22.3.0 arm64 arm

What steps can reproduce the bug?

In a monorepo with npm workspaces, scripts defined in a specific package's package.json can be run with

npm run my-script --workspace packages/my-package

What is the expected behavior?

Bun should allow me to run the package's script via the --workspace arg.

A possible workaround is:

(cd packages/my-package && bun run my-script)

What do you see instead?

error: missing script "my-script"

Additional information

No response

@swissspidy swissspidy added the bug Something isn't working label Feb 28, 2023
@Electroid Electroid added enhancement New feature or request npm Something that relates to the npm-compatible client and removed bug Something isn't working labels Mar 1, 2023
@Electroid
Copy link
Contributor

Thanks for reporting, we were talking about implementing this the other day. We'll do it

@ferdy-roz
Copy link

ferdy-roz commented Sep 9, 2023

Is this planned for the near future? this might be the one thing preventing us from migrating from pnpm to bun as without it we would have to do the cd workaround @swissspidy mentioned, which makes package.json very very chunky.

Really I want support for the --workspaces flag (plural), or some equivalent of pnpm's -r

@itpropro
Copy link

For bun as a drop-in replacement, it would really help to have --workspace(s) --filter and other monorepo related tooling available.
Most things with bun work extremely well and much better than I expected, are there any plans to improve the monorepo support in the near future @Electroid ?

@sheijne
Copy link

sheijne commented Sep 12, 2023

(cd packages/my-package && bun run my-script)

As a an alternative workaround, --cwd seems to work: bun run --cwd packages/my-package build.

Really I want support for the --workspaces flag (plural), or some equivalent of pnpm's -r

Would be nice to be able to reference a workspace based on the package name, ie: bun run --workspace @my-org/my-package build.

On top of this it'd be great to have wildcards: bun run --workspace @my-org/my-*

@colinhacks colinhacks changed the title bun run does not support --workspace arg Support bun run --workspace and bun run --workspaces Sep 13, 2023
@colinhacks colinhacks changed the title Support bun run --workspace and bun run --workspaces Support bun run --workspace Sep 13, 2023
@kyle-belle
Copy link

any updates on this?

@xlc
Copy link
Contributor

xlc commented Oct 11, 2023

It will be great if bun run can support this feature from yarn run

Otherwise, if the specified name contains a colon character and if one of the workspaces in the project contains exactly one script with a matching name, then this script will get executed.

@hyoretsu
Copy link

hyoretsu commented Nov 8, 2023

Bump, it's a bummer that such a great tool doesn't have this feature.

@Blankeos
Copy link

Blankeos commented Jan 7, 2024

+1 on this. Would be useful to be able to do something like this. I'm actually currently using the --cwd that @sheijne mentioned. Thanks!

What I'm doing

There's a Root package.json and an apps/server/package.json (a Workspace).

  • In the root package.json, I only do bun run --cwd on scripts from apps/server/package.json.
  • In apps/server/package.json, I write the actual script I wanna execute.
{
  // ./package.json (In Root of monorepo) | NOTICE HOW IT JUST RUNS PACKAGE.JSON SCRIPTS WITH BUN
  "scripts": {
    "server:dev": "bun run --cwd apps/server dev",
    "db:start": "bun run --cwd apps/server db:start",
    "db:stop": "bun run --cwd apps/server db:stop",
    "migrate:status": "bun run --cwd apps/server migrate:status",
    "migrate:create": "bun run --cwd apps/server migrate:create",
    "migrate:up": "bun run --cwd apps/server migrate:up",
    "migrate:down": "bun run --cwd apps/server migrate:down"
  },
}
{
  // apps/server/package.json
  "name": "server",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "bun run --watch src/index.ts",
    "db:start": "docker-compose up -d --build",
    "db:stop": "docker-compose stop",
    "migrate:create": "sh scripts/migrate-create.sh src/database/migrations",
    "migrate:status": "dotenv -- goose -dir src/database/migrations postgres $DATABASE_URL status",
    "migrate:up": "dotenv -- goose -dir src/database/migrations postgres $DATABASE_URL up",
    "migrate:down": "dotenv -- goose -dir src/database/migrations postgres $DATABASE_URL down",
    "seed": "bun run scripts/seed.ts"
  }
}

1 thing to watch out for:

When you have migration scripts that use dotenv-cli the .env file of the folder, the above example doesn't work for 3 scripts there:

  • migrate:create Works
  • migrate:status, migrate:up, and migrate:down DO NOT WORK.

Why? I'm actually not completely sure why. But it does come up when I use the "root" version of the script. It works properly when I use the "apps/server" (workspace) version of the script. I almost ruled it out as a Bun workspace issue.

But actually the culprit is dotenv-cli.

Solution for this

  1. "Subscript Encapsulation". (Fortunately they have docs for this).
  2. Another is just encapsulating the in an .sh file and let dotenv-cli run that script. Whcih turns out, I was already doing it for migrate:create (This is something I figured out accidentally on my own)
    "migrate:create": "sh scripts/migrate-create.sh src/database/migrations",
    - "migrate:status": "dotenv -- goose -dir src/database/migrations postgres $DATABASE_URL status",
    - "migrate:up": "dotenv -- goose -dir src/database/migrations postgres $DATABASE_URL up",
    - "migrate:down": "dotenv -- goose -dir src/database/migrations postgres $DATABASE_URL down",

Changed to:

    "migrate:create": "sh scripts/migrate-create.sh src/database/migrations",
    + "_migrate:status": "goose -dir src/database/migrations postgres $DATABASE_URL status",
    + "migrate:status": "dotenv -- bun run _migrate:status",
    + "_migrate:up": "goose -dir src/database/migrations postgres $DATABASE_URL up",
    + "migrate:up": "dotenv -- bun run _migrate:up",
    + "_migrate:down": "goose -dir src/database/migrations postgres $DATABASE_URL down",
    "migrate:down": "dotenv -- bun run _migrate:down",

I'll just leave these comments here for anyone attempting to do the same thing with Bun.

@choephix
Copy link

Is there complexity involved in shipping this feature out?
Being able to easily run a specific workspace's script is the only reason we're not switching to bun yet, tbh

@rhuanbarreto
Copy link

For now we are using moonrepo with full bun integration to be the task runner.

@zemse
Copy link

zemse commented Mar 4, 2024

I'd like to propose additional helpers for bun workspaces:

  • bun add --workspace: useful to install package in a workspace package from while in root wd itself
  • bun run --workspaces --if-present: execute a script in all workspaces if it is present. E.g. I want to build everything.

@wesleycoder
Copy link

wesleycoder commented Mar 19, 2024

For those like me stumbling into this nowadays, I'm following this recommendation by @cs-clarence on #5350 to use Thinkmill/manypkg by adding it and this script to my root package.json:

{
  "name": "my-project",
  "private": true,
  "scripts": {
    "pkg": "manypkg run"
  },
  "workspaces": [
    "apps/*",
    "packages/*"
  ],
  "devDependencies": {
    "@manypkg/cli": "^0.21.3"
  }
}

With this we can just bun pkg <children-pattern> <script> ex:
bun pkg @proj dev -> should start all dev scripts on sub-packages containing @proj on their name
See more on manypkg docs

@Jarred-Sumner
Copy link
Collaborator

Jarred-Sumner commented Apr 11, 2024

Bun v1.1.4 adds support for bun --filter which is a more powerful version of this feature. You can run workspace scripts in parallel (following dependency order). It works whether or not you're using "workspaces" in package.json and without bun install.

The equivalent of npm run --workspace looks something like:

bun --filter="my-workspace-pkg" my-script-name

To run the build script in all the workspace packages that haveutil in the name:

bun run --filter="*util*" build

We'll have a page for this in bun.sh/docs later today

gif

Fixed in #8185

@hyoretsu
Copy link

hyoretsu commented Apr 11, 2024

Bro, it's literally a drop-in replacement for running commands in a workspace, if you want to do just that you can. But it also has so much more potential.
bun --filter backend dev and bun --filter frontend dev There you go. But you can also simply do bun --filter "*" dev and voila.

@wesleycoder
Copy link

@johnnybenson this is not a new api, anyone that used workspaces with pnpm will be familiar with --filter.
https://pnpm.io/filtering

@jylin
Copy link

jylin commented Apr 17, 2024

The exit code for this features seems to always be 0. Should it exit 1 if any of the commands returns an error?

@wesleycoder
Copy link

I've tried to test this today using canary (bun upgrade --canary, which installed 1.1.4)
But every usage I've tried always return error: No packages matched the filter

I've took a look to the docs and the cli.zig on the main branch and unless I misunderstood I believed this was supposed to be on canary already.

Could anyone confirm if this is already available on canary?
If it was supposed to be working on canary I think it may be somewhat broken.
I'm able to provide more info or tests, just ping if needed :)

@dror-weiss
Copy link

@wesleycoder , I'm on 1.1.4 as well, and this message (error: No packages matched the filter) happened to me whenever there is a problem with the format.

The filter feature supports calling a script within the workspace. I needed to execute a command in a workspace but it's not supported yet so I opened a new issue.

@wesleycoder
Copy link

I've tested with a Github Codespaces machine on this minimal repository

Neither of these works:

curl -fsSL https://bun.sh/install | bash
source /home/codespace/.bashrc
bun --version # 1.1.4

bun run --filter 'a' test
# error: No packages matched the filter

bun --filter 'a' test
# >>> Installed to the root package instead of installing to `packages/a`

bun upgrade --canary
bun --version # 1.1.4

bun run --filter 'a' test # same results
bun --filter 'a' test # same results

Now I believe it's not an issue on my machine and it's definitely broken, so I've open another issue: #10367

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request npm Something that relates to the npm-compatible client
Projects
None yet
Development

Successfully merging a pull request may close this issue.