From 333cc66d3fce480777a927b9d2685e85083a23cd Mon Sep 17 00:00:00 2001 From: Giovanni Magliocchetti Date: Thu, 2 Oct 2025 23:29:15 +0200 Subject: [PATCH 1/2] Use instance name as folder name when --name is specified When creating a WSL 2 instance with `--name` but without `--location`, and when `distributionInstallPath` is configured in `.wslconfig`, the folder name now uses the instance name instead of a GUID. 1. **src/windows/service/exe/LxssUserSession.cpp** - Updated `RegisterDistribution` method to use the provided distribution name as the folder name when: - `DistributionName` is provided (via `--name` parameter) - `TargetDirectory` is not provided (no `--location` parameter) - Maintains backward compatibility by using GUID when no name is provided 2. **test/windows/UnitTests.cpp** - Enhanced existing test to verify folder name matches instance name - Added new test case to verify GUID behavior is preserved when no name is provided Fixes #13530 This change improves usability by making distribution folders more identifiable and easier to manage when users explicitly provide instance names. Signed-off-by: Giovanni Magliocchetti --- src/windows/service/exe/LxssUserSession.cpp | 11 +++- test/windows/UnitTests.cpp | 64 +++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) 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..2f53c6132 100644 --- a/test/windows/UnitTests.cpp +++ b/test/windows/UnitTests.cpp @@ -4337,6 +4337,9 @@ 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"); + ValidateDistributionShortcut(L"test-overridden-default-location", nullptr); cleanup.reset(); @@ -4346,6 +4349,67 @@ 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) + { + auto cleanup = wil::scope_exit_log( + WI_DIAGNOSTICS_INFO, []() { + auto distros = wsl::windows::common::SvcComm().EnumerateDistributions(); + for (const auto& distro : distros) + { + auto name = distro.DistributionName.get(); + if (wcsstr(name, L"Ubuntu") != nullptr) + { + std::wstring cmd = std::wstring(L"--unregister ") + name; + LxsstuLaunchWsl(cmd.c_str()); + break; + } + } + }); + + auto currentPath = std::filesystem::current_path(); + WslConfigChange wslconfig(std::format(L"[general]\ndistributionInstallPath = {}", EscapePath(currentPath.wstring()))); + + // Install without --name, so the distribution name will be auto-generated + InstallFromTar(g_testDistroPath.c_str(), L""); + + // Find the installed distribution + auto distros = wsl::windows::common::SvcComm().EnumerateDistributions(); + std::wstring installedName; + for (const auto& distro : distros) + { + auto name = distro.DistributionName.get(); + if (wcsstr(name, L"Ubuntu") != nullptr) + { + installedName = name; + break; + } + } + + VERIFY_IS_FALSE(installedName.empty()); + ValidateDistributionStarts(installedName.c_str()); + + auto distroKey = OpenDistributionKey(installedName.c_str()); + 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) + auto folderName = std::filesystem::path(basePath).filename().wstring(); + // A GUID has the format {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} (38 chars) + VERIFY_ARE_EQUAL(folderName.length(), static_cast(38)); + VERIFY_ARE_EQUAL(folderName[0], L'{'); + VERIFY_ARE_EQUAL(folderName[37], L'}'); + + cleanup.reset(); + + // Validate that the base path is removed + VERIFY_IS_FALSE(std::filesystem::exists(basePath)); + } + // Distribution installed in a custom location { From 10ba815aae034ee3b4ed6c8684f82c2a9df46c82 Mon Sep 17 00:00:00 2001 From: Giovanni Magliocchetti Date: Fri, 3 Oct 2025 02:04:37 +0200 Subject: [PATCH 2/2] feat: enhance distribution registration to support custom folder names Signed-off-by: Giovanni Magliocchetti --- test/windows/UnitTests.cpp | 56 ++++++++++++++------------------------ 1 file changed, 21 insertions(+), 35 deletions(-) diff --git a/test/windows/UnitTests.cpp b/test/windows/UnitTests.cpp index 2f53c6132..a617fdfbf 100644 --- a/test/windows/UnitTests.cpp +++ b/test/windows/UnitTests.cpp @@ -4340,6 +4340,9 @@ Error code: Wsl/Service/RegisterDistro/WSL_E_DISTRIBUTION_NAME_NEEDED\r\n"; // 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(); @@ -4351,44 +4354,26 @@ Error code: Wsl/Service/RegisterDistro/WSL_E_DISTRIBUTION_NAME_NEEDED\r\n"; // 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, []() { - auto distros = wsl::windows::common::SvcComm().EnumerateDistributions(); - for (const auto& distro : distros) - { - auto name = distro.DistributionName.get(); - if (wcsstr(name, L"Ubuntu") != nullptr) - { - std::wstring cmd = std::wstring(L"--unregister ") + name; - LxsstuLaunchWsl(cmd.c_str()); - break; - } - } + 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()))); - // Install without --name, so the distribution name will be auto-generated - InstallFromTar(g_testDistroPath.c_str(), L""); + // 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"); - // Find the installed distribution - auto distros = wsl::windows::common::SvcComm().EnumerateDistributions(); - std::wstring installedName; - for (const auto& distro : distros) - { - auto name = distro.DistributionName.get(); - if (wcsstr(name, L"Ubuntu") != nullptr) - { - installedName = name; - break; - } - } - - VERIFY_IS_FALSE(installedName.empty()); - ValidateDistributionStarts(installedName.c_str()); + // 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(installedName.c_str()); + auto distroKey = OpenDistributionKey(distroName); VERIFY_IS_TRUE(!!distroKey); auto basePath = wsl::windows::common::registry::ReadString(distroKey.get(), nullptr, L"BasePath", L""); @@ -4398,11 +4383,12 @@ Error code: Wsl/Service/RegisterDistro/WSL_E_DISTRIBUTION_NAME_NEEDED\r\n"; 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) - auto folderName = std::filesystem::path(basePath).filename().wstring(); - // A GUID has the format {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} (38 chars) - VERIFY_ARE_EQUAL(folderName.length(), static_cast(38)); - VERIFY_ARE_EQUAL(folderName[0], L'{'); - VERIFY_ARE_EQUAL(folderName[37], L'}'); + // 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();