From 924dc99426691027f2d93cb74b649dace9298354 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Thu, 13 Aug 2026 07:50:27 +0000 Subject: [PATCH 1/3] run --filter/--parallel: set npm_package_* and npm_lifecycle_* per script The multi-script runners (filter_run.rs for --filter, multi_run.rs for --parallel/--sequential) build every script's environment from one env map seeded once from the cwd package, swapping in only the per-package PATH before each spawn. Every workspace script therefore saw the root's npm_package_name/version/json, and npm_lifecycle_event, npm_lifecycle_script and npm_command were never set. Add ScriptEnv, which carries PATH plus the npm_package_* vars of the script's package and the npm_lifecycle_* vars of the script itself, and overlays them on the shared map only while building that script's envp. Both runners use it in place of the PATH-only swap, and export npm_command=run-script like plain `bun run` does. --- src/runtime/cli/filter_run.rs | 60 ++++++-------- src/runtime/cli/multi_run.rs | 53 +++++++------ src/runtime/cli/run_command.rs | 70 +++++++++++++++++ test/cli/run/filter-workspace.test.ts | 85 ++++++++++++++++++++ test/cli/run/multi-run.test.ts | 109 ++++++++++++++++++++++++++ 5 files changed, 316 insertions(+), 61 deletions(-) diff --git a/src/runtime/cli/filter_run.rs b/src/runtime/cli/filter_run.rs index e506cbd3f2f5..0153b6d043e6 100644 --- a/src/runtime/cli/filter_run.rs +++ b/src/runtime/cli/filter_run.rs @@ -8,7 +8,7 @@ use crate::api::bun::process::SpawnResultExt as _; use crate::api::bun::process::{self as spawn, Process, Rusage, SpawnOptions, Status}; use crate::cli::Command; use crate::cli::filter_arg as FilterArg; -use crate::cli::run_command::RunCommand; +use crate::cli::run_command::{RunCommand, ScriptEnv}; use bun_collections::StringHashMap; use bun_core::{Global, Output}; use bun_core::{ZStr, strings}; @@ -30,13 +30,10 @@ struct ScriptConfig { // parsed `PackageJSON` (which owns the file bytes) drops. deps: Vec>, - // $PATH must be set per script because it contains - // node_modules/.bin - // ../node_modules/.bin - // ../../node_modules/.bin - // and so forth, in addition to the user's $PATH. - #[allow(non_snake_case)] - PATH: Box<[u8]>, + /// This package's `$PATH` (its own `node_modules/.bin` chain) plus the + /// `npm_package_*` / `npm_lifecycle_*` vars for this package and script; + /// everything else comes from the env shared by the whole run. + env: ScriptEnv, elide_count: Option, } @@ -103,36 +100,22 @@ impl<'a> ProcessHandle<'a> { core::ptr::null(), ]; handle.start_time = Some(Instant::now()); - let spawned: spawn::SpawnProcessResult = 'brk: { - // Get the envp with the PATH configured - // There's probably a more optimal way to do this where you have a Vec shared - // instead of creating a new one for each process - let env_ptr = state.env; - // SAFETY: state.env is the process-lifetime DotEnv loader (Transpiler::env). - let env = unsafe { &mut *env_ptr }; - // Copy to owned — `original_path` borrows env.map which is - // mutated by put() below. - let original_path: Box<[u8]> = env.map.get(b"PATH").unwrap_or(b"").into(); - let _ = env.map.put(b"PATH", &handle.config.PATH); - // Restores PATH unconditionally at block exit (success OR error). - // Keep the guard armed for the whole block so `?` early-returns also - // restore. - scopeguard::defer! { - // SAFETY: env_ptr valid for the run loop lifetime (see above). - let _ = unsafe { (*env_ptr).map.put(b"PATH", &original_path) }; - } - // SAFETY: see above; reborrow through raw ptr to avoid overlapping &mut with guard. - let envp = unsafe { (*env_ptr).map.create_null_delimited_env_map()? }; + let spawned: spawn::SpawnProcessResult = { + // SAFETY: state.env is the process-lifetime DotEnv loader (Transpiler::env); + // this is the only borrow of it while `create_envp` runs. + let envp = handle + .config + .env + .create_envp(unsafe { &mut (*state.env).map })?; // SAFETY: `argv`/`envp` are local null-terminated C-string arrays // with argv[0] non-null; valid for this call. - break 'brk unsafe { + unsafe { spawn::spawn_process( &handle.options, argv.as_ptr(), envp.as_ptr().cast::<*const c_char>(), ) - }??; - // `_guard` drops here (or on `?` above), restoring PATH. + }?? }; #[cfg(unix)] let (stdout_fd, stderr_fd) = (spawned.stdout, spawned.stderr); @@ -328,9 +311,10 @@ struct State<'a> { shell_bin: &'static ZStr, // intentionally leaked (process exits) aborted: bool, // Raw `*mut` — process-lifetime singleton owned - // by Transpiler; ProcessHandle::start mutates `env.map` (PATH swap) so a - // shared borrow won't do, and `&'a mut` would conflict with the Transpiler's - // own raw-ptr field. Reborrow `&mut *env` at use sites. + // by Transpiler; ProcessHandle::start mutates `env.map` (per-script + // `ScriptEnv` overlay) so a shared borrow won't do, and `&'a mut` would + // conflict with the Transpiler's own raw-ptr field. Reborrow `&mut *env` + // at use sites. env: *mut bun_dotenv::Loader, } @@ -820,6 +804,11 @@ pub(crate) fn run_scripts_with_filter( let _ = RunCommand::configure_env_for_run(&mut *ctx, &mut this_transpiler, None, true, false)?; // SAFETY: configure_env_for_run fully initializes the out-param on Ok. let mut this_transpiler = unsafe { this_transpiler.assume_init() }; + // Same value `bun run