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

Use Bun's package manager from a built single-executable app #16262

Open
CosmoMyzrailGorynych opened this issue Jan 8, 2025 · 6 comments · May be fixed by #16829
Open

Use Bun's package manager from a built single-executable app #16262

CosmoMyzrailGorynych opened this issue Jan 8, 2025 · 6 comments · May be fixed by #16829
Labels
enhancement New feature or request

Comments

@CosmoMyzrailGorynych
Copy link

CosmoMyzrailGorynych commented Jan 8, 2025

What is the problem this feature would solve?

Ultimately I would like to use Bun's package manager in a single-executable Bun app without installing Bun globally, so my app manages npm packages automatically without additional downloads. It seems stupid to require node.js/Bun installed to install npm packages when an executable has all the needed APIs somewhere there, but not exported to Bun API. My target audience is only just partially overlaps with web developers so I would avoid making them download Bun as a dependency.

What is the feature you are proposing to solve the problem?

I would like Bun's package manager to be exposed in its bun import, maybe as a pm namespace (stands for Package Manager), or its CLI be accessible when calling a packaged single-executable app. For JS API, I would like it to have at least these methods that match their CLI versions: install, add, remove, update, pm ls. I assume this is not a hard tasks as the implementation already exists.

What alternatives have you considered?

Installing Bun/Node.js in end users' systems (and this sucks)
Having npm or an alternative as a package dependency in a Bun project 🤡

@CosmoMyzrailGorynych CosmoMyzrailGorynych added the enhancement New feature or request label Jan 8, 2025
@DonIsaac
Copy link
Contributor

DonIsaac commented Jan 8, 2025

To clarify: you want to use bun install without having Bun installed on your machine?

@pfgithub
Copy link
Contributor

pfgithub commented Jan 8, 2025

Bun is a single file, so installing it on a user's machine means downloading the right version and saving it somewhere. You don't need to have the user install bun globally, you can manage its installation yourself without too much difficulty.

@CosmoMyzrailGorynych CosmoMyzrailGorynych changed the title Programmatic usage of Bun's package manager Programmatic use of Bun's package manager Jan 9, 2025
@CosmoMyzrailGorynych
Copy link
Author

CosmoMyzrailGorynych commented Jan 9, 2025

To clarify: you want to use bun install without having Bun installed on your machine?

Well, this is quite a funny-sounding summary.
I want my Bun-baked executable app to use bun install and the most of other package manager logic without Bun installed on users' machines.

Bun is a single file, so installing it on a user's machine means downloading the right version and saving it somewhere. You don't need to have the user install bun globally, you can manage its installation yourself without too much difficulty.

If I use Bun's single-executable builder, its result is almost the same single file. Duplicating it just because Bun didn't export its package manager logic is idiotic. I would like to avoid bloating my builds.

@pfgithub
Copy link
Contributor

pfgithub commented Jan 9, 2025

Bun won't have a package management API in the near future, but bun could be modified to allow getting access to the main cli from a compiled binary

diff --git a/src/StandaloneModuleGraph.zig b/src/StandaloneModuleGraph.zig
index aae3c32a1..14b4b9fdd 100644
--- a/src/StandaloneModuleGraph.zig
+++ b/src/StandaloneModuleGraph.zig
@@ -786,6 +786,12 @@ pub const StandaloneModuleGraph = struct {
         const self_exe = openSelf() catch return null;
         defer _ = Syscall.close(self_exe);
 
+        if (bun.getenvZ("BUN_SKIP_STANDALONE_MODULE_GRAPH")) |v| {
+            if (v.len > 0 and !(v.len == 1 and v[0] == '0')) {
+                return null;
+            }
+        }
+
         var trailer_bytes: [4096]u8 = undefined;
         std.posix.lseek_END(self_exe.cast(), -4096) catch return null;

With this change, running a compiled binary with BUN_SKIP_STANDALONE_MODULE_GRAPH=1 gives access to the regular bun commands

@RiskyMH
Copy link
Member

RiskyMH commented Jan 10, 2025

With this change, running a compiled binary with BUN_SKIP_STANDALONE_MODULE_GRAPH=1 gives access to the regular bun commands

You can also be sus and name (or simlink) the executable bun or bun.exe and it also runs as normal bun :)

I don't recommend this though

@CosmoMyzrailGorynych
Copy link
Author

I don't think I would like to pick the sussy baka route; @RiskyMH would it be possible to implement the env flag in the nearest releases? This would suit my needs and be roughly in-style with runners like execa that use process.argv[0] for running node scripts.

@CosmoMyzrailGorynych CosmoMyzrailGorynych changed the title Programmatic use of Bun's package manager Use Bun's package manager CLI from a built single-executable app Jan 10, 2025
@CosmoMyzrailGorynych CosmoMyzrailGorynych changed the title Use Bun's package manager CLI from a built single-executable app Use Bun's package manager from a built single-executable app Jan 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants