Skip to content

Commit 993fe51

Browse files
committed
use metadata for command cache key spawning directly from command
1 parent cd9224f commit 993fe51

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

src/bootstrap/src/utils/exec.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ pub struct CommandCacheKey {
7777
/// [allow_failure]: BootstrapCommand::allow_failure
7878
/// [delay_failure]: BootstrapCommand::delay_failure
7979
pub struct BootstrapCommand {
80-
cache_key: CommandCacheKey,
8180
command: Command,
8281
pub failure_behavior: BehaviorOnFailure,
8382
// Run the command even during dry run
@@ -91,17 +90,9 @@ pub struct BootstrapCommand {
9190
impl<'a> BootstrapCommand {
9291
#[track_caller]
9392
pub fn new<S: AsRef<OsStr>>(program: S) -> Self {
94-
Self {
95-
should_cache: true,
96-
cache_key: CommandCacheKey {
97-
program: program.as_ref().to_os_string(),
98-
..CommandCacheKey::default()
99-
},
100-
..Command::new(program).into()
101-
}
93+
Self { should_cache: true, ..Command::new(program).into() }
10294
}
10395
pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Self {
104-
self.cache_key.args.push(arg.as_ref().to_os_string());
10596
self.command.arg(arg.as_ref());
10697
self
10798
}
@@ -126,7 +117,6 @@ impl<'a> BootstrapCommand {
126117
K: AsRef<OsStr>,
127118
V: AsRef<OsStr>,
128119
{
129-
self.cache_key.envs.push((key.as_ref().to_os_string(), val.as_ref().to_os_string()));
130120
self.command.env(key, val);
131121
self
132122
}
@@ -145,7 +135,6 @@ impl<'a> BootstrapCommand {
145135
}
146136

147137
pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut Self {
148-
self.cache_key.cwd = Some(dir.as_ref().to_path_buf());
149138
self.command.current_dir(dir);
150139
self
151140
}
@@ -239,8 +228,8 @@ impl<'a> BootstrapCommand {
239228
}
240229
}
241230

242-
pub fn cache_key(&self) -> CommandCacheKey {
243-
self.cache_key.clone()
231+
pub fn cache_key(&self) -> Option<CommandCacheKey> {
232+
(!self.should_cache).then(|| self.into())
244233
}
245234
}
246235

@@ -255,9 +244,7 @@ impl From<Command> for BootstrapCommand {
255244
#[track_caller]
256245
fn from(command: Command) -> Self {
257246
let program = command.get_program().to_owned();
258-
259247
Self {
260-
cache_key: CommandCacheKey::default(),
261248
should_cache: false,
262249
command,
263250
failure_behavior: BehaviorOnFailure::Exit,
@@ -267,6 +254,21 @@ impl From<Command> for BootstrapCommand {
267254
}
268255
}
269256

257+
impl From<&BootstrapCommand> for CommandCacheKey {
258+
fn from(value: &BootstrapCommand) -> Self {
259+
let command = &value.command;
260+
CommandCacheKey {
261+
program: command.get_program().into(),
262+
args: command.get_args().map(OsStr::to_os_string).collect(),
263+
envs: command
264+
.get_envs()
265+
.filter_map(|(k, v_opt)| v_opt.map(|v| (k.to_owned(), v.to_owned())))
266+
.collect(),
267+
cwd: command.get_current_dir().map(Path::to_path_buf),
268+
}
269+
}
270+
}
271+
270272
/// Represents the current status of `BootstrapCommand`.
271273
#[derive(Clone, PartialEq)]
272274
enum CommandStatus {

src/bootstrap/src/utils/execution_context.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,21 +148,24 @@ impl ExecutionContext {
148148
) -> CommandOutput {
149149
let cache_key = command.cache_key();
150150

151-
if command.should_cache()
152-
&& let Some(cached_output) = self.command_cache.get(&cache_key)
151+
if let Some(cached_output) = cache_key.as_ref().and_then(|key| self.command_cache.get(key))
153152
{
154153
command.mark_as_executed();
154+
155155
if self.dry_run() && !command.run_always {
156156
return CommandOutput::default();
157157
}
158+
158159
self.verbose(|| println!("Cache hit: {:?}", command));
159160
return cached_output;
160161
}
161162

162163
let output = self.start(command, stdout, stderr).wait_for_output(self);
163164

164-
if !self.dry_run() || command.run_always && command.should_cache() {
165-
self.command_cache.insert(cache_key, output.clone());
165+
if !self.dry_run() || command.run_always {
166+
if let Some(cache_key) = cache_key {
167+
self.command_cache.insert(cache_key, output.clone());
168+
}
166169
}
167170

168171
output

0 commit comments

Comments
 (0)