Skip to content

Refine suggestions about missing components #3453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 49 additions & 30 deletions src/dist/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,46 +43,65 @@ static TOOLCHAIN_CHANNELS: &[&str] = &[
r"\d{1}\.\d{1,3}(?:\.\d{1,2})?",
];

const TOOLSTATE_MSG: &str =
"If you require these components, please install and use the latest successful build version,\n\
which you can find at <https://rust-lang.github.io/rustup-components-history>.\n\nAfter determining \
the correct date, install it with a command such as:\n\n \
rustup toolchain install nightly-2018-12-27\n\n\
Then you can use the toolchain with commands such as:\n\n \
cargo +nightly-2018-12-27 build";

/// Returns a error message indicating that certain [`Component`]s are missing in a toolchain distribution.
///
/// This message is currently used exclusively in toolchain-wide operations,
/// otherwise [`component_unavailable_msg`](../../errors/fn.component_unavailable_msg.html) will be used.
///
/// # Panics
/// This function will panic when the collection of unavailable components `cs` is empty.
fn components_missing_msg(cs: &[Component], manifest: &ManifestV2, toolchain: &str) -> String {
assert!(!cs.is_empty());
let mut buf = vec![];
let suggestion = format!(" rustup toolchain add {toolchain} --profile minimal");
let nightly_tips = "Sometimes not all components are available in any given nightly. ";

if cs.len() == 1 {
let _ = writeln!(
buf,
"component {} is unavailable for download for channel '{}'",
&cs[0].description(manifest),
toolchain,
);
match cs {
[] => panic!("`components_missing_msg` should not be called with an empty collection of unavailable components"),
[c] => {
let _ = writeln!(
buf,
"component {} is unavailable for download for channel '{}'",
c.description(manifest),
toolchain,
);

if toolchain.starts_with("nightly") {
let _ = write!(buf, "{nightly_tips}");
}

if toolchain.starts_with("nightly") {
let _ = write!(buf, "{nightly_tips}");
let _ = write!(
buf,
"If you don't need the component, you could try a minimal installation with:\n\n{suggestion}\n\n{TOOLSTATE_MSG}"
);
}
cs => {
let cs_str = cs
.iter()
.map(|c| c.description(manifest))
.collect::<Vec<_>>()
.join(", ");
let _ = write!(
buf,
"some components unavailable for download for channel '{toolchain}': {cs_str}"
);

let _ = write!(
buf,
"If you don't need the component, you could try a minimal installation with:\n\n{suggestion}"
);
} else {
let cs_str = cs
.iter()
.map(|c| c.description(manifest))
.collect::<Vec<_>>()
.join(", ");
let _ = write!(
buf,
"some components unavailable for download for channel '{toolchain}': {cs_str}"
);
if toolchain.starts_with("nightly") {
let _ = write!(buf, "{nightly_tips}");
}

if toolchain.starts_with("nightly") {
let _ = write!(buf, "{nightly_tips}");
let _ = write!(
buf,
"If you don't need the components, you could try a minimal installation with:\n\n{suggestion}\n\n{TOOLSTATE_MSG}"
);
}
let _ = write!(
buf,
"If you don't need the components, you could try a minimal installation with:\n\n{suggestion}"
);
}

String::from_utf8(buf).unwrap()
Expand Down
44 changes: 14 additions & 30 deletions src/dist/manifestation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,25 +130,19 @@ impl Manifestation {
}

// Validate that the requested components are available
match update.unavailable_components(new_manifest, toolchain_str) {
Ok(_) => {}
Err(e) => {
if force_update {
if let Ok(RustupError::RequestedComponentsUnavailable { components, .. }) =
e.downcast::<RustupError>()
{
for component in &components {
(download_cfg.notify_handler)(
Notification::ForcingUnavailableComponent(
component.name(new_manifest).as_str(),
),
);
}
update.drop_components_to_install(&components);
}
} else {
return Err(e);
if let Err(e) = update.unavailable_components(new_manifest, toolchain_str) {
if !force_update {
return Err(e);
}
if let Ok(RustupError::RequestedComponentsUnavailable { components, .. }) =
e.downcast::<RustupError>()
{
for component in &components {
(download_cfg.notify_handler)(Notification::ForcingUnavailableComponent(
&component.name(new_manifest),
));
}
update.drop_components_to_install(&components);
}
}

Expand Down Expand Up @@ -678,18 +672,8 @@ impl Update {
}

fn drop_components_to_install(&mut self, to_drop: &[Component]) {
let components: Vec<_> = self
.components_to_install
.drain(..)
.filter(|c| !to_drop.contains(c))
.collect();
self.components_to_install.extend(components);
let final_components: Vec<_> = self
.final_component_list
.drain(..)
.filter(|c| !to_drop.contains(c))
.collect();
self.final_component_list = final_components;
self.components_to_install.retain(|c| !to_drop.contains(c));
self.final_component_list.retain(|c| !to_drop.contains(c));
}

/// Map components to urls and hashes
Expand Down
88 changes: 68 additions & 20 deletions src/dist/manifestation/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,8 +772,17 @@ fn unavailable_component() {
)
.unwrap_err();
match err.downcast::<RustupError>() {
Ok(e @ RustupError::RequestedComponentsUnavailable { .. }) => {
assert!(e.to_string().contains("rustup component remove --toolchain nightly --target x86_64-apple-darwin bonus"));
Ok(RustupError::RequestedComponentsUnavailable {
components,
manifest,
toolchain,
}) => {
assert_eq!(toolchain, "nightly");
let descriptions = components
.iter()
.map(|c| c.description(&manifest))
.collect::<Vec<_>>();
assert_eq!(descriptions, ["'bonus' for target 'x86_64-apple-darwin'"])
}
_ => panic!(),
}
Expand Down Expand Up @@ -833,8 +842,17 @@ fn unavailable_component_from_profile() {
)
.unwrap_err();
match err.downcast::<RustupError>() {
Ok(e @ RustupError::RequestedComponentsUnavailable { .. }) => {
assert!(e.to_string().contains("rustup component remove --toolchain nightly --target x86_64-apple-darwin rustc"));
Ok(RustupError::RequestedComponentsUnavailable {
components,
manifest,
toolchain,
}) => {
assert_eq!(toolchain, "nightly");
let descriptions = components
.iter()
.map(|c| c.description(&manifest))
.collect::<Vec<_>>();
assert_eq!(descriptions, ["'rustc' for target 'x86_64-apple-darwin'"])
}
_ => panic!(),
}
Expand Down Expand Up @@ -913,8 +931,17 @@ fn removed_component() {
)
.unwrap_err();
match err.downcast::<RustupError>() {
Ok(e @ RustupError::RequestedComponentsUnavailable { .. }) => {
assert!(e.to_string().contains("rustup component remove --toolchain nightly --target x86_64-apple-darwin bonus"));
Ok(RustupError::RequestedComponentsUnavailable {
components,
manifest,
toolchain,
}) => {
assert_eq!(toolchain, "nightly");
let descriptions = components
.iter()
.map(|c| c.description(&manifest))
.collect::<Vec<_>>();
assert_eq!(descriptions, ["'bonus' for target 'x86_64-apple-darwin'"])
}
_ => panic!(),
}
Expand Down Expand Up @@ -992,13 +1019,24 @@ fn unavailable_components_is_target() {
)
.unwrap_err();
match err.downcast::<RustupError>() {
Ok(e @ RustupError::RequestedComponentsUnavailable { .. }) => {
let err_str = e.to_string();
assert!(err_str
.contains("rustup target remove --toolchain nightly i686-apple-darwin"));
assert!(err_str.contains(
"rustup target remove --toolchain nightly i686-unknown-linux-gnu"
));
Ok(RustupError::RequestedComponentsUnavailable {
components,
manifest,
toolchain,
}) => {
assert_eq!(toolchain, "nightly");
let descriptions = components
.iter()
.map(|c| c.description(&manifest))
.collect::<Vec<_>>();
assert_eq!(
descriptions,
[
"'rust-std' for target 'x86_64-apple-darwin'",
"'rust-std' for target 'i686-apple-darwin'",
"'rust-std' for target 'i686-unknown-linux-gnu'"
]
);
}
_ => panic!(),
}
Expand Down Expand Up @@ -1071,13 +1109,23 @@ fn unavailable_components_with_same_target() {
)
.unwrap_err();
match err.downcast::<RustupError>() {
Ok(e @ RustupError::RequestedComponentsUnavailable { .. }) => {
let err_str = e.to_string();
assert!(err_str
.contains("rustup target remove --toolchain nightly x86_64-apple-darwin"));
assert!(err_str.contains(
"rustup component remove --toolchain nightly --target x86_64-apple-darwin rustc"
));
Ok(RustupError::RequestedComponentsUnavailable {
components,
manifest,
toolchain,
}) => {
assert_eq!(toolchain, "nightly");
let descriptions = components
.iter()
.map(|c| c.description(&manifest))
.collect::<Vec<_>>();
assert_eq!(
descriptions,
[
"'rustc' for target 'x86_64-apple-darwin'",
"'rust-std' for target 'x86_64-apple-darwin'"
]
);
}
_ => panic!(),
}
Expand Down
Loading