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

alow standalone bun executable to use local bun instance in Bun.$ #16829

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

RiskyMH
Copy link
Member

@RiskyMH RiskyMH commented Jan 28, 2025

What does this PR do?

Ensures you can always do this, even when in standalone and no bun in path:

await Bun.$`bun --revision`
await Bun.$`bunx cowsay hi`

import { fork } from "child_process";
fork("-p", ["1 + 1"]);

currently it will use same executable if no bun in path, however standalone it just infinite loops because its running itself Now it will always try to use the same bun that started it (including standalone). fixes #16262, fixes #14459, fixes #5414

uses @pfgithub's solution and extends it to be able to use in safer Bun shell

concerns: implies the whole binary will be available forever, so could be harder to minify and remove unneeded parts

  • Documentation or TypeScript types (it's okay to leave the rest blank in this case)
  • Code changes

How did you verify your code works?

will need to test with the the CI's release build

@robobun
Copy link

robobun commented Jan 28, 2025

Updated 6:11 AM PT - Feb 5th, 2025

@RiskyMH, your commit c225c12 has 5 failures in Build #11130:


🧪   try this PR locally:

bunx bun-pr 16829

@RiskyMH RiskyMH marked this pull request as ready for review January 28, 2025 08:58
@Jarred-Sumner
Copy link
Collaborator

Seems nice

Needs a test though

@RiskyMH
Copy link
Member Author

RiskyMH commented Jan 28, 2025

Done, i added tests!

Also should bun.$ (for "bun") prefer the current executable, or be right now and prefer PATH and fallback so there is something?

The issue that this is closing wants it for bun install and I was wondering if maybe the user has a Bun but really old, so the standalone now will use it and get all sorts of errors.

@CosmoMyzrailGorynych
Copy link

CosmoMyzrailGorynych commented Jan 28, 2025

The issue that this is closing wants it for bun install and I was wondering if maybe the user has a Bun but really old, so the standalone now will use it and get all sorts of errors.

My two cents,
In case of a standalone executable, the main use case of exposing bun CLI to the application is making self-contained, self-sufficient packages — useful for proprietary products or those that aim for non-techie people. So my say is that a packaged app should use itself as a bun CLI to eliminate environment inconsistencies.

@RiskyMH
Copy link
Member Author

RiskyMH commented Jan 29, 2025

Makes sense, to simplify I'll just make it always try to use the same executable (otherwise it would just be weird code to check if standalone and do one bit earlier)

and it kinda made sense the way it was written in docs to use same bun

@pfgithub
Copy link
Contributor

Node has a child_process.fork() function to call the node executable and pass it args, it would make sense for it to work in standalone executables

import { fork } from "child_process";

fork("-p", ["1 + 1"]);

In node/bun, that prints 2, but in a standalone executable it loops forever because it's launching the executable

@RiskyMH
Copy link
Member Author

RiskyMH commented Jan 31, 2025

Oh nice, I didn't know about it. Might see if it's simple to add env var there too

@yus-ham
Copy link
Contributor

yus-ham commented Jan 31, 2025

for minifying, there should be a build config for what parts to be included into executable

@RiskyMH
Copy link
Member Author

RiskyMH commented Feb 1, 2025

Node has a child_process.fork() function to call the node executable and pass it args, it would make sense for it to work in standalone executables

import { fork } from "child_process";

fork("-p", ["1 + 1"]);

In node/bun, that prints 2, but in a standalone executable it loops forever because it's launching the executable

Added support! I'm surprised this child_processes stuff is all in JS 😅

@RiskyMH
Copy link
Member Author

RiskyMH commented Feb 1, 2025

for minifying, there should be a build config for what parts to be included into executable

Yeah, I don't know the plan for that, but its way out of scope for this pr.

I'd think at least initially your suggestion of flag makes most sense (but there has to be some way of bun knowing which parts of bundle to delete - which gets harder with more interconnected stuff).

@RiskyMH
Copy link
Member Author

RiskyMH commented Feb 3, 2025

i just realized about bunx, i think this is also important to support if bun not installed (not as nice as bun x)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
6 participants