Skip to content

Commit 60d75df

Browse files
committed
Explicitly handle Visual Studio needing a restart
1 parent cfe8bd7 commit 60d75df

File tree

2 files changed

+56
-21
lines changed

2 files changed

+56
-21
lines changed

src/cli/self_update.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -380,14 +380,21 @@ pub(crate) fn install(
380380
"\nAutomatically download and install Visual Studio 2022 Community edition? (Y/n)",
381381
true,
382382
)? {
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");
383+
match try_install_msvc(&opts) {
384+
Err(e) => {
385+
// Make sure the console doesn't exit before the user can
386+
// see the error and give the option to continue anyway.
387+
report_error(&e);
388+
if !common::confirm("\nContinue installing rustup? (y/N)", false)? {
389+
info!("aborting installation");
390+
return Ok(utils::ExitCode(0));
391+
}
392+
}
393+
Ok(ContinueInstall::No) => {
394+
ensure_prompt()?;
389395
return Ok(utils::ExitCode(0));
390396
}
397+
_ => {}
391398
}
392399
} else {
393400
md(&mut term, MSVC_MANUAL_INSTALL_MESSAGE);

src/cli/self_update/windows.rs

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub(crate) fn do_msvc_check(opts: &InstallOpts<'_>) -> Option<VsInstallPlan> {
6666
}
6767
}
6868

69-
#[derive(Debug)]
69+
#[derive(Debug, Eq, PartialEq)]
7070
struct VsInstallError(i32);
7171
impl std::error::Error for VsInstallError {}
7272
impl fmt::Display for VsInstallError {
@@ -97,8 +97,21 @@ impl fmt::Display for VsInstallError {
9797
write!(f, "{} (exit code {})", message, self.0)
9898
}
9999
}
100+
impl VsInstallError {
101+
const REBOOTING_NOW: Self = Self(1641);
102+
const REBOOT_REQUIRED: Self = Self(3010);
103+
}
104+
105+
pub(crate) enum ContinueInstall {
106+
Yes,
107+
No,
108+
}
100109

101-
pub(crate) fn try_install_msvc(opts: &InstallOpts<'_>) -> Result<()> {
110+
/// Tries to install the needed Visual Studio components.
111+
///
112+
/// Returns `Ok(ContinueInstall::No)` if installing Visual Studio was successful
113+
/// but the rustup install should not be continued at this time.
114+
pub(crate) fn try_install_msvc(opts: &InstallOpts<'_>) -> Result<ContinueInstall> {
102115
// download the installer
103116
let visual_studio_url = utils::parse_url("https://aka.ms/vs/17/release/vs_community.exe")?;
104117

@@ -145,20 +158,35 @@ pub(crate) fn try_install_msvc(opts: &InstallOpts<'_>) -> Result<()> {
145158
.context("error running Visual Studio installer")?;
146159

147160
if exit_status.success() {
148-
Ok(())
161+
Ok(ContinueInstall::Yes)
149162
} else {
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")
163+
match VsInstallError(exit_status.code().unwrap()) {
164+
err @ VsInstallError::REBOOT_REQUIRED => {
165+
// A reboot is required but the user opted to delay it.
166+
warn!("{}", err);
167+
Ok(ContinueInstall::Yes)
168+
}
169+
err @ VsInstallError::REBOOTING_NOW => {
170+
// The user is wanting to reboot right now, so we should
171+
// not continue the install.
172+
warn!("{}", err);
173+
info!("\nRun rustup-init after restart to continue install");
174+
Ok(ContinueInstall::No)
175+
}
176+
err => {
177+
// It's possible that the installer returned a non-zero exit code
178+
// even though the required components were successfully installed.
179+
// In that case we warn about the error but continue on.
180+
let have_msvc = do_msvc_check(opts).is_none();
181+
let has_libs = has_windows_sdk_libs();
182+
if have_msvc && has_libs {
183+
warn!("Visual Studio is installed but a problem ocurred during installation");
184+
warn!("{}", err);
185+
Ok(ContinueInstall::Yes)
186+
} else {
187+
Err(err).context("failed to install Visual Studio")
188+
}
189+
}
162190
}
163191
}
164192
}

0 commit comments

Comments
 (0)