Make command field of BootstrapCommand private to force access to it through the as_command_mut method

This will be useful for disarming drop bombs when the inner command is accessed.
This commit is contained in:
Jakub Beránek 2024-07-04 16:13:33 +02:00 committed by Jakub Beránek
parent 042473fd13
commit a1626d709c
No known key found for this signature in database
GPG key ID: 909CD0D26483516B
14 changed files with 60 additions and 42 deletions

View file

@ -2080,7 +2080,8 @@ pub fn stream_cargo(
tail_args: Vec<String>,
cb: &mut dyn FnMut(CargoMessage<'_>),
) -> bool {
let mut cargo = cargo.into_cmd().command;
let mut cmd = cargo.into_cmd();
let cargo = cmd.as_command_mut();
// Instruct Cargo to give us json messages on stdout, critically leaving
// stderr as piped so we can get those pretty colors.
let mut message_format = if builder.config.json_output {

View file

@ -172,7 +172,7 @@ pub(crate) fn detect_llvm_sha(config: &Config, is_git: bool) -> String {
// the LLVM shared object file is named `LLVM-12-rust-{version}-nightly`
config.src.join("src/version"),
]);
output(&mut rev_list.command).trim().to_owned()
output(rev_list.as_command_mut()).trim().to_owned()
} else if let Some(info) = channel::read_commit_info_file(&config.src) {
info.sha.trim().to_owned()
} else {
@ -254,7 +254,7 @@ pub(crate) fn is_ci_llvm_modified(config: &Config) -> bool {
// `true` here.
let llvm_sha = detect_llvm_sha(config, true);
let head_sha =
output(&mut helpers::git(Some(&config.src)).arg("rev-parse").arg("HEAD").command);
output(helpers::git(Some(&config.src)).arg("rev-parse").arg("HEAD").as_command_mut());
let head_sha = head_sha.trim();
llvm_sha == head_sha
}

View file

@ -484,7 +484,7 @@ impl Step for Hook {
fn install_git_hook_maybe(config: &Config) -> io::Result<()> {
let git = helpers::git(Some(&config.src))
.args(["rev-parse", "--git-common-dir"])
.command
.as_command_mut()
.output()
.map(|output| {
assert!(output.status.success(), "failed to run `git`");

View file

@ -2105,7 +2105,7 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the
cmd.arg("--nightly-branch").arg(git_config.nightly_branch);
// FIXME: Move CiEnv back to bootstrap, it is only used here anyway
builder.ci_env.force_coloring_in_ci(&mut cmd.command);
builder.ci_env.force_coloring_in_ci(cmd.as_command_mut());
#[cfg(feature = "build-metrics")]
builder.metrics.begin_test_suite(

View file

@ -106,7 +106,7 @@ fn check_changed_files(toolstates: &HashMap<Box<str>, ToolState>) {
.arg("--name-status")
.arg("HEAD")
.arg("HEAD^")
.command
.as_command_mut()
.output();
let output = match output {
Ok(o) => o,
@ -329,7 +329,7 @@ fn checkout_toolstate_repo() {
.arg("--depth=1")
.arg(toolstate_repo())
.arg(TOOLSTATE_DIR)
.command
.as_command_mut()
.status();
let success = match status {
Ok(s) => s.success(),
@ -343,8 +343,13 @@ fn checkout_toolstate_repo() {
/// Sets up config and authentication for modifying the toolstate repo.
fn prepare_toolstate_config(token: &str) {
fn git_config(key: &str, value: &str) {
let status =
helpers::git(None).arg("config").arg("--global").arg(key).arg(value).command.status();
let status = helpers::git(None)
.arg("config")
.arg("--global")
.arg(key)
.arg(value)
.as_command_mut()
.status();
let success = match status {
Ok(s) => s.success(),
Err(_) => false,
@ -413,7 +418,7 @@ fn commit_toolstate_change(current_toolstate: &ToolstateData) {
.arg("-a")
.arg("-m")
.arg(&message)
.command
.as_command_mut()
.status());
if !status.success() {
success = true;
@ -424,7 +429,7 @@ fn commit_toolstate_change(current_toolstate: &ToolstateData) {
.arg("push")
.arg("origin")
.arg("master")
.command
.as_command_mut()
.status());
// If we successfully push, exit.
if status.success() {
@ -437,14 +442,14 @@ fn commit_toolstate_change(current_toolstate: &ToolstateData) {
.arg("fetch")
.arg("origin")
.arg("master")
.command
.as_command_mut()
.status());
assert!(status.success());
let status = t!(helpers::git(Some(Path::new(TOOLSTATE_DIR)))
.arg("reset")
.arg("--hard")
.arg("origin/master")
.command
.as_command_mut()
.status());
assert!(status.success());
}
@ -460,7 +465,7 @@ fn commit_toolstate_change(current_toolstate: &ToolstateData) {
/// `publish_toolstate.py` script if the PR passes all tests and is merged to
/// master.
fn publish_test_results(current_toolstate: &ToolstateData) {
let commit = t!(helpers::git(None).arg("rev-parse").arg("HEAD").command.output());
let commit = t!(helpers::git(None).arg("rev-parse").arg("HEAD").as_command_mut().output());
let commit = t!(String::from_utf8(commit.stdout));
let toolstate_serialized = t!(serde_json::to_string(&current_toolstate));

View file

@ -2105,7 +2105,7 @@ impl<'a> Builder<'a> {
// Try to use a sysroot-relative bindir, in case it was configured absolutely.
cargo.env("RUSTC_INSTALL_BINDIR", self.config.bindir_relative());
self.ci_env.force_coloring_in_ci(&mut cargo.command);
self.ci_env.force_coloring_in_ci(cargo.as_command_mut());
// When we build Rust dylibs they're all intended for intermediate
// usage, so make sure we pass the -Cprefer-dynamic flag instead of

View file

@ -1259,7 +1259,7 @@ impl Config {
cmd.arg("rev-parse").arg("--show-cdup");
// Discard stderr because we expect this to fail when building from a tarball.
let output = cmd
.command
.as_command_mut()
.stderr(std::process::Stdio::null())
.output()
.ok()
@ -2163,7 +2163,7 @@ impl Config {
let mut git = helpers::git(Some(&self.src));
git.arg("show").arg(format!("{commit}:{}", file.to_str().unwrap()));
output(&mut git.command)
output(git.as_command_mut())
}
/// Bootstrap embeds a version number into the name of shared libraries it uploads in CI.
@ -2469,11 +2469,11 @@ impl Config {
// Look for a version to compare to based on the current commit.
// Only commits merged by bors will have CI artifacts.
let merge_base = output(
&mut helpers::git(Some(&self.src))
helpers::git(Some(&self.src))
.arg("rev-list")
.arg(format!("--author={}", self.stage0_metadata.config.git_merge_commit_email))
.args(["-n1", "--first-parent", "HEAD"])
.command,
.as_command_mut(),
);
let commit = merge_base.trim_end();
if commit.is_empty() {
@ -2489,7 +2489,7 @@ impl Config {
.args(["diff-index", "--quiet", commit])
.arg("--")
.args([self.src.join("compiler"), self.src.join("library")])
.command
.as_command_mut()
.status())
.success();
if has_changes {
@ -2563,11 +2563,11 @@ impl Config {
// Look for a version to compare to based on the current commit.
// Only commits merged by bors will have CI artifacts.
let merge_base = output(
&mut helpers::git(Some(&self.src))
helpers::git(Some(&self.src))
.arg("rev-list")
.arg(format!("--author={}", self.stage0_metadata.config.git_merge_commit_email))
.args(["-n1", "--first-parent", "HEAD"])
.command,
.as_command_mut(),
);
let commit = merge_base.trim_end();
if commit.is_empty() {
@ -2589,7 +2589,7 @@ impl Config {
git.arg(top_level.join(path));
}
let has_changes = !t!(git.command.status()).success();
let has_changes = !t!(git.as_command_mut().status()).success();
if has_changes {
if if_unchanged {
if self.verbose > 0 {

View file

@ -209,7 +209,7 @@ than building it.
#[cfg(not(feature = "bootstrap-self-test"))]
let stage0_supported_target_list: HashSet<String> = crate::utils::helpers::output(
&mut command(&build.config.initial_rustc).args(["--print", "target-list"]).command,
command(&build.config.initial_rustc).args(["--print", "target-list"]).as_command_mut(),
)
.lines()
.map(|s| s.to_string())

View file

@ -493,11 +493,14 @@ impl Build {
let submodule_git = || helpers::git(Some(&absolute_path));
// Determine commit checked out in submodule.
let checked_out_hash = output(&mut submodule_git().args(["rev-parse", "HEAD"]).command);
let checked_out_hash = output(submodule_git().args(["rev-parse", "HEAD"]).as_command_mut());
let checked_out_hash = checked_out_hash.trim_end();
// Determine commit that the submodule *should* have.
let recorded = output(
&mut helpers::git(Some(&self.src)).args(["ls-tree", "HEAD"]).arg(relative_path).command,
helpers::git(Some(&self.src))
.args(["ls-tree", "HEAD"])
.arg(relative_path)
.as_command_mut(),
);
let actual_hash = recorded
.split_whitespace()
@ -522,7 +525,7 @@ impl Build {
let current_branch = {
let output = helpers::git(Some(&self.src))
.args(["symbolic-ref", "--short", "HEAD"])
.command
.as_command_mut()
.stderr(Stdio::inherit())
.output();
let output = t!(output);
@ -548,7 +551,7 @@ impl Build {
git
};
// NOTE: doesn't use `try_run` because this shouldn't print an error if it fails.
if !update(true).command.status().map_or(false, |status| status.success()) {
if !update(true).as_command_mut().status().map_or(false, |status| status.success()) {
update(false).run(self);
}
@ -941,10 +944,12 @@ impl Build {
self.verbose(|| println!("running: {command:?}"));
command.command.stdout(command.stdout.stdio());
command.command.stderr(command.stderr.stdio());
let stdout = command.stdout.stdio();
command.as_command_mut().stdout(stdout);
let stderr = command.stderr.stdio();
command.as_command_mut().stderr(stderr);
let output = command.command.output();
let output = command.as_command_mut().output();
use std::fmt::Write;
@ -1931,7 +1936,7 @@ fn envify(s: &str) -> String {
pub fn generate_smart_stamp_hash(dir: &Path, additional_input: &str) -> String {
let diff = helpers::git(Some(dir))
.arg("diff")
.command
.as_command_mut()
.output()
.map(|o| String::from_utf8(o.stdout).unwrap_or_default())
.unwrap_or_default();
@ -1941,7 +1946,7 @@ pub fn generate_smart_stamp_hash(dir: &Path, additional_input: &str) -> String {
.arg("--porcelain")
.arg("-z")
.arg("--untracked-files=normal")
.command
.as_command_mut()
.output()
.map(|o| String::from_utf8(o.stdout).unwrap_or_default())
.unwrap_or_default();

View file

@ -45,7 +45,7 @@ impl GitInfo {
}
// Make sure git commands work
match helpers::git(Some(dir)).arg("rev-parse").command.output() {
match helpers::git(Some(dir)).arg("rev-parse").as_command_mut().output() {
Ok(ref out) if out.status.success() => {}
_ => return GitInfo::Absent,
}
@ -63,11 +63,12 @@ impl GitInfo {
.arg("-1")
.arg("--date=short")
.arg("--pretty=format:%cd")
.command,
.as_command_mut(),
);
let ver_hash = output(&mut helpers::git(Some(dir)).arg("rev-parse").arg("HEAD").command);
let ver_hash =
output(helpers::git(Some(dir)).arg("rev-parse").arg("HEAD").as_command_mut());
let short_ver_hash = output(
&mut helpers::git(Some(dir)).arg("rev-parse").arg("--short=9").arg("HEAD").command,
helpers::git(Some(dir)).arg("rev-parse").arg("--short=9").arg("HEAD").as_command_mut(),
);
GitInfo::Present(Some(Info {
commit_date: ver_date.trim().to_string(),

View file

@ -55,7 +55,7 @@ impl OutputMode {
/// [delay_failure]: BootstrapCommand::delay_failure
#[derive(Debug)]
pub struct BootstrapCommand {
pub command: Command,
command: Command,
pub failure_behavior: BehaviorOnFailure,
pub stdout: OutputMode,
pub stderr: OutputMode,
@ -145,6 +145,12 @@ impl BootstrapCommand {
pub fn run(&mut self, builder: &Build) -> CommandOutput {
builder.run(self)
}
/// Provides access to the stdlib Command inside.
/// All usages of this function should be eventually removed from bootstrap.
pub fn as_command_mut(&mut self) -> &mut Command {
&mut self.command
}
}
impl From<Command> for BootstrapCommand {

View file

@ -244,7 +244,7 @@ pub fn is_valid_test_suite_arg<'a, P: AsRef<Path>>(
// FIXME: get rid of this function
pub fn check_run(cmd: &mut BootstrapCommand, print_cmd_on_fail: bool) -> bool {
let status = match cmd.command.status() {
let status = match cmd.as_command_mut().status() {
Ok(status) => status,
Err(e) => {
println!("failed to execute command: {cmd:?}\nERROR: {e}");

View file

@ -50,7 +50,7 @@ pub(crate) fn try_run_tests(
}
fn run_tests(builder: &Builder<'_>, cmd: &mut BootstrapCommand, stream: bool) -> bool {
let cmd = &mut cmd.command;
let cmd = cmd.as_command_mut();
cmd.stdout(Stdio::piped());
builder.verbose(|| println!("running: {cmd:?}"));

View file

@ -370,11 +370,11 @@ impl<'a> Tarball<'a> {
if self.builder.rust_info().is_managed_git_subrepository() {
// %ct means committer date
let timestamp = helpers::output(
&mut helpers::git(Some(&self.builder.src))
helpers::git(Some(&self.builder.src))
.arg("log")
.arg("-1")
.arg("--format=%ct")
.command,
.as_command_mut(),
);
cmd.args(["--override-file-mtime", timestamp.trim()]);
}