more mangling

This commit is contained in:
Mike Griese
2022-01-06 09:43:56 -06:00
parent e64ae7d04b
commit 42036c5370
2 changed files with 29 additions and 1 deletions

View File

@@ -450,4 +450,17 @@ void UtilsTests::TestMangleWSLPaths()
VERIFY_ARE_EQUAL(LR"(wsl ~ -d Ubuntu)", commandline); VERIFY_ARE_EQUAL(LR"(wsl ~ -d Ubuntu)", commandline);
VERIFY_ARE_EQUAL(startingDirectory, path); VERIFY_ARE_EQUAL(startingDirectory, path);
} }
{
// Test for GH#11994 - make sure `//wsl$/` paths get mangled back to
// `\\wsl$\`, to workaround a potential bug in `wsl --cd`
auto [commandline, path] = MangleStartingDirectoryForWSL(LR"(wsl -d Ubuntu)", LR"(//wsl$/Ubuntu/home/user)");
VERIFY_ARE_EQUAL(LR"("wsl" --cd "\\wsl$\Ubuntu\home\user" -d Ubuntu)", commandline);
VERIFY_ARE_EQUAL(L"", path);
}
{
auto [commandline, path] = MangleStartingDirectoryForWSL(LR"(wsl -d Ubuntu)", LR"(\\wsl$\Ubuntu\home\user)");
VERIFY_ARE_EQUAL(LR"("wsl" --cd "\\wsl$\Ubuntu\home\user" -d Ubuntu)", commandline);
VERIFY_ARE_EQUAL(L"", path);
}
} }

View File

@@ -647,8 +647,23 @@ std::tuple<std::wstring, std::wstring> Utils::MangleStartingDirectoryForWSL(std:
// Tilde followed by non-space should be okay (like, wsl -d Debian ~/blah.sh) // Tilde followed by non-space should be okay (like, wsl -d Debian ~/blah.sh)
} }
// GH#11994 - If the path starts with //wsl$, then the user is
// likely passing a Windows-style path to the WSL filesystem,
// but with forward slashes instead of backslashes.
// Unfortunately, `wsl --cd` will try to treat this as a
// linux-relative path, which will fail to do the expected
// thing.
//
// In that case, manually mangle the startingDirectory to use
// backslashes as the path separator instead.
std::wstring mangledDirectory{ startingDirectory };
if (til::starts_with(mangledDirectory, L"//wsl$"))
{
mangledDirectory = std::filesystem::path{ startingDirectory }.make_preferred().wstring();
}
return { return {
fmt::format(LR"("{}" --cd "{}" {})", executablePath.wstring(), startingDirectory, arguments), fmt::format(LR"("{}" --cd "{}" {})", executablePath.wstring(), mangledDirectory, arguments),
std::wstring{} std::wstring{}
}; };
} }