Skip to content

Commit

Permalink
early exit on unsupported subcommand and explain why
Browse files Browse the repository at this point in the history
  • Loading branch information
Emilgardis committed Oct 11, 2023
1 parent 3c21f9e commit 36ecf71
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/bin/cross.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn main() -> cross::Result<()> {

let target_list = rustc::target_list(&mut Verbosity::Quiet.into())?;
let args = cli::parse(&target_list)?;
let subcommand = args.subcommand;
let subcommand = args.subcommand.clone();
let mut msg_info = shell::MessageInfo::create(args.verbose, args.quiet, args.color.as_deref())?;
let status = match cross::run(args, target_list, &mut msg_info)? {
Some(status) => status,
Expand Down
8 changes: 4 additions & 4 deletions src/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ use crate::errors::*;
use crate::extensions::CommandExt;
use crate::shell::{self, MessageInfo};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Subcommand {
Build,
Check,
Doc,
Other,
Run,
Rustc,
Test,
Expand All @@ -21,13 +20,14 @@ pub enum Subcommand {
Metadata,
List,
Clean,
Other(String),
}

impl Subcommand {
#[must_use]
pub fn needs_docker(self, is_remote: bool) -> bool {
match self {
Subcommand::Other | Subcommand::List => false,
Subcommand::Other(_) | Subcommand::List => false,
Subcommand::Clean if !is_remote => false,
_ => true,
}
Expand Down Expand Up @@ -63,7 +63,7 @@ impl<'a> From<&'a str> for Subcommand {
"clippy" => Subcommand::Clippy,
"metadata" => Subcommand::Metadata,
"--list" => Subcommand::List,
_ => Subcommand::Other,
command => Subcommand::Other(command.to_owned()),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn group_subcommands(stdout: &str) -> (Vec<&str>, Vec<&str>) {
let first = line.split_whitespace().next();
if let Some(command) = first {
match Subcommand::from(command) {
Subcommand::Other => host.push(line),
Subcommand::Other(_) => host.push(line),
_ => cross.push(line),
}
}
Expand Down
29 changes: 25 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,13 @@ pub fn run(
))?;
}

if let Some(Subcommand::Other(command)) = &args.subcommand {
msg_info.warn(format_args!(
"specified cargo subcommand `{command}` is not supported by `cross`."
))?;
return Ok(None);
}

let host_version_meta = rustc::version_meta()?;

let cwd = std::env::current_dir()?;
Expand Down Expand Up @@ -597,6 +604,7 @@ pub fn run(

let needs_docker = args
.subcommand
.clone()
.map_or(false, |sc| sc.needs_docker(is_remote));
if target.needs_docker() && needs_docker {
let paths = docker::DockerPaths::create(
Expand All @@ -623,8 +631,14 @@ pub fn run(
&options,
msg_info,
)?;
let status = docker::run(options, paths, &filtered_args, args.subcommand, msg_info)
.wrap_err("could not run container")?;
let status = docker::run(
options,
paths,
&filtered_args,
args.subcommand.clone(),
msg_info,
)
.wrap_err("could not run container")?;
let needs_host = args.subcommand.map_or(false, |sc| sc.needs_host(is_remote));
if !status.success() {
warn_on_failure(&target, &toolchain, msg_info)?;
Expand All @@ -646,7 +660,10 @@ pub fn install_interpreter_if_needed(
options: &docker::DockerOptions,
msg_info: &mut MessageInfo,
) -> Result<(), color_eyre::Report> {
let needs_interpreter = args.subcommand.map_or(false, |sc| sc.needs_interpreter());
let needs_interpreter = args
.subcommand
.clone()
.map_or(false, |sc| sc.needs_interpreter());

if host_version_meta.needs_interpreter()
&& needs_interpreter
Expand All @@ -670,6 +687,7 @@ pub fn get_filtered_args(
let add_libc = |triple: &str| add_libc_version(triple, zig_version.as_deref());
let mut filtered_args = if args
.subcommand
.clone()
.map_or(false, |s| !s.needs_target_in_command())
{
let mut filtered_args = Vec::new();
Expand Down Expand Up @@ -710,7 +728,10 @@ pub fn get_filtered_args(
args.cargo_args.clone()
};

let is_test = args.subcommand.map_or(false, |sc| sc == Subcommand::Test);
let is_test = args
.subcommand
.clone()
.map_or(false, |sc| sc == Subcommand::Test);
if is_test && config.doctests().unwrap_or_default() && is_nightly {
filtered_args.push("-Zdoctest-xcompile".to_owned());
}
Expand Down
1 change: 1 addition & 0 deletions src/rustup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ pub fn setup_components(
}
if args
.subcommand
.clone()
.map_or(false, |sc| sc == crate::Subcommand::Clippy)
&& !component_is_installed("clippy", toolchain, msg_info)?
{
Expand Down

0 comments on commit 36ecf71

Please sign in to comment.