Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate nightly toolchain reinvoke #183

Merged
merged 1 commit into from
May 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 2 additions & 69 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use termcolor::{Color::Green, ColorChoice, ColorSpec, StandardStream, WriteColor
cargo_subcommand_metadata::description!("Show result of macro expansion");

fn main() {
let result = cargo_expand_or_run_nightly();
let result = cargo_expand();
process::exit(match result {
Ok(code) => code,
Err(err) => {
Expand All @@ -57,74 +57,6 @@ fn main() {
});
}

fn cargo_expand_or_run_nightly() -> Result<i32> {
const NO_RUN_NIGHTLY: &str = "CARGO_EXPAND_NO_RUN_NIGHTLY";

if env::var_os(NO_RUN_NIGHTLY).is_some() || maybe_nightly() || !can_rustup_run_nightly() {
return cargo_expand();
}

let mut nightly = Command::new("rustup");
nightly.arg("run");
nightly.arg("nightly");
nightly.arg("cargo");
nightly.arg("expand");

let mut args = env::args_os().peekable();
args.next().unwrap(); // cargo
if args.peek().map_or(false, |arg| arg == "expand") {
args.next().unwrap(); // expand
}
nightly.args(args);

// Hopefully prevent infinite re-run loop.
nightly.env(NO_RUN_NIGHTLY, "");

let status = nightly.status()?;

Ok(match status.code() {
Some(code) => code,
None => {
if status.success() {
0
} else {
1
}
}
})
}

fn maybe_nightly() -> bool {
!definitely_not_nightly()
}

fn definitely_not_nightly() -> bool {
let mut cmd = Command::new(cargo_binary());
cmd.arg("--version");

let output = match cmd.output() {
Ok(output) => output,
Err(_) => return false,
};

let version = match String::from_utf8(output.stdout) {
Ok(version) => version,
Err(_) => return false,
};

version.starts_with("cargo 1") && !version.contains("nightly")
}

fn can_rustup_run_nightly() -> bool {
Command::new("rustup")
.arg("run")
.arg("nightly")
.arg("cargo")
.arg("--version")
.output()
.map_or(false, |output| output.status.success())
}

fn cargo_binary() -> OsString {
env::var_os("CARGO").unwrap_or_else(|| "cargo".to_owned().into())
}
Expand Down Expand Up @@ -173,6 +105,7 @@ fn cargo_expand() -> Result<i32> {
// Run cargo
let mut cmd = Command::new(cargo_binary());
apply_args(&mut cmd, &args, &color, &outfile_path);
cmd.env("RUSTC_BOOTSTRAP", "1");
let code = filter_err(&mut cmd)?;

if !outfile_path.exists() {
Expand Down