Skip to content

Commit

Permalink
Don't ever allow ~ as a startingDirectory (#12437)
Browse files Browse the repository at this point in the history
Basically, some WSL distros ship fragments that replace the `commandline` with the executable for their distro (`ubuntu.exe`, etc.). We didn't expect that when we changed the `startingDirectory` for them all to `~`.

Unfortunately, `~` is really never a valid path for a process on windows, so those distros would now fail with

```
[error 2147942667 (0x8007010b) when launching `ubuntu1804.exe']
Could not access starting directory "~"
```

If we find that we were unable to mangle `~` into the user's WSL `commandline`, then we will re-evaluate that `startingDirectory` as `%USERPROFILE%`, which is at least something sensible, if albeit not what they wanted.

* regressed in #12315
* [x] Closes #12353
* [x] Tested with a (`ubuntu1804.exe`, `~`) profile - launched successfully, where 1.13 in market fails.
* [x] added tests

(cherry picked from commit 1870fee)
  • Loading branch information
zadjii-msft authored and DHowett committed Feb 9, 2022
1 parent 37b59c1 commit 076ca02
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/types/ut_types/UtilsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,5 +479,29 @@ void UtilsTests::TestMangleWSLPaths()
VERIFY_ARE_EQUAL(LR"("wsl" --cd "\\wsl.localhost\Ubuntu\home\user" -d Ubuntu)", commandline);
VERIFY_ARE_EQUAL(L"", path);
}

/// Tests for GH #12353

const auto expectedUserProfilePath = wil::ExpandEnvironmentStringsW<std::wstring>(L"%USERPROFILE%");
{
auto [commandline, path] = MangleStartingDirectoryForWSL(LR"(wsl -d Ubuntu)", L"~");
VERIFY_ARE_EQUAL(LR"("wsl" --cd "~" -d Ubuntu)", commandline);
VERIFY_ARE_EQUAL(L"", path);
}
{
auto [commandline, path] = MangleStartingDirectoryForWSL(LR"(wsl ~ -d Ubuntu)", L"~");
VERIFY_ARE_EQUAL(LR"(wsl ~ -d Ubuntu)", commandline);
VERIFY_ARE_EQUAL(expectedUserProfilePath, path);
}
{
auto [commandline, path] = MangleStartingDirectoryForWSL(LR"(ubuntu ~ -d Ubuntu)", L"~");
VERIFY_ARE_EQUAL(LR"(ubuntu ~ -d Ubuntu)", commandline);
VERIFY_ARE_EQUAL(expectedUserProfilePath, path);
}
{
auto [commandline, path] = MangleStartingDirectoryForWSL(LR"(powershell.exe)", L"~");
VERIFY_ARE_EQUAL(LR"(powershell.exe)", commandline);
VERIFY_ARE_EQUAL(expectedUserProfilePath, path);
}
}
#endif
9 changes: 8 additions & 1 deletion src/types/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -670,8 +670,15 @@ std::tuple<std::wstring, std::wstring> Utils::MangleStartingDirectoryForWSL(std:
}
} while (false);

// GH #12353: `~` is never a valid windows path. We can only accept that as
// a startingDirectory when the exe is specifically wsl.exe, because that
// can override the real startingDirectory. If the user set the
// startingDirectory to ~, but the commandline to something like pwsh.exe,
// that won't actually work. In that case, mangle the startingDirectory to
// %userprofile%, so it's at least something reasonable.
return {
std::wstring{ commandLine },
std::wstring{ startingDirectory }
startingDirectory == L"~" ? wil::ExpandEnvironmentStringsW<std::wstring>(L"%USERPROFILE%") :
std::wstring{ startingDirectory }
};
}

0 comments on commit 076ca02

Please sign in to comment.