Skip to content

Commit cfe8bd7

Browse files
committed
Improve handling of Visual Studio errors
Make sure the user can always sees Visual Studio install errors even if the console closes when rustup-init exits. Also turn errors into warnings if the VS installer returned a non-zero exit code but all necessary components were installed.
1 parent feec94b commit cfe8bd7

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

src/cli/self_update.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ use anyhow::{anyhow, Context, Result};
5757
use cfg_if::cfg_if;
5858
use same_file::Handle;
5959

60-
use super::common::{self, ignorable_error, Confirm};
60+
use super::common::{self, ignorable_error, report_error, Confirm};
6161
use super::errors::*;
6262
use super::markdown::md;
6363
use super::term2;
@@ -380,7 +380,15 @@ pub(crate) fn install(
380380
"\nAutomatically download and install Visual Studio 2022 Community edition? (Y/n)",
381381
true,
382382
)? {
383-
try_install_msvc()?;
383+
if let Err(e) = try_install_msvc(&opts) {
384+
// Make sure the console doesn't exit before the user can
385+
// see the error and give the option to continue anyway.
386+
report_error(&e);
387+
if !common::confirm("\nContinue installing rustup? (y/N)", false)? {
388+
info!("aborting installation");
389+
return Ok(utils::ExitCode(0));
390+
}
391+
}
384392
} else {
385393
md(&mut term, MSVC_MANUAL_INSTALL_MESSAGE);
386394
if !common::confirm("\nContinue? (y/N)", false)? {

src/cli/self_update/windows.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ impl fmt::Display for VsInstallError {
9898
}
9999
}
100100

101-
pub(crate) fn try_install_msvc() -> Result<()> {
101+
pub(crate) fn try_install_msvc(opts: &InstallOpts<'_>) -> Result<()> {
102102
// download the installer
103103
let visual_studio_url = utils::parse_url("https://aka.ms/vs/17/release/vs_community.exe")?;
104104

@@ -131,16 +131,7 @@ pub(crate) fn try_install_msvc() -> Result<()> {
131131

132132
// It's possible an earlier or later version of the Windows SDK has been
133133
// installed separately from Visual Studio so installing it can be skipped.
134-
let mut has_libs = false;
135-
if let Some(paths) = process().var_os("lib") {
136-
for mut path in split_paths(&paths) {
137-
path.push("kernel32.lib");
138-
if path.exists() {
139-
has_libs = true;
140-
}
141-
}
142-
};
143-
if !has_libs {
134+
if !has_windows_sdk_libs() {
144135
cmd.args([
145136
"--add",
146137
"Microsoft.VisualStudio.Component.Windows11SDK.22000",
@@ -156,10 +147,34 @@ pub(crate) fn try_install_msvc() -> Result<()> {
156147
if exit_status.success() {
157148
Ok(())
158149
} else {
159-
Err(VsInstallError(exit_status.code().unwrap())).context("failed to install Visual Studio")
150+
let err = VsInstallError(exit_status.code().unwrap());
151+
// It's possible that the installer returned a non-zero exit code
152+
// even though the required components were successfully installed.
153+
// In that case we warn about the error but continue on.
154+
let have_msvc = do_msvc_check(opts).is_none();
155+
let has_libs = has_windows_sdk_libs();
156+
if have_msvc && has_libs {
157+
warn!("Visual Studio is installed but a problem ocurred during installation");
158+
warn!("{}", err);
159+
Ok(())
160+
} else {
161+
Err(err).context("failed to install Visual Studio")
162+
}
160163
}
161164
}
162165

166+
fn has_windows_sdk_libs() -> bool {
167+
if let Some(paths) = process().var_os("lib") {
168+
for mut path in split_paths(&paths) {
169+
path.push("kernel32.lib");
170+
if path.exists() {
171+
return true;
172+
}
173+
}
174+
};
175+
false
176+
}
177+
163178
/// Run by rustup-gc-$num.exe to delete CARGO_HOME
164179
pub fn complete_windows_uninstall() -> Result<utils::ExitCode> {
165180
use std::process::Stdio;

0 commit comments

Comments
 (0)