Skip to content

Commit 6373604

Browse files
authored
Merge pull request #216 from dtolnay/wrapper
Avoid triggering rerun-if-env-changed on RUSTC_BOOTSTRAP if possible
2 parents 7fe6b02 + 4832aa3 commit 6373604

File tree

1 file changed

+49
-6
lines changed

1 file changed

+49
-6
lines changed

src/main.rs

+49-6
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ use crate::opts::{Coloring, Expand, Subcommand};
3333
use crate::unparse::unparse_maximal;
3434
use crate::version::Version;
3535
use bat::{PagingMode, PrettyPrinter};
36-
use clap::{Parser, ValueEnum};
36+
use clap::{CommandFactory as _, Parser, ValueEnum};
3737
use quote::quote;
3838
use std::env;
3939
use std::error::Error as StdError;
40-
use std::ffi::OsString;
40+
use std::ffi::{OsStr, OsString};
4141
use std::io::{self, BufRead, IsTerminal, Write};
4242
use std::iter;
4343
use std::panic::{self, PanicInfo, UnwindSafe};
@@ -51,7 +51,12 @@ use termcolor::{Color::Green, ColorChoice, ColorSpec, StandardStream, WriteColor
5151
cargo_subcommand_metadata::description!("Show result of macro expansion");
5252

5353
fn main() {
54-
let result = cargo_expand();
54+
let result = if let Some(wrapper) = env::var_os(CARGO_EXPAND_RUSTC_WRAPPER) {
55+
do_rustc_wrapper(&wrapper)
56+
} else {
57+
do_cargo_expand()
58+
};
59+
5560
process::exit(match result {
5661
Ok(code) => code,
5762
Err(err) => {
@@ -67,11 +72,40 @@ fn main() {
6772
});
6873
}
6974

75+
const CARGO_EXPAND_RUSTC_WRAPPER: &str = "CARGO_EXPAND_RUSTC_WRAPPER";
76+
const ARG_Z_UNPRETTY_EXPANDED: &str = "-Zunpretty=expanded";
77+
7078
fn cargo_binary() -> OsString {
7179
env::var_os("CARGO").unwrap_or_else(|| "cargo".to_owned().into())
7280
}
7381

74-
fn cargo_expand() -> Result<i32> {
82+
fn do_rustc_wrapper(wrapper: &OsStr) -> Result<i32> {
83+
let mut rustc_command = env::args_os().skip(1);
84+
let mut cmd = if wrapper != "/" {
85+
Command::new(wrapper)
86+
} else if let Some(rustc) = rustc_command.next() {
87+
Command::new(rustc)
88+
} else {
89+
Subcommand::command().print_help()?;
90+
return Ok(1);
91+
};
92+
93+
let mut is_unpretty_expanded = false;
94+
for arg in rustc_command {
95+
is_unpretty_expanded |= arg == ARG_Z_UNPRETTY_EXPANDED;
96+
cmd.arg(arg);
97+
}
98+
99+
if is_unpretty_expanded {
100+
cmd.env("RUSTC_BOOTSTRAP", "1");
101+
}
102+
103+
let exit_status = cmd.status()?;
104+
let code = exit_status.code().unwrap_or(1);
105+
Ok(code)
106+
}
107+
108+
fn do_cargo_expand() -> Result<i32> {
75109
let Subcommand::Expand(args) = Subcommand::parse();
76110

77111
if args.version {
@@ -124,9 +158,18 @@ fn cargo_expand() -> Result<i32> {
124158
// Run cargo
125159
let mut cmd = Command::new(cargo_binary());
126160
apply_args(&mut cmd, &args, &color, &outfile_path);
161+
127162
if needs_rustc_bootstrap() {
128-
cmd.env("RUSTC_BOOTSTRAP", "1");
163+
if let Ok(current_exe) = env::current_exe() {
164+
let original_wrapper = env::var_os("RUSTC_WRAPPER");
165+
let wrapper = original_wrapper.as_deref().unwrap_or(OsStr::new("/"));
166+
cmd.env(CARGO_EXPAND_RUSTC_WRAPPER, wrapper);
167+
cmd.env("RUSTC_WRAPPER", current_exe);
168+
} else {
169+
cmd.env("RUSTC_BOOTSTRAP", "1");
170+
}
129171
}
172+
130173
let code = filter_err(&mut cmd)?;
131174

132175
if !outfile_path.exists() {
@@ -402,7 +445,7 @@ fn apply_args(cmd: &mut Command, args: &Expand, color: &Coloring, outfile: &Path
402445

403446
line.arg("-o");
404447
line.arg(outfile);
405-
line.arg("-Zunpretty=expanded");
448+
line.arg(ARG_Z_UNPRETTY_EXPANDED);
406449

407450
if args.verbose {
408451
let mut display = line.clone();

0 commit comments

Comments
 (0)