install: do not attempt to install distros if a reboot is required (#13298)

* install: do not attempt to install distros if a reboot is required * nfc: update comment --------- Co-authored-by: Ben Hillis <benhill@ntdev.microsoft.com>

Ben Hillis committed Jul 29, 2025 at 08:48 UTC 4867250d1148624a5bc255f849e37910aae04148
3 files changed +10 -10
src/windows/common/WslClient.cpp
+4 -7
@@ -568,10 +568,7 @@ int Install(_In_ std::wstring_view commandLine)
568 }
569
570 bool rebootRequired = InstallPrerequisites(installWslOptionalComponent);
571 - if (rebootRequired)
572 - {
573 - noLaunchAfterInstall = false;
574 - }
571 + noLaunchAfterInstall |= rebootRequired;
572
573 // Install a distribution only if no reboot is required, or if we're on the --legacy path (to maintain old behavior).
574 const Distribution* legacyDistro = nullptr;
@@ -647,10 +644,10 @@ int Install(_In_ std::wstring_view commandLine)
644
645 bool InstallPrerequisites(_In_ bool installWslOptionalComponent)
646 {
650 - const auto missingComponents = WslInstall::CheckForMissingOptionalComponents(installWslOptionalComponent);
647 + const auto [rebootRequired, missingComponents] = WslInstall::CheckForMissingOptionalComponents(installWslOptionalComponent);
648 if (missingComponents.empty())
649 {
653 - return false;
650 + return rebootRequired;
651 }
652
653 // Install any optional components that have not yet been installed.
@@ -671,7 +668,7 @@ bool InstallPrerequisites(_In_ bool installWslOptionalComponent)
668 WslInstall::InstallOptionalComponents(missingComponents);
669 }
670
674 - return true;
671 + return rebootRequired;
672 }
673
674 int LaunchProcess(_In_opt_ LPCWSTR filename, _In_ int argc, _In_reads_(argc) LPCWSTR argv[], _In_ const LaunchProcessOptions& options)
src/windows/common/WslInstall.cpp
+5 -2
@@ -211,7 +211,7 @@ try
211 }
212 CATCH_RETURN()
213
214 -std::vector<std::wstring> WslInstall::CheckForMissingOptionalComponents(_In_ bool requireWslOptionalComponent)
214 +std::pair<bool, std::vector<std::wstring>> WslInstall::CheckForMissingOptionalComponents(_In_ bool requireWslOptionalComponent)
215 {
216 // Include the WSL optional component if it was requested, or if the OS is not Windows 11 or later.
217 std::vector<std::wstring> missingComponents;
@@ -226,6 +226,9 @@ std::vector<std::wstring> WslInstall::CheckForMissingOptionalComponents(_In_ boo
226 missingComponents.emplace_back(c_optionalFeatureNameVmp);
227 }
228
229 + // If any required components are not present, a reboot is required.
230 + bool rebootRequired = !missingComponents.empty();
231 +
232 // Query the list of optional components that have already been installed.
233 const auto installedComponents = GetInstalledOptionalComponents();
234 for (const auto& component : installedComponents)
@@ -233,7 +236,7 @@ std::vector<std::wstring> WslInstall::CheckForMissingOptionalComponents(_In_ boo
236 std::erase(missingComponents, component);
237 }
238
236 - return missingComponents;
239 + return {rebootRequired, std::move(missingComponents)};
240 }
241
242 void WslInstall::InstallOptionalComponents(const std::vector<std::wstring>& components)
src/windows/common/WslInstall.h
+1 -1
@@ -40,7 +40,7 @@ public:
40 _In_ const std::optional<std::wstring>& location,
41 _In_ const std::optional<uint64_t>& vhdSize);
42
43 - static std::vector<std::wstring> CheckForMissingOptionalComponents(_In_ bool requireWslOptionalComponent);
43 + static std::pair<bool, std::vector<std::wstring>> CheckForMissingOptionalComponents(_In_ bool requireWslOptionalComponent);
44
45 static void InstallOptionalComponents(const std::vector<std::wstring>& components);
46