-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathspawn.rs
More file actions
221 lines (192 loc) · 7.32 KB
/
spawn.rs
File metadata and controls
221 lines (192 loc) · 7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//! Process spawning with file system tracking via fspy.
use std::{
collections::hash_map::Entry,
process::{ExitStatus, Stdio},
time::{Duration, Instant},
};
use bincode::{Decode, Encode};
use bstr::BString;
use fspy::AccessMode;
use serde::Serialize;
use tokio::io::AsyncReadExt as _;
use vite_path::{AbsolutePath, RelativePathBuf};
use vite_task_plan::SpawnCommand;
use crate::collections::HashMap;
/// Path read access info
#[derive(Debug, Clone, Copy)]
pub struct PathRead {
pub read_dir_entries: bool,
}
/// Path write access info
#[derive(Debug, Clone, Copy)]
pub struct PathWrite;
/// Output kind for stdout/stderr
#[derive(Debug, PartialEq, Eq, Clone, Copy, Encode, Decode, Serialize)]
pub enum OutputKind {
StdOut,
StdErr,
}
/// Output chunk with stream kind
#[derive(Debug, Encode, Decode, Serialize, Clone)]
pub struct StdOutput {
pub kind: OutputKind,
pub content: Vec<u8>,
}
/// Result of spawning a process with file tracking
#[derive(Debug)]
pub struct SpawnResult {
pub exit_status: ExitStatus,
pub duration: Duration,
}
/// Tracking result from a spawned process for caching
#[derive(Default, Debug)]
pub struct SpawnTrackResult {
/// captured stdout/stderr
pub std_outputs: Vec<StdOutput>,
/// Tracked path reads
pub path_reads: HashMap<RelativePathBuf, PathRead>,
/// Tracked path writes
pub path_writes: HashMap<RelativePathBuf, PathWrite>,
}
/// Spawn a command with file system tracking via fspy.
///
/// Returns the execution result including captured outputs, exit status,
/// and tracked file accesses.
///
/// - `on_output` is called in real-time as stdout/stderr data arrives.
/// - `track_result` if provided, will be populated with captured outputs and path accesses for caching. If `None`, tracking is disabled.
pub async fn spawn_with_tracking<F>(
spawn_command: &SpawnCommand,
workspace_root: &AbsolutePath,
mut on_output: F,
track_result: Option<&mut SpawnTrackResult>,
) -> anyhow::Result<SpawnResult>
where
F: FnMut(OutputKind, BString),
{
let mut cmd = fspy::Command::new(spawn_command.program_path.as_path());
cmd.args(spawn_command.args.iter().map(|arg| arg.as_str()));
cmd.envs(spawn_command.all_envs.iter());
cmd.current_dir(&*spawn_command.cwd);
cmd.stdout(Stdio::piped()).stderr(Stdio::piped());
/// The tracking state of the spawned process
enum TrackingState<'a> {
/// Tacking is enabled, with the tracked child and result reference
Enabled(fspy::TrackedChild, &'a mut SpawnTrackResult),
/// Tracking is disabled, with the tokio child process
Disabled(tokio::process::Child),
}
let mut tracking_state = if let Some(track_result) = track_result {
// track_result is Some. Spawn with tracking enabled
TrackingState::Enabled(cmd.spawn().await?, track_result)
} else {
// Spawn without tracking
TrackingState::Disabled(cmd.into_tokio_command().spawn()?)
};
let mut child_stdout = match &mut tracking_state {
TrackingState::Enabled(tracked_child, _) => tracked_child.stdout.take().unwrap(),
TrackingState::Disabled(tokio_child) => tokio_child.stdout.take().unwrap(),
};
let mut child_stderr = match &mut tracking_state {
TrackingState::Enabled(tracked_child, _) => tracked_child.stderr.take().unwrap(),
TrackingState::Disabled(tokio_child) => tokio_child.stderr.take().unwrap(),
};
let mut outputs = match &mut tracking_state {
TrackingState::Enabled(_, track_result) => Some(&mut track_result.std_outputs),
TrackingState::Disabled(_) => None,
};
let mut stdout_buf = [0u8; 8192];
let mut stderr_buf = [0u8; 8192];
let mut stdout_done = false;
let mut stderr_done = false;
let start = Instant::now();
// Helper closure to process output chunks
let mut process_output = |kind: OutputKind, content: Vec<u8>| {
// Emit event immediately
on_output(kind, content.clone().into());
// Store outputs for caching
if let Some(outputs) = &mut outputs {
// Merge consecutive outputs of the same kind for caching
if let Some(last) = outputs.last_mut()
&& last.kind == kind
{
last.content.extend(&content);
} else {
outputs.push(StdOutput { kind, content });
}
}
};
// Read from both stdout and stderr concurrently using select!
loop {
tokio::select! {
result = child_stdout.read(&mut stdout_buf), if !stdout_done => {
match result? {
0 => stdout_done = true,
n => process_output(OutputKind::StdOut, stdout_buf[..n].to_vec()),
}
}
result = child_stderr.read(&mut stderr_buf), if !stderr_done => {
match result? {
0 => stderr_done = true,
n => process_output(OutputKind::StdErr, stderr_buf[..n].to_vec()),
}
}
else => break,
}
}
let (termination, track_result) = match tracking_state {
TrackingState::Enabled(tracked_child, track_result) => {
(tracked_child.wait_handle.await?, track_result)
}
TrackingState::Disabled(mut tokio_child) => {
return Ok(SpawnResult {
exit_status: tokio_child.wait().await?,
duration: start.elapsed(),
});
}
};
let duration = start.elapsed();
// Process path accesses
let path_reads = &mut track_result.path_reads;
let path_writes = &mut track_result.path_writes;
for access in termination.path_accesses.iter() {
let relative_path = access.path.strip_path_prefix(workspace_root, |strip_result| {
let Ok(stripped_path) = strip_result else {
return None;
};
// On Windows, paths are possible to be still absolute after stripping the workspace root.
// For example: c:\workspace\subdir\c:\workspace\subdir
// Just ignore those accesses.
RelativePathBuf::new(stripped_path).ok()
});
let Some(relative_path) = relative_path else {
// Ignore accesses outside the workspace
continue;
};
// Skip .git directory accesses (workaround for tools like oxlint)
if relative_path.as_path().strip_prefix(".git").is_ok() {
continue;
}
if access.mode.contains(AccessMode::READ) {
path_reads.entry(relative_path.clone()).or_insert(PathRead { read_dir_entries: false });
}
if access.mode.contains(AccessMode::WRITE) {
path_writes.insert(relative_path.clone(), PathWrite);
}
if access.mode.contains(AccessMode::READ_DIR) {
match path_reads.entry(relative_path) {
Entry::Occupied(mut occupied) => occupied.get_mut().read_dir_entries = true,
Entry::Vacant(vacant) => {
vacant.insert(PathRead { read_dir_entries: true });
}
}
}
}
tracing::debug!(
"spawn finished, path_reads: {}, path_writes: {}, exit_status: {}",
path_reads.len(),
path_writes.len(),
termination.status,
);
Ok(SpawnResult { exit_status: termination.status, duration })
}