Skip to content

Commit 4c4f395

Browse files
authored
Merge pull request #612 from mjcheetham/wsl-localhost
Update `IsWslPath` to detect `\\wsl.localhost`-style paths
2 parents 00b84a0 + 15a0c7b commit 4c4f395

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

src/shared/Core.Tests/WslUtilsTests.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace GitCredentialManager.Tests
66
{
77
public class WslUtilsTests
88
{
9-
[PlatformTheory(Platforms.Windows)]
9+
[Theory]
1010
[InlineData(null, false)]
1111
[InlineData(@"", false)]
1212
[InlineData(@" ", false)]
@@ -26,6 +26,18 @@ public class WslUtilsTests
2626
[InlineData(@"\\WSL$\UBUNTU\home", true)]
2727
[InlineData(@"\\wsl$\ubuntu\home\", true)]
2828
[InlineData(@"\\wsl$\openSUSE-42\home", true)]
29+
[InlineData(@"wsl.localhost", false)]
30+
[InlineData(@"\wsl.localhost\ubuntu\home", false)]
31+
[InlineData(@"wsl.localhost\ubuntu\home", false)]
32+
[InlineData(@"//wsl.localhost/ubuntu/home", false)]
33+
[InlineData(@"\\wsl.localhost", false)]
34+
[InlineData(@"\\wsl.localhost\", false)]
35+
[InlineData(@"\\wsl.localhost\ubuntu", true)]
36+
[InlineData(@"\\wsl.localhost\ubuntu\", true)]
37+
[InlineData(@"\\wsl.localhost\ubuntu\home", true)]
38+
[InlineData(@"\\WSL.LOCALHOST\UBUNTU\home", true)]
39+
[InlineData(@"\\wsl.localhost\ubuntu\home\", true)]
40+
[InlineData(@"\\wsl.localhost\openSUSE-42\home", true)]
2941
public void WslUtils_IsWslPath(string path, bool expected)
3042
{
3143
bool actual = WslUtils.IsWslPath(path);
@@ -40,14 +52,21 @@ public void WslUtils_IsWslPath(string path, bool expected)
4052
[InlineData(@"\\wsl$\UBUNTU\home", "UBUNTU", "/home")]
4153
[InlineData(@"\\wsl$\ubuntu\home\", "ubuntu", "/home/")]
4254
[InlineData(@"\\wsl$\openSUSE-42\home", "openSUSE-42", "/home")]
55+
[InlineData(@"\\wsl.localhost\ubuntu", "ubuntu", "/")]
56+
[InlineData(@"\\wsl.localhost\ubuntu\", "ubuntu", "/")]
57+
[InlineData(@"\\wsl.localhost\ubuntu\home", "ubuntu", "/home")]
58+
[InlineData(@"\\wsl.localhost\ubuntu\HOME", "ubuntu", "/HOME")]
59+
[InlineData(@"\\wsl.localhost\UBUNTU\home", "UBUNTU", "/home")]
60+
[InlineData(@"\\wsl.localhost\ubuntu\home\", "ubuntu", "/home/")]
61+
[InlineData(@"\\wsl.localhost\openSUSE-42\home", "openSUSE-42", "/home")]
4362
public void WslUtils_ConvertToDistroPath(string path, string expectedDistro, string expectedPath)
4463
{
4564
string actualPath = WslUtils.ConvertToDistroPath(path, out string actualDistro);
4665
Assert.Equal(expectedPath, actualPath);
4766
Assert.Equal(expectedDistro, actualDistro);
4867
}
4968

50-
[PlatformTheory(Platforms.Windows)]
69+
[Theory]
5170
[InlineData(null)]
5271
[InlineData(@"")]
5372
[InlineData(@" ")]
@@ -61,6 +80,12 @@ public void WslUtils_ConvertToDistroPath(string path, string expectedDistro, str
6180
[InlineData(@"\\wsl")]
6281
[InlineData(@"\\wsl$")]
6382
[InlineData(@"\\wsl$\")]
83+
[InlineData(@"wsl.localhost")]
84+
[InlineData(@"\wsl.localhost\ubuntu\home")]
85+
[InlineData(@"wsl.localhost\ubuntu\home")]
86+
[InlineData(@"//wsl.localhost/ubuntu/home")]
87+
[InlineData(@"\\wsl.localhost")]
88+
[InlineData(@"\\wsl.localhost\")]
6489
public void WslUtils_ConvertToDistroPath_Invalid_ThrowsException(string path)
6590
{
6691
Assert.Throws<ArgumentException>(() => WslUtils.ConvertToDistroPath(path, out _));

src/shared/Core/WslUtils.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace GitCredentialManager
88
public static class WslUtils
99
{
1010
private const string WslUncPrefix = @"\\wsl$\";
11+
private const string WslLocalHostUncPrefix = @"\\wsl.localhost\";
1112
private const string WslCommandName = "wsl.exe";
1213

1314
/// <summary>
@@ -19,8 +20,10 @@ public static bool IsWslPath(string path)
1920
{
2021
if (string.IsNullOrWhiteSpace(path)) return false;
2122

22-
return path.StartsWith(WslUncPrefix, StringComparison.OrdinalIgnoreCase) &&
23-
path.Length > WslUncPrefix.Length;
23+
return (path.StartsWith(WslUncPrefix, StringComparison.OrdinalIgnoreCase) &&
24+
path.Length > WslUncPrefix.Length) ||
25+
(path.StartsWith(WslLocalHostUncPrefix, StringComparison.OrdinalIgnoreCase) &&
26+
path.Length > WslLocalHostUncPrefix.Length);
2427
}
2528

2629
/// <summary>
@@ -54,7 +57,20 @@ public static string ConvertToDistroPath(string path, out string distribution)
5457
{
5558
if (!IsWslPath(path)) throw new ArgumentException("Must provide a WSL path", nameof(path));
5659

57-
int distroStart = WslUncPrefix.Length;
60+
int distroStart;
61+
if (path.StartsWith(WslUncPrefix, StringComparison.OrdinalIgnoreCase))
62+
{
63+
distroStart = WslUncPrefix.Length;
64+
}
65+
else if (path.StartsWith(WslLocalHostUncPrefix, StringComparison.OrdinalIgnoreCase))
66+
{
67+
distroStart = WslLocalHostUncPrefix.Length;
68+
}
69+
else
70+
{
71+
throw new Exception("Invalid WSL path prefix");
72+
}
73+
5874
int distroEnd = path.IndexOf('\\', distroStart);
5975

6076
if (distroEnd < 0) distroEnd = path.Length;

0 commit comments

Comments
 (0)