Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 7, 2025

WSL was generating terminal fragment profiles with hardcoded --cd ~ arguments in the command line, which prevented Windows Terminal from being able to override the starting directory using its startingDirectory setting.

Problem

When WSL creates fragment profiles for Windows Terminal, it was generating command lines like:

C:\Windows\System32\wsl.exe --distribution-id {guid} --cd ~

This hardcoded --cd ~ argument overrides any startingDirectory setting that users configure in Windows Terminal, making it impossible to start WSL distributions in directories other than the home directory.

Solution

The fix removes the hardcoded --cd ~ arguments from the command line and instead ensures the fragment profile includes "startingDirectory": "~" in the JSON. This allows Windows Terminal to:

  1. Use the default behavior (starting in ~) when no override is specified
  2. Override the starting directory when users configure startingDirectory in their Terminal settings

Changes

  • Command line generation: Removed WSL_CHANGE_DIRECTORY_ARG and WSL_CWD_HOME from the terminal profile command line
  • JSON profile: Added logic to set "startingDirectory": "~" if not already specified by custom distribution templates
  • Tests: Updated unit tests to verify the new command line format and presence of startingDirectory property

Behavior

Before:

{
  "commandline": "C:\\Windows\\System32\\wsl.exe --distribution-id {guid} --cd ~"
}

After:

{
  "commandline": "C:\\Windows\\System32\\wsl.exe --distribution-id {guid}",
  "startingDirectory": "~"
}

The fix preserves backward compatibility while enabling Terminal's startingDirectory functionality. Desktop shortcuts continue to work correctly as they still use --cd ~ (which is appropriate for shortcuts).

Fixes #13346.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copy link
Contributor Author

Copilot AI commented Aug 7, 2025

@benhillis 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.

I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.

Copilot AI changed the title [WIP] WSL-generated fragment profiles contain --cd ~, which destroys Terminal's support for startingDirectory Remove hardcoded --cd ~ from WSL terminal fragment profiles to support Terminal's startingDirectory Aug 7, 2025
Copilot AI requested a review from benhillis August 7, 2025 00:12
@benhillis benhillis marked this pull request as ready for review August 7, 2025 16:47
@benhillis benhillis requested a review from a team as a code owner August 7, 2025 16:47
@benhillis
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@benhillis
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@benhillis
Copy link
Member

@DHowett - is this what you had in mind?

Copy link
Member

@DHowett DHowett left a comment

Choose a reason for hiding this comment

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

@benhillis looks good to me!

// This allows Windows Terminal to override with startingDirectory setting
if (e.find("startingDirectory") == e.end())
{
e["startingDirectory"] = "~";
Copy link
Collaborator

Choose a reason for hiding this comment

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

@DHowett: Does terminal know how to translate '~' to the proper path ?

Copy link
Collaborator

@OneBlue OneBlue Aug 11, 2025

Choose a reason for hiding this comment

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

Looks like there's special handling which, if I'm reading it correctly, will default to the Windows home directory

Copy link
Member

Choose a reason for hiding this comment

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

Terminal passes ~ through to WSL if it knows it's launching WSL (per the comment below the linked line)

TL;DR: Terminal does not translate ~ at all if WSL is involved. It passes it along as --cd "~"

Copy link
Member

Choose a reason for hiding this comment

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

The line you linked is solidly after all of the WSL --cd promotion attempts fail :)

@benhillis benhillis merged commit 17bd371 into master Aug 11, 2025
6 checks passed
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.

WSL-generated fragment profiles contain --cd ~, which destroys Terminal's support for startingDirectory

4 participants