From fbddd5a2ebfb11dd376a751e9fc4cf09a6286ada Mon Sep 17 00:00:00 2001 From: Nathan Whitaker <17734409+nathanwhit@users.noreply.github.com> Date: Fri, 27 Sep 2024 12:35:37 -0700 Subject: fix(node): Pass NPM_PROCESS_STATE to subprocesses via temp file instead of env var (#25896) Fixes https://github.com/denoland/deno/issues/25401. Fixes https://github.com/denoland/deno/issues/25841. Fixes https://github.com/denoland/deno/issues/25891. --- .../managed/resolvers/common/lifecycle_scripts.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'cli/npm/managed/resolvers/common/lifecycle_scripts.rs') diff --git a/cli/npm/managed/resolvers/common/lifecycle_scripts.rs b/cli/npm/managed/resolvers/common/lifecycle_scripts.rs index f700d4bf9..b358c3585 100644 --- a/cli/npm/managed/resolvers/common/lifecycle_scripts.rs +++ b/cli/npm/managed/resolvers/common/lifecycle_scripts.rs @@ -2,7 +2,9 @@ use super::bin_entries::BinEntries; use crate::args::LifecycleScriptsConfig; +use deno_core::anyhow::Context; use deno_npm::resolution::NpmResolutionSnapshot; +use deno_runtime::deno_io::FromRawIoHandle; use deno_semver::package::PackageNv; use deno_semver::Version; use std::borrow::Cow; @@ -163,9 +165,24 @@ impl<'a> LifecycleScripts<'a> { ); let mut env_vars = crate::task_runner::real_env_vars(); + // we want to pass the current state of npm resolution down to the deno subprocess + // (that may be running as part of the script). we do this with an inherited temp file + // + // SAFETY: we are sharing a single temp file across all of the scripts. the file position + // will be shared among these, which is okay since we run only one script at a time. + // However, if we concurrently run scripts in the future we will + // have to have multiple temp files. + let temp_file_fd = + deno_runtime::ops::process::npm_process_state_tempfile( + process_state.as_bytes(), + ).context("failed to create npm process state tempfile for running lifecycle scripts")?; + // SAFETY: fd/handle is valid + let _temp_file = + unsafe { std::fs::File::from_raw_io_handle(temp_file_fd) }; // make sure the file gets closed env_vars.insert( - crate::args::NPM_RESOLUTION_STATE_ENV_VAR_NAME.to_string(), - process_state, + deno_runtime::ops::process::NPM_RESOLUTION_STATE_FD_ENV_VAR_NAME + .to_string(), + (temp_file_fd as usize).to_string(), ); for (package, package_path) in self.packages_with_scripts { // add custom commands for binaries from the package's dependencies. this will take precedence over the -- cgit v1.2.3