Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions crates/vite_task_plan/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ pub struct PlanContext<'a> {
cwd: Arc<AbsolutePath>,

/// The environment variables for the current execution context.
envs: FxHashMap<Arc<OsStr>, Arc<OsStr>>,
///
/// `Arc`-shared with copy-on-write semantics: [`duplicate`](Self::duplicate)
/// and downstream consumers (`ScriptCommand::envs`) share the map;
/// mutations ([`add_envs`](Self::add_envs),
/// [`prepend_path`](Self::prepend_path)) clone only when the map is
/// currently shared.
envs: Arc<FxHashMap<Arc<OsStr>, Arc<OsStr>>>,

/// The callbacks for loading task graphs and parsing commands.
callbacks: &'a mut (dyn PlanRequestParser + 'a),
Expand All @@ -54,7 +60,7 @@ impl<'a> PlanContext<'a> {
pub fn new(
workspace_path: &'a Arc<AbsolutePath>,
cwd: Arc<AbsolutePath>,
envs: FxHashMap<Arc<OsStr>, Arc<OsStr>>,
envs: Arc<FxHashMap<Arc<OsStr>, Arc<OsStr>>>,
callbacks: &'a mut (dyn PlanRequestParser + 'a),
indexed_task_graph: &'a IndexedTaskGraph,
resolved_global_cache: ResolvedGlobalCacheConfig,
Expand All @@ -73,7 +79,7 @@ impl<'a> PlanContext<'a> {
}
}

pub const fn envs(&self) -> &FxHashMap<Arc<OsStr>, Arc<OsStr>> {
pub const fn envs(&self) -> &Arc<FxHashMap<Arc<OsStr>, Arc<OsStr>>> {
&self.envs
}

Expand Down Expand Up @@ -108,15 +114,22 @@ impl<'a> PlanContext<'a> {
}

pub fn prepend_path(&mut self, path_to_prepend: &AbsolutePath) -> Result<(), JoinPathsError> {
prepend_path_env(&mut self.envs, path_to_prepend)
prepend_path_env(Arc::make_mut(&mut self.envs), path_to_prepend)
}

pub fn add_envs(
&mut self,
new_envs: impl Iterator<Item = (impl AsRef<OsStr>, impl AsRef<OsStr>)>,
) {
// Don't touch the Arc for an empty iterator — `make_mut` would clone
// the shared map for nothing (most commands have no prefix envs).
let mut new_envs = new_envs.peekable();
if new_envs.peek().is_none() {
return;
}
let envs = Arc::make_mut(&mut self.envs);
for (key, value) in new_envs {
self.envs.insert(Arc::from(key.as_ref()), Arc::from(value.as_ref()));
envs.insert(Arc::from(key.as_ref()), Arc::from(value.as_ref()));
}
}

Expand Down Expand Up @@ -154,7 +167,7 @@ impl<'a> PlanContext<'a> {
PlanContext {
workspace_path: self.workspace_path,
cwd: Arc::clone(&self.cwd),
envs: self.envs.clone(),
envs: Arc::clone(&self.envs),
callbacks: self.callbacks,
task_call_stack: self.task_call_stack.clone(),
indexed_task_graph: self.indexed_task_graph,
Expand Down
4 changes: 2 additions & 2 deletions crates/vite_task_plan/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ pub async fn plan_query(
query_plan_request: QueryPlanRequest,
workspace_path: &Arc<AbsolutePath>,
cwd: &Arc<AbsolutePath>,
envs: &FxHashMap<Arc<OsStr>, Arc<OsStr>>,
envs: &Arc<FxHashMap<Arc<OsStr>, Arc<OsStr>>>,
plan_request_parser: &mut (dyn PlanRequestParser + '_),
task_graph_loader: &mut (dyn TaskGraphLoader + '_),
) -> Result<PlanResult, Error> {
Expand All @@ -216,7 +216,7 @@ pub async fn plan_query(
let context = PlanContext::new(
workspace_path,
Arc::clone(cwd),
envs.clone(),
Arc::clone(envs),
plan_request_parser,
indexed_task_graph,
resolved_global_cache,
Expand Down
10 changes: 6 additions & 4 deletions crates/vite_task_plan/src/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ async fn plan_task_as_execution_node(
};

// Try to parse the args of an and_item to a plan request like `run -r build`
let envs: Arc<FxHashMap<Arc<OsStr>, Arc<OsStr>>> = context.envs().clone().into();
let envs = Arc::clone(context.envs());
let mut script_command = ScriptCommand {
program: and_item.program.clone(),
args: args.into(),
Expand Down Expand Up @@ -597,12 +597,14 @@ fn plan_spawn_execution(
execution_cache_key: Option<ExecutionCacheKey>,
prefix_envs: &BTreeMap<Str, Str>,
resolved_task_options: &ResolvedTaskOptions,
envs: &FxHashMap<Arc<OsStr>, Arc<OsStr>>,
envs: &Arc<FxHashMap<Arc<OsStr>, Arc<OsStr>>>,
program_path: Arc<AbsolutePath>,
args: Arc<[Str]>,
) -> Result<SpawnExecution, Error> {
// all envs available in the current context
let mut all_envs = envs.clone();
// The child env starts from the full context and is filtered in place by
// `EnvFingerprints::resolve` below — this clone is the one place the map's
// contents are actually copied per spawn.
let mut all_envs = (**envs).clone();
Comment thread
fengmk2 marked this conversation as resolved.
let cwd = Arc::clone(&resolved_task_options.cwd);

let mut resolved_cache_metadata = None;
Expand Down
Loading