diff --git a/src/windows/service/exe/LxssUserSession.cpp b/src/windows/service/exe/LxssUserSession.cpp index 2f541d4fe..a204999cb 100644 --- a/src/windows/service/exe/LxssUserSession.cpp +++ b/src/windows/service/exe/LxssUserSession.cpp @@ -1431,7 +1431,16 @@ HRESULT LxssUserSessionImpl::RegisterDistribution( if (TargetDirectory == nullptr) { - distributionPath = config.DefaultDistributionLocation / wsl::shared::string::GuidToString(DistributionId); + // If a custom name is provided, use it as the folder name instead of a GUID. + // This improves usability when distributionInstallPath is configured in .wslconfig. + if (DistributionName != nullptr) + { + distributionPath = config.DefaultDistributionLocation / DistributionName; + } + else + { + distributionPath = config.DefaultDistributionLocation / wsl::shared::string::GuidToString(DistributionId); + } } else { diff --git a/test/windows/UnitTests.cpp b/test/windows/UnitTests.cpp index 2aa46b363..a617fdfbf 100644 --- a/test/windows/UnitTests.cpp +++ b/test/windows/UnitTests.cpp @@ -4337,6 +4337,12 @@ Error code: Wsl/Service/RegisterDistro/WSL_E_DISTRIBUTION_NAME_NEEDED\r\n"; // Validate that the distribution was created in the correct path VERIFY_ARE_EQUAL(std::filesystem::path(basePath).parent_path().string(), currentPath.string()); + // Validate that the folder name matches the instance name (not a GUID) + VERIFY_ARE_EQUAL(std::filesystem::path(basePath).filename().wstring(), L"test-overridden-default-location"); + + // Validate that the folder name matches the instance name (not a GUID) + VERIFY_ARE_EQUAL(std::filesystem::path(basePath).filename().wstring(), L"test-overridden-default-location"); + ValidateDistributionShortcut(L"test-overridden-default-location", nullptr); cleanup.reset(); @@ -4346,6 +4352,50 @@ Error code: Wsl/Service/RegisterDistro/WSL_E_DISTRIBUTION_NAME_NEEDED\r\n"; VERIFY_IS_FALSE(std::filesystem::exists(basePath)); } + // Distribution with overridden default location but without explicit name (should use GUID) + { + constexpr auto distroName = L"test-guid-folder"; + + auto cleanup = wil::scope_exit_log( + WI_DIAGNOSTICS_INFO, [&]() { + LxsstuLaunchWsl(std::format(L"--unregister {}", distroName)); + DeleteFile(L"test-guid-folder.tar"); + }); + + auto currentPath = std::filesystem::current_path(); + WslConfigChange wslconfig(std::format(L"[general]\ndistributionInstallPath = {}", EscapePath(currentPath.wstring()))); + + // Create a tar with a default name, then install without --name + CreateTarFromManifest(std::format(L"[oobe]\ndefaultName = {}", distroName).c_str(), L"test-guid-folder.tar"); + + // Install without --name, so the folder should use a GUID despite having a default name + InstallFromTar(L"test-guid-folder.tar", L""); + + ValidateDistributionStarts(distroName); + + auto distroKey = OpenDistributionKey(distroName); + VERIFY_IS_TRUE(!!distroKey); + + auto basePath = wsl::windows::common::registry::ReadString(distroKey.get(), nullptr, L"BasePath", L""); + VERIFY_IS_TRUE(std::filesystem::exists(basePath)); + + // Validate that the distribution was created in the correct path + VERIFY_ARE_EQUAL(std::filesystem::path(basePath).parent_path().string(), currentPath.string()); + + // Validate that the folder name is a GUID (since no --name was provided) + // The GUID should match the distribution ID + wsl::windows::common::SvcComm service; + auto distroGuid = service.GetDistributionId(distroName); + auto expectedFolderName = wsl::shared::string::GuidToString(distroGuid); + auto actualFolderName = std::filesystem::path(basePath).filename().wstring(); + VERIFY_ARE_EQUAL(expectedFolderName, actualFolderName); + + cleanup.reset(); + + // Validate that the base path is removed + VERIFY_IS_FALSE(std::filesystem::exists(basePath)); + } + // Distribution installed in a custom location {