Skip to content

fix(rustup-mode): return ExitCode(1) when update() fails #3952

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 3 commits into from
Jul 16, 2024
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
11 changes: 7 additions & 4 deletions src/cli/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ pub(crate) async fn update_all_channels(
force_update: bool,
) -> Result<utils::ExitCode> {
let toolchains = cfg.update_all_channels(force_update).await?;
let has_update_error = toolchains.iter().any(|(_, r)| r.is_err());
let mut exit_code = utils::ExitCode(if has_update_error { 1 } else { 0 });

if toolchains.is_empty() {
info!("no updatable toolchains installed");
Expand All @@ -308,14 +310,15 @@ pub(crate) async fn update_all_channels(
.collect();
show_channel_updates(cfg, t)?;
}
Ok(utils::ExitCode(0))
Ok(())
};

if do_self_update {
self_update(show_channel_updates, cfg.process).await
exit_code &= self_update(show_channel_updates, cfg.process).await?;
} else {
show_channel_updates()
show_channel_updates()?;
}
Ok(exit_code)
}

#[derive(Clone, Copy, Debug)]
Expand Down Expand Up @@ -358,7 +361,7 @@ pub(crate) fn self_update_permitted(explicit: bool) -> Result<SelfUpdatePermissi
/// Performs all of a self-update: check policy, download, apply and exit.
pub(crate) async fn self_update<F>(before_restart: F, process: &Process) -> Result<utils::ExitCode>
where
F: FnOnce() -> Result<utils::ExitCode>,
F: FnOnce() -> Result<()>,
{
match self_update_permitted(false)? {
SelfUpdatePermission::HardFail => {
Expand Down
8 changes: 5 additions & 3 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,8 @@ async fn check_updates(cfg: &Cfg<'_>) -> Result<utils::ExitCode> {
}

async fn update(cfg: &mut Cfg<'_>, opts: UpdateOpts) -> Result<utils::ExitCode> {
let mut exit_code = utils::ExitCode(0);

common::warn_if_host_is_emulated(cfg.process);
let self_update_mode = cfg.get_self_update_mode()?;
// Priority: no-self-update feature > self_update_mode > no-self-update args.
Expand Down Expand Up @@ -862,10 +864,10 @@ async fn update(cfg: &mut Cfg<'_>, opts: UpdateOpts) -> Result<utils::ExitCode>
}
}
if self_update {
common::self_update(|| Ok(utils::ExitCode(0)), cfg.process).await?;
common::self_update(|| Ok(()), cfg.process).await?;
}
} else {
common::update_all_channels(cfg, self_update, opts.force).await?;
exit_code &= common::update_all_channels(cfg, self_update, opts.force).await?;
info!("cleaning up downloads & tmp directories");
utils::delete_dir_contents_following_links(&cfg.download_dir);
cfg.tmp_cx.clean();
Expand All @@ -880,7 +882,7 @@ async fn update(cfg: &mut Cfg<'_>, opts: UpdateOpts) -> Result<utils::ExitCode>
info!("any updates to rustup will need to be fetched with your system package manager")
}

Ok(utils::ExitCode(0))
Ok(exit_code)
}

async fn run(
Expand Down
23 changes: 23 additions & 0 deletions src/utils/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::env;
use std::fs::{self, File};
use std::io::{self, BufReader, Write};
use std::ops::{BitAnd, BitAndAssign};
use std::path::{Path, PathBuf};
use std::process::ExitStatus;

Expand All @@ -21,8 +22,30 @@ pub(crate) use crate::utils::utils::raw::is_directory;

pub use crate::utils::utils::raw::{is_file, path_exists};

#[derive(Debug, PartialEq, Eq)]
pub struct ExitCode(pub i32);

impl BitAnd for ExitCode {
type Output = Self;

// If `self` is `0` (success), yield `rhs`.
fn bitand(self, rhs: Self) -> Self::Output {
match self.0 {
0 => rhs,
_ => self,
}
}
}

impl BitAndAssign for ExitCode {
// If `self` is `0` (success), set `self` to `rhs`.
fn bitand_assign(&mut self, rhs: Self) {
if self.0 == 0 {
*self = rhs
}
}
}

impl From<ExitStatus> for ExitCode {
fn from(status: ExitStatus) -> Self {
Self(match status.success() {
Expand Down
Loading