Repository navigation
Description
What version of Bun is running?
1.4.2+744846f84
What platform is your computer?
Darwin 27.0.0 arm64 (macOS 27.0.1)
What steps can reproduce the bug?
A compiled executable that embeds a .node addon extracts it to the temp dir before dlopen. If the temp dir isn't writable (for example a Kubernetes pod with readOnlyRootFilesystem: true), the load fails with an error about /$bunfs/... not existing. It does not mention the temp dir.
hello.c:
#include <node_api.h>
static napi_value Hello(napi_env env, napi_callback_info info) {
napi_value s; napi_create_string_utf8(env, "hello", NAPI_AUTO_LENGTH, &s); return s;
}
NAPI_MODULE_INIT() {
napi_value fn; napi_create_function(env, "hello", NAPI_AUTO_LENGTH, Hello, NULL, &fn);
napi_set_named_property(env, exports, "hello", fn); return exports;
}index.js:
const addon = require("./hello.node");
console.log(addon.hello());clang -shared -undefined dynamic_lookup -I/path/to/include/node -o hello.node hello.c
bun build --compile index.js --outfile app
mkdir rw rw2 ro && chmod 555 ro
TMPDIR=$PWD/rw ./app # hello
TMPDIR=$PWD/ro ./app # fails, see below
TMPDIR=$PWD/ro BUN_TMPDIR=$PWD/rw2 ./app # helloWhat is the expected behavior?
An error that names the real cause, for example:
error: failed to extract embedded native addon "hello-4xzsmah1.node" to /…/ro: EACCES (set BUN_TMPDIR to a writable directory)
What do you see instead?
7 | module.exports = __require("/$bunfs/root/hello-4xzsmah1.node");
^
error: dlopen(/$bunfs/root/hello-4xzsmah1.node, 0x0001): tried: '/$bunfs/root/hello-4xzsmah1.node' (no such file), '/System/Volumes/Preboot/Cryptexes/OS/$bunfs/root/hello-4xzsmah1.node' (no such file), '/$bunfs/root/hello-4xzsmah1.node' (no such file)
code: "ERR_DLOPEN_FAILED"
Bun v1.4.2 (macOS arm64)
Additional information
resolve_embedded_file_to_buf turns every extraction failure into None (tmpdir().ok()?, Tmpfile::create_with_mode(...).ok()?, and the write_ok branch). See src/runtime/jsc_hooks.rs:4597, :4620 and :4625-4628 at c69a636.
When that happens, process.dlopen in src/jsc/bindings/BunProcess.cpp:518-527 keeps the virtual /$bunfs/... path and passes it to the OS dlopen. The OS error is about a path that only exists inside the executable, so the actual write error is lost.
Possible fixes, in order of preference:
- Return the errno and the temp dir path from the extraction step, and throw
ERR_DLOPEN_FAILEDwith them instead of falling through todlopen("/$bunfs/..."). MentioningBUN_TMPDIRin the message would make the fix easy to find. - And/or, when the temp dir isn't writable, try a fallback location. One option is next to
process.execPathif that directory is writable. - At the least, document in the single-file executable docs that embedded
.nodefiles need a writableTMPDIR/BUN_TMPDIRat runtime.
Activity