Skip to content

Commit bafb08c

Browse files
committed
Extracting wsl version from binfmt files rather than "/proc/version".
1 parent e0e310e commit bafb08c

File tree

1 file changed

+8
-21
lines changed

1 file changed

+8
-21
lines changed

src/shared/Core/WslUtils.cs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public static class WslUtils
1818
private const string WslUncPrefix = @"\\wsl$\";
1919
private const string WslLocalHostUncPrefix = @"\\wsl.localhost\";
2020
private const string WslCommandName = "wsl.exe";
21-
private const string WslInteropEnvar = "WSL_INTEROP";
2221
private const string WslConfFilePath = "/etc/wsl.conf";
2322
private const string DefaultWslMountPrefix = "/mnt";
2423
private const string DefaultWslSysDriveMountName = "c";
@@ -61,29 +60,17 @@ private static int GetWslVersion(IEnvironment env, IFileSystem fs)
6160
return 0;
6261
}
6362

64-
// The WSL_INTEROP variable is set in WSL2 distributions
65-
if (env.Variables.TryGetValue(WslInteropEnvar, out _))
63+
const string procWslInteropPath = "/proc/sys/fs/binfmt_misc/WSLInterop";
64+
const string procWslInteropLatePath = "/proc/sys/fs/binfmt_misc/WSLInterop-Late";
65+
// The WSLInterop file exists in WSL1 distributions
66+
if (fs.FileExists(procWslInteropPath))
6667
{
67-
return 2;
68+
return 1;
6869
}
69-
70-
const string procVersionPath = "/proc/version";
71-
if (fs.FileExists(procVersionPath))
70+
// The WSLInterop-Late file exists in WSL2 distributions
71+
if (fs.FileExists(procWslInteropLatePath))
7272
{
73-
// Both WSL1 and WSL2 distributions include "[Mm]icrosoft" in the version string
74-
string procVersion = fs.ReadAllText(procVersionPath);
75-
if (!Regex.IsMatch(procVersion, "[Mm]icrosoft"))
76-
{
77-
return 0;
78-
}
79-
80-
// WSL2 distributions return "WSL2" in the version string
81-
if (Regex.IsMatch(procVersion, "wsl2", RegexOptions.IgnoreCase))
82-
{
83-
return 2;
84-
}
85-
86-
return 1;
73+
return 2;
8774
}
8875

8976
return 0;

0 commit comments

Comments
 (0)