From 1ec682ba188d074074b2f169b3b79bd034ebe714 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Fri, 31 Jan 2025 08:28:59 +0900 Subject: [PATCH] Return std::process::ExitCode instead of exiting with std::process::exit https://github.com/rust-lang/rust/issues/77553 still exists on nightly-2025-01-30. --- src/cli.rs | 14 +++++++------- src/context.rs | 5 +---- src/main.rs | 13 ++++++++++--- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 48e22a2..758c85c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -104,7 +104,7 @@ pub(crate) struct Args { } impl Args { - pub(crate) fn parse(cargo: &OsStr) -> Result { + pub(crate) fn parse(cargo: &OsStr) -> Result> { const SUBCMD: &str = "hack"; // rustc/cargo args must be valid Unicode @@ -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 @@ -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")?; @@ -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, @@ -644,7 +644,7 @@ impl Args { no_default_features, target: target.into_iter().collect(), - }) + })) } } diff --git a/src/context.rs b/src/context.rs index 1ed8717..0b0b856 100644 --- a/src/context.rs +++ b/src/context.rs @@ -33,10 +33,7 @@ pub(crate) struct Context { } impl Context { - pub(crate) fn new() -> Result { - 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 { assert!( args.subcommand.is_some() || args.remove_dev_deps, "no subcommand or valid flag specified" diff --git a/src/main.rs b/src/main.rs index ddeea2e..7f0fbb3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,9 @@ mod version; use std::{ collections::{BTreeMap, HashSet}, env, + ffi::OsString, fmt::{self, Write as _}, + process::ExitCode, str::FromStr, }; @@ -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() {