Skip to content

Implement OSC 7 for setting the CWD#20019

Merged
carlos-zamora merged 3 commits intomainfrom
dev/lhecker/3158-osc7
Mar 30, 2026
Merged

Implement OSC 7 for setting the CWD#20019
carlos-zamora merged 3 commits intomainfrom
dev/lhecker/3158-osc7

Conversation

@lhecker
Copy link
Copy Markdown
Member

@lhecker lhecker commented Mar 25, 2026

This adds support for OSC 7 which is the same as OSC 9;9 but using
file URIs. As per the previous discussion in #8214, the problem is
that WSL shells emit URIs that can't work because the hostname in
the URI translates to an UNC path with an non-existing hostname.
In my opinion this doesn't deter the fact though that OSC 7 works
just fine for a native Windows application.

In the future we should consider trying to detect WSL file URIs
and translating it to the --cd argument for wsl.exe.

All of the heavy lifting for parsing the URI is done by
PathCreateFromUrlW. It just had to be plugged into the OSC 9;9 code.

Closes #3158

Validation Steps Performed

  • Launch fish-shell in WSL
  • Duplicate tab works ✅
  • (And because it doesn't work without using
    IsValidDirectory I know that OSC 7 works ✅)

@microsoft-github-policy-service microsoft-github-policy-service bot added Issue-Feature Complex enough to require an in depth planning process and actual budgeted, scheduled work. Area-Settings Issues related to settings and customizability, for console or terminal Area-VT Virtual Terminal sequence support Product-Conpty For console issues specifically related to conpty Product-Powershell Issues in the modern command interpreter, Powershell.exe. Product-Terminal The new Windows Terminal. labels Mar 25, 2026
Copy link
Copy Markdown
Member

@carlos-zamora carlos-zamora left a comment

Choose a reason for hiding this comment

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

Looks good! Just the minor comments and it looks like you need to run the code formatter. I'll circle back to this then.

Copy link
Copy Markdown
Member

@carlos-zamora carlos-zamora left a comment

Choose a reason for hiding this comment

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

kemit do it meme

@carlos-zamora carlos-zamora enabled auto-merge (squash) March 30, 2026 17:40
Copy link
Copy Markdown
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.

blocking to think about my opinion

@microsoft-github-policy-service microsoft-github-policy-service bot added the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label Mar 30, 2026
// and the resulting path can never be longer than the URI.
const auto ptr = path.data();
auto len = gsl::narrow<DWORD>(path.size());
THROW_IF_FAILED(PathCreateFromUrlW(ptr, ptr, &len, 0));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this will add a dependency from conhost on the library containing PathCreateFromUrlW; is that acceptable?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

.... it's in kernelbase?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

    api-ms-win-core-url-l1-1-0
      PathCreateFromUrlW

hmm where are you hosted little guy

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

> $d.Apis["PathCreateFromUrlW"][1].Binary


Architecture                       : Amd64
BinplacePath                       : kernelbase.dll

wellp


winrt::hstring ControlCore::CurrentWorkingDirectory() const
{
return winrt::hstring{ _terminal->GetWorkingDirectory() };
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

i s2g, we just had a whole-ass duplicate of this function?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah lol. I was super surprised about that too. We don't have skeletons in our basement, we got entire graveyards apparently lmao.

}

WIN32_FILE_ATTRIBUTE_DATA data;
const auto ok = GetFileAttributesExW(path, GetFileExInfoStandard, &data);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

instead of std::filesystem::access or whatever? cool w/ me

Copy link
Copy Markdown
Member Author

@lhecker lhecker Mar 30, 2026

Choose a reason for hiding this comment

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

Yeah, I just hate that the std::filesystem::path objects are expensive to construct. Under the hood, it gets passed to __std_fs_get_stats, which - and you will never guess this - takes a null-terminated string. 🤦‍♂️

Hurr durr "C++ is for low level zero overhead real man's man engineering."
Meanwhile: Wrap in wstring to unwrap in pointer. lmao

@microsoft-github-policy-service microsoft-github-policy-service bot removed the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label Mar 30, 2026
@carlos-zamora carlos-zamora merged commit 4ce79d7 into main Mar 30, 2026
18 of 20 checks passed
@carlos-zamora carlos-zamora deleted the dev/lhecker/3158-osc7 branch March 30, 2026 18:10
@github-project-automation github-project-automation bot moved this to To Cherry Pick in 1.25 Servicing Pipeline Apr 3, 2026
@DHowett DHowett moved this from To Cherry Pick to Cherry Picked in 1.25 Servicing Pipeline Apr 3, 2026
DHowett pushed a commit that referenced this pull request Apr 3, 2026
This adds support for OSC 7 which is the same as OSC 9;9 but using
file URIs. As per the previous discussion in #8214, the problem is
that WSL shells emit URIs that can't work because the hostname in
the URI translates to an UNC path with an non-existing hostname.
In my opinion this doesn't deter the fact though that OSC 7 works
just fine for a native Windows application.

In the future we should consider trying to detect WSL file URIs
and translating it to the `--cd` argument for `wsl.exe`.

All of the heavy lifting for parsing the URI is done by
`PathCreateFromUrlW`. It just had to be plugged into the OSC 9;9 code.

Closes #3158

## Validation Steps Performed
* Launch fish-shell in WSL
* Duplicate tab works ✅
* (And because it doesn't work without using
  `IsValidDirectory` I know that OSC 7 works ✅)

(cherry picked from commit 4ce79d7)
Service-Card-Id: PVTI_lADOAF3p4s4BQX0-zgpBLe8
Service-Version: 1.25
@austin-beer
Copy link
Copy Markdown

austin-beer commented Apr 6, 2026

I just updated to version 1.25.923.0 (a Friday servicing release: https://github.com/microsoft/terminal/releases/tag/v1.25.923.0 😃). I currently use OSC 9;9 to set the CWD. It appears that that is now broken. Was that intentional?

@lhecker
Copy link
Copy Markdown
Member Author

lhecker commented Apr 6, 2026

@austin-beer Be more specific when reporting issues please. OSC 9;9 + Duplicate Tab works as expected for me.

@DHowett
Copy link
Copy Markdown
Member

DHowett commented Apr 6, 2026

Unfortunately I was also unable to repro any breakage here! There might be some additional conditions we aren't aware of 😅

@DHowett
Copy link
Copy Markdown
Member

DHowett commented Apr 6, 2026

The only difference for OSC 9;9 is that we now check whether the target directory truly is accessible as a directory before spawning a process there.

If somebody was smuggling Linux paths in via OSC 9;9, it may have worked previously, but it would have been out of spec.

@DHowett
Copy link
Copy Markdown
Member

DHowett commented Apr 6, 2026

Chatting with Leonard a bit more, we have some ideas on how to make this less terrible for everyone.

@austin-beer
Copy link
Copy Markdown

Okay, yes, I am providing Linux paths, not Windows paths, so that's why it's now broken for me. I need to change my command prompt to use the documented approach that converts to Windows paths first, correct?

@DHowett
Copy link
Copy Markdown
Member

DHowett commented Apr 6, 2026

That'll fix it for now while we evaluate how best to fix it for everyone. Thanks for using Preview! :)

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

Labels

Area-Settings Issues related to settings and customizability, for console or terminal Area-VT Virtual Terminal sequence support Issue-Feature Complex enough to require an in depth planning process and actual budgeted, scheduled work. Product-Conpty For console issues specifically related to conpty Product-Powershell Issues in the modern command interpreter, Powershell.exe. Product-Terminal The new Windows Terminal.

Projects

Status: Cherry Picked

Development

Successfully merging this pull request may close these issues.

Open new terminal tab in same directory as existing tab (OSC 7?)

4 participants