Skip to content

Conversation

obrobrio2000
Copy link

@obrobrio2000 obrobrio2000 commented Oct 2, 2025

Closes #13530

Summary

This PR implements a quality-of-life improvement that uses the --name parameter value as the folder name for new WSL distributions when distributionInstallPath is configured in .wslconfig but --location is not explicitly provided.

Problem

Currently, when creating a WSL 2 instance with a specific name and a configured default installation path in .wslconfig, the folder created uses a GUID-based name rather than the instance name:

wsl --install Ubuntu-24.04 --name Ubuntu-24.04

With .wslconfig:

[general]
distributionInstallPath=D:\WSL

Results in:

D:\WSL\{d7c75f51-5ebb-4d83-bafe-95e5a8333e9d}\

This makes it harder to manage instances and requires users to always specify --location explicitly.

Solution

This PR modifies the distribution registration logic to use the provided instance name as the folder name when:

  • --name is specified
  • --location is NOT specified
  • A distribution install path is configured (either default or via .wslconfig)

After this change:

wsl --install Ubuntu-24.04 --name Ubuntu-24.04

Results in:

D:\WSL\Ubuntu-24.04\

Backward Compatibility

The GUID-based folder naming is preserved when no --name is provided, ensuring backward compatibility with existing workflows and automation that rely on auto-generated names.

Changes Made

Code Changes

  1. src/windows/service/exe/LxssUserSession.cpp

    • Modified RegisterDistribution to check if a distribution name is provided
    • When a name is provided and no explicit location is set, use the name as the folder name
    • Otherwise, maintain the existing GUID-based behavior
  2. test/windows/UnitTests.cpp

    • Enhanced existing test to validate folder name matches instance name
    • Added new test case to ensure GUID behavior is preserved when appropriate

Testing

The changes include comprehensive test coverage:

  1. Named distribution with configured path: Validates that the folder name matches the provided instance name
  2. Auto-named distribution with configured path: Validates that GUID-based folders are still created when no name is provided
  3. Explicit location: Existing tests validate that --location parameter still works as expected

Benefits

  • Improved usability: Folder names are human-readable and match instance names
  • Easier management: Users can quickly identify and manage distribution folders
  • No breaking changes: Existing behavior is preserved for backward compatibility
  • Consistent with user expectations: The folder name reflects the chosen instance name

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 microsoft#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 <[email protected]>
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements a quality-of-life improvement that uses the --name parameter value as the folder name for new WSL distributions when distributionInstallPath is configured but --location is not explicitly provided. Previously, WSL would always use GUID-based folder names even when users specified a custom instance name.

Key changes:

  • Modified distribution registration logic to use instance name as folder name when --name is provided
  • Preserved GUID-based naming for backward compatibility when no name is specified
  • Added comprehensive test coverage for both named and auto-named distribution scenarios

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/windows/service/exe/LxssUserSession.cpp Updated RegisterDistribution logic to use instance name as folder name when available
test/windows/UnitTests.cpp Enhanced existing test and added new test case to validate both named and GUID-based folder creation

{
std::wstring cmd = std::wstring(L"--unregister ") + name;
LxsstuLaunchWsl(cmd.c_str());
break;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use hardcoded name for the test distributions here, otherwise this might conflicts with pre-existing distributions and I'd rather not have the tests remove my distributions when I run them :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CreateTarFromManifest() can be used to generate a .tar with a specific default name

// Find the installed distribution
auto distros = wsl::windows::common::SvcComm().EnumerateDistributions();
std::wstring installedName;
for (const auto& distro : distros)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this block, since ValidateDistributionStarts will validate that the distribution exists

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();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: We could use GetDistributionId(<name>) and validate that the GUID matches the distribution ID here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Suggestion: To use --name <InstanceName> as the folder name for new instances

2 participants