Skip to content

Commit

Permalink
Merge #1340
Browse files Browse the repository at this point in the history
1340: Fix non-provided targets with only dockerfile specified r=Emilgardis a=Emilgardis



Co-authored-by: Emil Gardström <[email protected]>
  • Loading branch information
bors[bot] and Emilgardis authored Oct 11, 2023
2 parents 3bfc6d5 + c66c24c commit 3c21f9e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 30 deletions.
4 changes: 4 additions & 0 deletions .changes/1340.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"description": "don't error when a non-provided target is used with only a dockerfile specified",
"type": "fixed"
}
5 changes: 5 additions & 0 deletions src/bin/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
81 changes: 51 additions & 30 deletions src/docker/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
if let Some(image) = config.image(target)? {
pub fn get_image_name(
config: &Config,
target: &Target,
uses_zig: bool,
) -> Result<String, GetImageError> {
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",
},
Expand All @@ -1256,10 +1275,7 @@ pub fn get_image_name(config: &Config, target: &Target, uses_zig: bool) -> Resul
.collect::<Vec<_>>();

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() {
Expand All @@ -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<PossibleImage> {
if let Some(image) = config.image(target)? {
pub fn get_image(
config: &Config,
target: &Target,
uses_zig: bool,
) -> Result<PossibleImage, GetImageError> {
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",
},
Expand All @@ -1292,10 +1312,7 @@ pub fn get_image(config: &Config, target: &Target, uses_zig: bool) -> Result<Pos
.collect::<Vec<_>>();

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() {
Expand All @@ -1320,28 +1337,32 @@ pub fn get_image(config: &Config, target: &Target, uses_zig: bool) -> Result<Pos
.expect("should exists at least one non-sub image in list")
} else {
// if there's multiple targets and no option can be chosen, bail
return Err(eyre::eyre!(
"`cross` provides multiple images for target {target_name}, \
return Err(GetImageError::MultipleImages(
eyre::eyre!(
"`cross` provides multiple images for target {target_name}, \
specify toolchain in `Cross.toml`."
)
.with_note(|| {
format!(
"candidates: {}",
compatible
.iter()
.map(|provided| format!("\"{}\"", provided.image_name(CROSS_IMAGE, version)))
.collect::<Vec<_>>()
.join(", ")
)
}));
.with_note(|| {
format!(
"candidates: {}",
compatible
.iter()
.map(|provided| format!(
"\"{}\"",
provided.image_name(CROSS_IMAGE, version)
))
.collect::<Vec<_>>()
.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)
Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;

Expand Down

0 comments on commit 3c21f9e

Please sign in to comment.