From c66c24c01d89248c7e84510ca991327e03f7dae0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20Gardstr=C3=B6m?= Date: Thu, 5 Oct 2023 19:40:06 +0200 Subject: [PATCH] Fix non-provided targets with only dockerfile specified --- .changes/1340.json | 4 ++ src/bin/commands/run.rs | 5 +++ src/docker/shared.rs | 81 ++++++++++++++++++++++++++--------------- src/lib.rs | 5 +++ 4 files changed, 65 insertions(+), 30 deletions(-) create mode 100644 .changes/1340.json diff --git a/.changes/1340.json b/.changes/1340.json new file mode 100644 index 000000000..3c1fc6dfa --- /dev/null +++ b/.changes/1340.json @@ -0,0 +1,4 @@ +{ + "description": "don't error when a non-provided target is used with only a dockerfile specified", + "type": "fixed" +} diff --git a/src/bin/commands/run.rs b/src/bin/commands/run.rs index 08c1aad65..ed8cc755c 100644 --- a/src/bin/commands/run.rs +++ b/src/bin/commands/run.rs @@ -69,6 +69,11 @@ impl Run { let image = match docker::get_image(&config, &target, false) { Ok(i) => i, + Err(docker::GetImageError::NoCompatibleImages(..)) + if config.dockerfile(&target)?.is_some() => + { + "scratch".into() + } Err(err) => { msg_info.warn(&err)?; eyre::bail!("Error: {}", &err); diff --git a/src/docker/shared.rs b/src/docker/shared.rs index efe1e332e..994bd27f4 100644 --- a/src/docker/shared.rs +++ b/src/docker/shared.rs @@ -1237,14 +1237,33 @@ pub(crate) fn group_id() -> String { env::var("CROSS_CONTAINER_GID").unwrap_or_else(|_| id::group().to_string()) } +#[derive(Debug, thiserror::Error)] +pub enum GetImageError { + #[error( + "`cross` does not provide a Docker image for target {0}, \ + specify a custom image in `Cross.toml`." + )] + NoCompatibleImages(String), + #[error("platforms for provided image `{0}` are not specified, this is a bug in cross")] + SpecifiedImageNoPlatform(String), + #[error(transparent)] + MultipleImages(eyre::Report), + #[error(transparent)] + Other(eyre::Report), +} + /// Simpler version of [get_image] -pub fn get_image_name(config: &Config, target: &Target, uses_zig: bool) -> Result { - if let Some(image) = config.image(target)? { +pub fn get_image_name( + config: &Config, + target: &Target, + uses_zig: bool, +) -> Result { + if let Some(image) = config.image(target).map_err(GetImageError::Other)? { return Ok(image.name); } let target_name = match uses_zig { - true => match config.zig_image(target)? { + true => match config.zig_image(target).map_err(GetImageError::Other)? { Some(image) => return Ok(image.name), None => "zig", }, @@ -1256,10 +1275,7 @@ pub fn get_image_name(config: &Config, target: &Target, uses_zig: bool) -> Resul .collect::>(); if compatible.is_empty() { - eyre::bail!( - "`cross` does not provide a Docker image for target {target_name}, \ - specify a custom image in `Cross.toml`." - ); + return Err(GetImageError::NoCompatibleImages(target_name.to_owned())); } let version = if crate::commit_info().is_empty() { @@ -1274,13 +1290,17 @@ pub fn get_image_name(config: &Config, target: &Target, uses_zig: bool) -> Resul .image_name(CROSS_IMAGE, version)) } -pub fn get_image(config: &Config, target: &Target, uses_zig: bool) -> Result { - if let Some(image) = config.image(target)? { +pub fn get_image( + config: &Config, + target: &Target, + uses_zig: bool, +) -> Result { + if let Some(image) = config.image(target).map_err(GetImageError::Other)? { return Ok(image); } let target_name = match uses_zig { - true => match config.zig_image(target)? { + true => match config.zig_image(target).map_err(GetImageError::Other)? { Some(image) => return Ok(image), None => "zig", }, @@ -1292,10 +1312,7 @@ pub fn get_image(config: &Config, target: &Target, uses_zig: bool) -> Result>(); if compatible.is_empty() { - eyre::bail!( - "`cross` does not provide a Docker image for target {target_name}, \ - specify a custom image in `Cross.toml`." - ); + return Err(GetImageError::NoCompatibleImages(target_name.to_owned())); } let version = if crate::commit_info().is_empty() { @@ -1320,28 +1337,32 @@ pub fn get_image(config: &Config, target: &Target, uses_zig: bool) -> Result>() - .join(", ") ) - })); + .with_note(|| { + format!( + "candidates: {}", + compatible + .iter() + .map(|provided| format!( + "\"{}\"", + provided.image_name(CROSS_IMAGE, version) + )) + .collect::>() + .join(", ") + ) + }), + )); }; let mut image: PossibleImage = pick.image_name(CROSS_IMAGE, version).into(); - eyre::ensure!( - !pick.platforms.is_empty(), - "platforms for provided image `{image}` are not specified, this is a bug in cross" - ); + if pick.platforms.is_empty() { + return Err(GetImageError::SpecifiedImageNoPlatform(image.to_string())); + }; image.toolchain = pick.platforms.to_vec(); Ok(image) diff --git a/src/lib.rs b/src/lib.rs index 6f01be053..f39309161 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -743,6 +743,11 @@ pub fn setup( let zig_version = config.zig_version(&target)?; let image = match docker::get_image(&config, &target, uses_zig) { Ok(i) => i, + Err(docker::GetImageError::NoCompatibleImages(..)) + if config.dockerfile(&target)?.is_some() => + { + "scratch".into() + } Err(err) => { msg_info.warn(err)?;