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

Native bun support - significant speed increases possible. #21075

Closed
bhouston opened this issue Jan 10, 2024 · 21 comments
Closed

Native bun support - significant speed increases possible. #21075

bhouston opened this issue Jan 10, 2024 · 21 comments
Assignees
Labels

Comments

@bhouston
Copy link

bhouston commented Jan 10, 2024

Description

Bun has been an often requested feature for NX see here:

Previously, it was not prioritized/accepted because Bun was pre-release. But Bun is now > 1.0. So now would be the time.

Motivation

I've done some tests by attempting to manually get most executors to run using bun in this template (TypeScript + React/Vite + Koa Server) mono repository setup with NX.

Using node.js + npm both on the latest, I get these build times:

Screenshot 2024-01-10 at 9 06 03 AM

(from here: https://github.com/bhouston/template-typescript-monorepo/actions/runs/7469814936)

If I switch to my hack in of bun, I get these much improved build times:

Screenshot 2024-01-10 at 9 06 24 AM

(from here: https://github.com/bhouston/template-typescript-monorepo/actions/runs/7476000610)

That is nearly 30% faster and this isn't even a complex project. I have a production project with a nearly identical setup (but a lot more code) and I am seeing a 4x speed up when it comes to running build + test. The degree of speed improvement cannot be ignored - 4x is absolute insane and generally unheard of.

My hack of bun into my template repository can be found here - you can run it locally easily:

bhouston/template-typescript-monorepo#12

My docker build fails in the CI though because my hack obviously isn't enough and I Think it is trying to use npm/node inside of NX and in the docker build that isn't available.

Suggested Implementation

I think that one could allow bun to be specified as the package manager similar to how npm, yarn, etc are specified. And ensure that NX can build properly in Docker and make it so I do not have to hack in using bun/bunx everywhere.

Alternate Implementations

I am not sure. If you wanted to you could just figure out why NX crashes during the docker and I can then use this bun hack in production fully.

@ffMathy
Copy link

ffMathy commented Jan 10, 2024

Amazing work! I really hope this gets accepted. My gut tells me that Bun isn't just hype.

When Yarn came out, it was also hyped. And that hype made NPM almost as fast as Yarn as a response.

But this time, there doesn't seem to have been a response from the Node team. It seems like they don't want to do a lot about the performance in comparison to what Bun is doing.

Windows support (as of today) is 33% done (33% of tests are passing according to the Bun X profile, for Windows).

I honestly think that NX is amazing. But if Bun doesn't get supported within the next year or so, I think we'll switch to a monorepo tech that supports it.

@Jordan-Hall
Copy link
Contributor

Have you validated the output? They have been issues where the output didn't match what npm and node was doing. Sometimes it claimed it ran without it actually running. I have a benchmark between the two with full nx bun support. It wasn't until we validate the output did I notice it was incorrect.

@gunta
Copy link

gunta commented Jan 11, 2024

Amazing!
If any issue arises on Bun side we should report them.
The Bun team has been quite responsive.

@bhouston
Copy link
Author

@gunta - There is a crash when creating the Docker, basically an undefined inside of nx when I try to build the targets:

Screenshot 2024-01-11 at 9 12 03 AM

Full details here: https://github.com/bhouston/template-typescript-monorepo/actions/runs/7476266475/job/20346266765

@bhouston
Copy link
Author

@Jordan-Hall wrote:

Have you validated the output?

The template project I have linked to multiple times definitely is working well, even though it compiles in 30% less time.

My production app, which I cannot share, has hundreds of unit tests and they are passing with bun, even though the compile time is 4x faster. And the app appears to work as well when using it. So if there are differences they are small enough that I have not yet noticed them.

@bhouston
Copy link
Author

@ffMathy wrote:

But this time, there doesn't seem to have been a response from the Node team. It seems like they don't want to do a lot about the performance in comparison to what Bun is doing.

Node tends to respond but very slowly.

I remember when IO.js project arrived and it caused change in the Node.JS community.

Yarn pushed Node/NPM to change as well, but it took like 4 years.

Node/NPM is still not dealing with mono repositories properly. Even though mono repositories are the main way to work for the last 5 years.

Maybe Bun causes Node to change here. One of the major differences is that Bun uses the Safari JavaScript engine, JSC, where as Node uses the Chrome JavaScript engine V8. That is a major change and I am not sure Node is willing to make that change and it would break backwards compatibility at the low level. Maybe Chrome somehow adopts better JS execution technology in their JS engine? That could also close the gap.

@bhouston
Copy link
Author

@Jordan-Hall wrote:

I have a benchmark between the two with full nx bun support. It wasn't until we validate the output did I notice it was incorrect.

Can you share it? Are you sure that it wasn't another bug? I've found that bun works properly. Now, I am not using bun's typescript compiler/transpiler, I am just using it as a runtime replacement for node.js and npm. I found that because Bun is just faster, this is enough for me.

@Jordan-Hall
Copy link
Contributor

Jordan-Hall commented Jan 11, 2024

It was a while ago I'll update repo and test it. I'm using nx-bun plugin I wrote but the issue was found to be related to bun install. It wasn't doing soemthing correctly when installing but not crashing. The bun team was aware because we was all shocked but the result

https://github.com/Jordan-Hall/large-ts-monorepo-node-bun

@AgentEnder AgentEnder added the scope: core core nx functionality label Jan 28, 2024
@wall-street-dev
Copy link

.@AgentEnder I don't mean to troll, but why are you guys fighting the tide?
This is the coolest s*** in the Universe right now!

@Jordan-Hall
Copy link
Contributor

Jordan-Hall commented Feb 5, 2024

#19113

Happy to bring this back but again I personally think bun belongs to Nx labs atm like deno use to be, and thats more use at own risk. @AgentEnder would that be better? Plus with plugin package maybe allow plugins to handle package install and lock checking etc like it would be if manually added

@Jordan-Hall
Copy link
Contributor

Jordan-Hall commented Feb 5, 2024

.@AgentEnder I don't mean to troll, but why are you guys fighting the tide? This is the coolest s*** in the Universe right now!

Bun support for windows is delayed. Many issues still outstanding, I also think it's still more of a hype as a runtime. Maybe not as a package manager but a full install including a runtime is kinda bad image :/ my 2 cent

@ludicast
Copy link

ludicast commented Feb 6, 2024

.@AgentEnder I don't mean to troll, but why are you guys fighting the tide? This is the coolest s*** in the Universe right now!

Unless launchweek has some bun announcement you really are putting Nrwl on the wrong side of history here @AgentEnder.

Bun support for windows is delayed. Many issues still outstanding, I also think it's still more of a hype as a runtime. Maybe not as a package manager but a full install including a runtime is kinda bad image :/ my 2 cent

If Nrwl is waiting for "inbox 0" on bun's issue count, this perfectionism will make Nx an also-ran (and that perfectionism includes waiting for windows support).

@Jordan-Hall
Copy link
Contributor

Waiting for windows is must and it's not zero. They many many bugs thats stops it being used in production. Look at comment above about the large repo

@ludicast
Copy link

ludicast commented Feb 6, 2024

Waiting for windows is must and it's not zero.

Eh I agree to disagree whether windows is a must for an asterisked/labs release. People stuck on windows have wsl, docker etc if they are desperate for bun, nobody is saying bun should be a default in order for its usage to start.

They many many bugs thats stops it being used in production.

People ideally rebundle things for prod anyways, rather than e.g. deploying react with nestjs+storybook+jest code (same for nest). Or for workers and lambdas you might be shipping minimized js and that's it.

As long as the compile output is sane/legit js ✔️ and the bun side of build tooling works well ✔️ , then the prod deployment is a separate concern imo.

@Jordan-Hall
Copy link
Contributor

As long as the compile output is sane/legit js ✔️ and the bun side of build tooling works well ✔️ , then the prod deployment is a separate concern imo.

This isn't always the case though! They not outputting the same! but also no error on the output!

@ludicast
Copy link

ludicast commented Feb 6, 2024

As long as the compile output is sane/legit js ✔️ and the bun side of build tooling works well ✔️ , then the prod deployment is a separate concern imo.

This isn't always the case though! They not outputting the same! but also no error on the output!

don't doubt it's happened plenty of times. and in retrospect would kinda undermine my point re the build experience.

failure-to-build hasn't hit me badly enough, and earlier on for production bunx esbuild gave me the speed/stability I needed (unimplemented node apis in that case) for those things. but silence/inconsistency is bad, yeah.

bun def shouldn't be the golden path yet, but nobody wanting to use it thinks it's perfect. just that it's often perfect for their cases.

I'd also emphasize that a bun monorepo wo/nx is often a nicer experience then an nx monorepo wo/bun and that's a major thing to consider (depending on size of repo of course).

@bhouston
Copy link
Author

bhouston commented Feb 6, 2024

@AgentEnder FYI, we (Threekit) have contracted with your company formally to help migrate a large repository to NX. That migration doesn't yet include bun, but we know from our tests that adopting bun would lead to significant build speed increases.

@Jordan-Hall
Copy link
Contributor

@AgentEnder FYI, we (Threekit) have contracted with your company formally to help migrate a large repository to NX. That migration doesn't yet include bun, but we know from our tests that adopting bun would lead to significant build speed increases.

Worker and worker_threads are unreliable in Bun right now. They also issues around not finding packages which could be related to this.

@Jordan-Hall
Copy link
Contributor

Just going to log a couple of mores found with bun thay could leads to issues on NX side and not bun.

Duplicated exports (this isn't just an issue with circular dependencies) oven-sh/bun#7521
Relative paths in browser build oven-sh/bun#6553 this doesn't actually link to sourcemaps seem to break in general.
Bundling also doesn't seem to bring in node modules correctly unless it's hacked by code spliting

@Jordan-Hall
Copy link
Contributor

@FrozenPandaz I'm happy to get bun support in once #22602 been merged. We could have a bun package however most of node will work in bun but node wont work in bun. I believe it best if a runtime package is added?

Also bun test is easy to add once its been decided on the approach for bun

jaysoo pushed a commit that referenced this issue May 22, 2024
Bun uses yarn lock for it's binary file. Running the binary will produce
the content of a yarn lock file (v1)

Other option is to use the -y command on add and install. This will
create a yarn lock file and then createLockFile can just modify the
yarn.lock file instead?

This is the PR made from #19113 and pushed due to #22402 being closed.

PS Bun feels more stable since the PR was first created!

This PR will resolve #22283 and start of #21075
Copy link

This issue has been automatically marked as stale because it hasn't had any activity for 6 months.
Many things may have changed within this time. The issue may have already been fixed or it may not be relevant anymore.
If at this point, this is still an issue, please respond with updated information.
It will be closed in 21 days if no further activity occurs.
Thanks for being a part of the Nx community! 🙏

@github-actions github-actions bot added the stale label Nov 10, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants