Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 8 additions & 21 deletions src/shared/Core/WslUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public static class WslUtils
private const string WslUncPrefix = @"\\wsl$\";
private const string WslLocalHostUncPrefix = @"\\wsl.localhost\";
private const string WslCommandName = "wsl.exe";
private const string WslInteropEnvar = "WSL_INTEROP";
private const string WslConfFilePath = "/etc/wsl.conf";
private const string DefaultWslMountPrefix = "/mnt";
private const string DefaultWslSysDriveMountName = "c";
Expand Down Expand Up @@ -61,29 +60,17 @@ private static int GetWslVersion(IEnvironment env, IFileSystem fs)
return 0;
}

// The WSL_INTEROP variable is set in WSL2 distributions
if (env.Variables.TryGetValue(WslInteropEnvar, out _))
const string procWslInteropPath = "/proc/sys/fs/binfmt_misc/WSLInterop";
const string procWslInteropLatePath = "/proc/sys/fs/binfmt_misc/WSLInterop-Late";
// The WSLInterop file exists in WSL1 distributions
if (fs.FileExists(procWslInteropPath))
{
return 2;
return 1;
}

const string procVersionPath = "/proc/version";
if (fs.FileExists(procVersionPath))
// The WSLInterop-Late file exists in WSL2 distributions
if (fs.FileExists(procWslInteropLatePath))
{
// Both WSL1 and WSL2 distributions include "[Mm]icrosoft" in the version string
string procVersion = fs.ReadAllText(procVersionPath);
if (!Regex.IsMatch(procVersion, "[Mm]icrosoft"))
{
return 0;
}

// WSL2 distributions return "WSL2" in the version string
if (Regex.IsMatch(procVersion, "wsl2", RegexOptions.IgnoreCase))
{
return 2;
}

return 1;
return 2;
}

return 0;
Expand Down