Skip to content

Commit

Permalink
Return std::process::ExitCode instead of exiting with std::process::exit
Browse files Browse the repository at this point in the history
rust-lang/rust#77553 still exists on nightly-2025-01-30.
  • Loading branch information
taiki-e committed Jan 30, 2025
1 parent fa9de69 commit 1ec682b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
14 changes: 7 additions & 7 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub(crate) struct Args {
}

impl Args {
pub(crate) fn parse(cargo: &OsStr) -> Result<Self> {
pub(crate) fn parse(cargo: &OsStr) -> Result<Option<Self>> {
const SUBCMD: &str = "hack";

// rustc/cargo args must be valid Unicode
Expand Down Expand Up @@ -331,15 +331,15 @@ impl Args {

Short('h') if subcommand.is_none() => {
print!("{}", Help::short());
std::process::exit(0);
return Ok(None);
}
Long("help") if subcommand.is_none() => {
print!("{}", Help::long());
std::process::exit(0);
return Ok(None);
}
Short('V') | Long("version") if subcommand.is_none() => {
println!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
std::process::exit(0);
return Ok(None);
}

// passthrough
Expand Down Expand Up @@ -523,7 +523,7 @@ impl Args {
if subcommand.is_none() {
if cargo_args.iter().any(|a| a == "--list") {
cmd!(cargo, "--list").run()?;
std::process::exit(0);
return Ok(None);
} else if !remove_dev_deps {
// TODO: improve this
mini_usage("no subcommand or valid flag specified")?;
Expand Down Expand Up @@ -600,7 +600,7 @@ impl Args {
cargo_args.push(format!("-{}", "v".repeat(verbose - 1)));
}

Ok(Self {
Ok(Some(Self {
leading_args: cargo_args,
trailing_args: rest,

Expand Down Expand Up @@ -644,7 +644,7 @@ impl Args {

no_default_features,
target: target.into_iter().collect(),
})
}))
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ pub(crate) struct Context {
}

impl Context {
pub(crate) fn new() -> Result<Self> {
let cargo = env::var_os("CARGO_HACK_CARGO_SRC")
.unwrap_or_else(|| env::var_os("CARGO").unwrap_or_else(|| OsString::from("cargo")));
let args = Args::parse(&cargo)?;
pub(crate) fn new(args: Args, cargo: OsString) -> Result<Self> {
assert!(
args.subcommand.is_some() || args.remove_dev_deps,
"no subcommand or valid flag specified"
Expand Down
13 changes: 10 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ mod version;
use std::{
collections::{BTreeMap, HashSet},
env,
ffi::OsString,
fmt::{self, Write as _},
process::ExitCode,
str::FromStr,
};

Expand All @@ -37,18 +39,23 @@ use crate::{
version::{Version, VersionRange},
};

fn main() {
fn main() -> ExitCode {
term::init_coloring();
if let Err(e) = try_main() {
error!("{e:#}");
}
if term::error() || term::warn() && env::var_os("CARGO_HACK_DENY_WARNINGS").is_some() {
std::process::exit(1)
ExitCode::FAILURE
} else {
ExitCode::SUCCESS
}
}

fn try_main() -> Result<()> {
let cx = &Context::new()?;
let cargo = env::var_os("CARGO_HACK_CARGO_SRC")
.unwrap_or_else(|| env::var_os("CARGO").unwrap_or_else(|| OsString::from("cargo")));
let Some(args) = cli::Args::parse(&cargo)? else { return Ok(()) };
let cx = &Context::new(args, cargo)?;

manifest::with(cx, || {
if cx.subcommand.is_none() {
Expand Down

0 comments on commit 1ec682b

Please sign in to comment.