diff --git a/src/runtime/api/cron.rs b/src/runtime/api/cron.rs index 35530597e00b..045a6c96771a 100644 --- a/src/runtime/api/cron.rs +++ b/src/runtime/api/cron.rs @@ -833,9 +833,9 @@ pub(crate) fn cron_register(global: &JSGlobalObject, frame: &CallFrame) -> JsRes } } - let bun_exe = match bun_core::self_exe_path() { - Ok(p) => p, - Err(_) => { + let bun_exe = match crate::node::process::exec_path_zstr() { + Some(p) => p, + None => { return Err(global.throw(format_args!("Failed to get bun executable path"))); } }; diff --git a/src/runtime/cli/mod.rs b/src/runtime/cli/mod.rs index b10463298572..d7e7bb3a0a5a 100644 --- a/src/runtime/cli/mod.rs +++ b/src/runtime/cli/mod.rs @@ -427,6 +427,38 @@ pub(crate) static Bun__Node__RedirectWarnings: std::sync::OnceLock> = pub(crate) static Bun__Node__DisabledWarnings: std::sync::OnceLock>> = std::sync::OnceLock::new(); +#[allow(non_upper_case_globals)] +/// Overrides `process.execPath` / `process.argv[0]` / `npm_execpath`. Set +/// once, at bunx dispatch on Windows, when the process is `bunx.exe`, whose +/// path children cannot be spawned from (it dispatches to bunx CLI mode). +pub(crate) static Bun__Node__ExecPathOverride: std::sync::OnceLock = + std::sync::OnceLock::new(); + +/// When the process is `bunx.exe`, record the sibling `bun.exe` as the +/// executable path before anything captures it (`npm_execpath` in the run +/// env, `process.execPath` in the in-process fast path). A child spawned +/// from `bunx.exe` re-enters bunx CLI mode (#40298). POSIX needs no +/// override: `bunx` is a symlink and `self_exe_path` resolves through it. +#[cfg(windows)] +fn detect_bunx_exec_path_override() { + let Ok(self_exe) = bun::self_exe_path() else { + return; + }; + let bytes = self_exe.as_bytes(); + if !strings::eql_case_insensitive_ascii_check_length(bun_paths::basename(bytes), b"bunx.exe") { + return; + } + let Some(dir) = bun_paths::dirname(bytes) else { + return; + }; + let mut sibling = Vec::with_capacity(dir.len() + b"\\bun.exe".len()); + sibling.extend_from_slice(dir); + sibling.extend_from_slice(b"\\bun.exe"); + if bun_sys::exists(&sibling) { + let _ = Bun__Node__ExecPathOverride.set(bun_core::ZBox::from_vec(sibling)); + } +} + /// Backing storage for [`cli_arena`]. Written exactly once in [`Cli::start`] /// during single-threaded process startup (before `Command::start`, hence /// before any `cli_arena()` / `cli_dupe` caller), then read freely — same @@ -1528,6 +1560,8 @@ pub mod command { #[cold] #[inline(never)] fn exec_bunx(log: &mut bun_ast::Log) -> CmdResult { + #[cfg(windows)] + detect_bunx_exec_path_override(); let ctx = init(Tag::BunxCommand, log)?; let start_idx = if IS_BUNX_EXE.load(core::sync::atomic::Ordering::Relaxed) { 0 diff --git a/src/runtime/cli/run_command.rs b/src/runtime/cli/run_command.rs index efb26107ed34..42a1c570daef 100644 --- a/src/runtime/cli/run_command.rs +++ b/src/runtime/cli/run_command.rs @@ -728,10 +728,10 @@ Full documentation is available at https://bun.com/docs/cli/run if env_loader.get(b"npm_execpath").is_none() { // we don't care if this fails - if let Ok(self_exe_path) = bun_core::self_exe_path() { + if let Some(exec_path) = crate::node::process::exec_path_bytes() { env_loader .map - .put_default(b"npm_execpath", self_exe_path.as_bytes()) + .put_default(b"npm_execpath", exec_path) .expect("unreachable"); } } @@ -3998,6 +3998,48 @@ impl BunXFastPath { bun_core::scoped_log!(BUNX_FAST_PATH_LOG, "did not start via shim"); } + /// Give the in-process launch the identity a POSIX bunx child gets by + /// exec'ing through the temp `node` shim: argv in the + /// `bun