Skip to content

Commit 542344f

Browse files
committed
Print command creation and execution location when it fails
This should make it quicker to debug command failures.
1 parent cefd5b3 commit 542344f

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

src/bootstrap/src/lib.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -937,13 +937,19 @@ impl Build {
937937

938938
/// Execute a command and return its output.
939939
/// This method should be used for all command executions in bootstrap.
940+
#[track_caller]
940941
fn run(&self, command: &mut BootstrapCommand) -> CommandOutput {
941942
command.mark_as_executed();
942943
if self.config.dry_run() && !command.run_always {
943944
return CommandOutput::default();
944945
}
945946

946-
self.verbose(|| println!("running: {command:?}"));
947+
let created_at = command.get_created_location();
948+
let executed_at = std::panic::Location::caller();
949+
950+
self.verbose(|| {
951+
println!("running: {command:?} (created at {created_at}, executed at {executed_at})")
952+
});
947953

948954
let stdout = command.stdout.stdio();
949955
command.as_command_mut().stdout(stdout);
@@ -962,8 +968,11 @@ impl Build {
962968
Ok(output) => {
963969
writeln!(
964970
message,
965-
"\n\nCommand {command:?} did not execute successfully.\
966-
\nExpected success, got: {}",
971+
r#"
972+
Command {command:?} did not execute successfully.
973+
Expected success, got {}
974+
Created at: {created_at}
975+
Executed at: {executed_at}"#,
967976
output.status,
968977
)
969978
.unwrap();

src/bootstrap/src/utils/exec.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,17 +160,23 @@ impl BootstrapCommand {
160160
&mut self.command
161161
}
162162

163-
/// Mark the command as being executd, disarming the drop bomb.
163+
/// Mark the command as being executed, disarming the drop bomb.
164164
/// If this method is not called before the command is dropped, its drop will panic.
165165
pub fn mark_as_executed(&mut self) {
166166
self.drop_bomb.defuse();
167167
}
168+
169+
/// Returns the source code location where this command was created.
170+
pub fn get_created_location(&self) -> std::panic::Location<'static> {
171+
self.drop_bomb.get_created_location()
172+
}
168173
}
169174

170175
impl From<Command> for BootstrapCommand {
171176
#[track_caller]
172177
fn from(command: Command) -> Self {
173178
let program = command.get_program().to_owned();
179+
174180
Self {
175181
command,
176182
failure_behavior: BehaviorOnFailure::Exit,

src/tools/build_helper/src/drop_bomb/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ impl DropBomb {
3131
}
3232
}
3333

34+
pub fn get_created_location(&self) -> panic::Location<'static> {
35+
self.armed_location
36+
}
37+
3438
/// Defuse the [`DropBomb`]. This will prevent the drop bomb from panicking when dropped.
3539
pub fn defuse(&mut self) {
3640
self.defused = true;

0 commit comments

Comments
 (0)