Skip to content

Commit 557bf05

Browse files
committed
Escape single quotes while translating dropped Win32 paths
When file/folder is dropped to the terminal, its path is translated and quoted with a pair of single quotes if the profile is of the WSL. However, the terminal control did not escape single quotes (allowed in the Win32 subsystem) that need escapes when translated. On WSL profiles, it causes incorrect path to be pasted when the path contains one or more single quotes (see Issue microsoft#18006 for an example). With this commit, the terminal control escapes a single quote with a valid escape sequence `'\''` (finish quote, print a single quote then begin quote again) when the WSL path translation is required.
1 parent 03f0505 commit 557bf05

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/cascadia/TerminalControl/TermControl.cpp

+19-1
Original file line numberDiff line numberDiff line change
@@ -3250,7 +3250,25 @@ namespace winrt::Microsoft::Terminal::Control::implementation
32503250
{
32513251
allPathsString.push_back(quotesChar);
32523252
}
3253-
allPathsString.append(fullPath);
3253+
if (isWSL)
3254+
{
3255+
// Fix quoted path for WSL
3256+
// Single quote is allowed on the Win32 subsystem and must be processed on WSL profiles.
3257+
// Note that we assume that all paths are quoted using a pair of single quotes for WSL.
3258+
std::wstring_view fullPathView{ fullPath };
3259+
size_t pos;
3260+
while ((pos = fullPathView.find(L'\'')) != std::wstring_view::npos)
3261+
{
3262+
allPathsString.append(fullPathView.begin(), fullPathView.begin() + pos);
3263+
allPathsString.append(L"'\\''");
3264+
fullPathView.remove_prefix(pos + 1);
3265+
}
3266+
allPathsString.append(fullPathView);
3267+
}
3268+
else
3269+
{
3270+
allPathsString.append(fullPath);
3271+
}
32543272
if (quotesNeeded)
32553273
{
32563274
allPathsString.push_back(quotesChar);

0 commit comments

Comments
 (0)