Skip to content

Commit

Permalink
Merge #1193
Browse files Browse the repository at this point in the history
1193: suggest reinstalling the toolchain r=Emilgardis a=Emilgardis

also fixes suggestion when using wrong channel

resolves #1192 

Co-authored-by: Emil Gardström <[email protected]>
  • Loading branch information
bors[bot] and Emilgardis authored Mar 24, 2023
2 parents 202013b + 6219c40 commit 032bbdc
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
14 changes: 7 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,10 +553,7 @@ pub fn run(
.with_suggestion(|| {
format!(
"try `cross +{}` instead",
Toolchain {
host: None,
..picked_toolchain
}
picked_toolchain.remove_host()
)
}).with_section(|| format!(
r#"Overriding the toolchain in cross is only possible in CLI by specifying a channel and optional date: `+channel[-YYYY-MM-DD]`.
Expand All @@ -574,7 +571,8 @@ To override the toolchain mounted in the image, set `target.{}.image.toolchain =
let image = image.to_definite_with(&engine, msg_info);

toolchain.replace_host(&image.platform);
let maybe_warn = matches!(toolchain.channel.as_str(), "stable" | "beta" | "nightly");
let picked_generic_channel =
matches!(toolchain.channel.as_str(), "stable" | "beta" | "nightly");

if image.platform.target.is_supported(Some(&target)) {
if image.platform.architecture != toolchain.host().architecture {
Expand All @@ -593,7 +591,9 @@ To override the toolchain mounted in the image, set `target.{}.image.toolchain =
rustup::install_toolchain(&toolchain, msg_info)?;
}
let available_targets = if !toolchain.is_custom {
rustup::available_targets(&toolchain.full, msg_info)?
rustup::available_targets(&toolchain.full, msg_info).with_note(|| {
format!("cross would use the toolchain '{toolchain}' for mounting rust")
})?
} else {
rustup::AvailableTargets {
default: String::new(),
Expand All @@ -604,7 +604,7 @@ To override the toolchain mounted in the image, set `target.{}.image.toolchain =

let mut rustc_version = None;
if let Some((version, channel, commit)) = toolchain.rustc_version()? {
if maybe_warn && toolchain.date.is_none() {
if picked_generic_channel && toolchain.date.is_none() {
warn_host_version_mismatch(
&host_version_meta,
&toolchain,
Expand Down
13 changes: 13 additions & 0 deletions src/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,19 @@ pub struct Toolchain {
pub full: String,
}

impl Toolchain {
pub fn remove_host(&self) -> Self {
let mut new = Self {
host: None,
..self.clone()
};
if let Some(host) = &self.host {
new.full = new.full.replace(&format!("-{host}"), "");
}
new
}
}

impl std::fmt::Display for Toolchain {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.full)
Expand Down
22 changes: 14 additions & 8 deletions src/rustup.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::path::PathBuf;
use std::process::Command;

use color_eyre::owo_colors::OwoColorize;
use color_eyre::SectionExt;
use rustc_version::{Channel, Version};

use crate::errors::*;
Expand Down Expand Up @@ -93,14 +91,22 @@ pub fn available_targets(
.suggestion("is rustup installed?")?;

if !output.status.success() {
if String::from_utf8_lossy(&output.stderr).contains("is a custom toolchain") {
return Err(eyre::eyre!("`{toolchain}` is a custom toolchain.").with_section(|| r#"To use this toolchain with cross, you'll need to set the environment variable `CROSS_CUSTOM_TOOLCHAIN=1`
cross will not attempt to configure the toolchain further so that it can run your binary."#.header("Suggestion".bright_cyan())));
}
return Err(cmd
let mut err = cmd
.status_result(msg_info, output.status, Some(&output))
.expect_err("we know the command failed")
.to_section_report());
.to_section_report();
if String::from_utf8_lossy(&output.stderr).contains("is a custom toolchain") {
err = err.wrap_err("'{toolchain}' is a custom toolchain.")
.suggestion(r#"To use this toolchain with cross, you'll need to set the environment variable `CROSS_CUSTOM_TOOLCHAIN=1`
cross will not attempt to configure the toolchain further so that it can run your binary."#);
} else if String::from_utf8_lossy(&output.stderr).contains("does not support components") {
err = err.suggestion(format!(
"try reinstalling the '{toolchain}' toolchain
$ rustup toolchain uninstall {toolchain}
$ rustup toolchain install {toolchain} --force-non-host"
));
}
return Err(err);
}
let out = output.stdout()?;
let mut default = String::new();
Expand Down

0 comments on commit 032bbdc

Please sign in to comment.