Skip to content

Commit 69ae64b

Browse files
authored
Check for Muxer path relative to base directory (#47664)
When determining the muxer (dotnet) path, search relative to the app base directory first. In most scenarios, this should correspond to the dotnet.dll directory of <root>/sdk/<version> where the dotnet executable is under <root>. If it doesn't exist, fall back to the existing logic. With custom SDK search paths in global.json, the running dotnet process may not be the same as that corresponding to the running SDK. This updated logic finds the dotnet corresponding to the running SDK such that it can be properly propagated to anything launched via SDK commands (for example, build).
1 parent 110f80a commit 69ae64b

File tree

1 file changed

+35
-20
lines changed
  • src/Cli/Microsoft.DotNet.Cli.Utils

1 file changed

+35
-20
lines changed

src/Cli/Microsoft.DotNet.Cli.Utils/Muxer.cs

+35-20
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,46 @@ public string MuxerPath
3636

3737
public Muxer()
3838
{
39-
// Best-effort search for muxer.
40-
// SDK sets DOTNET_HOST_PATH as absolute path to current dotnet executable
39+
// Most scenarios are running dotnet.dll as the app
40+
// Root directory with muxer should be two above app base: <root>/sdk/<version>
41+
string? rootPath = Path.GetDirectoryName(Path.GetDirectoryName(AppContext.BaseDirectory.TrimEnd(Path.DirectorySeparatorChar)));
42+
if (rootPath is not null)
43+
{
44+
string muxerPathMaybe = Path.Combine(rootPath, $"{MuxerName}{FileNameSuffixes.CurrentPlatform.Exe}");
45+
if (File.Exists(muxerPathMaybe))
46+
{
47+
_muxerPath = muxerPathMaybe;
48+
}
49+
}
50+
51+
if (_muxerPath is null)
52+
{
53+
// Best-effort search for muxer.
54+
// SDK sets DOTNET_HOST_PATH as absolute path to current dotnet executable
4155
#if NET6_0_OR_GREATER
42-
string? processPath = Environment.ProcessPath;
56+
string? processPath = Environment.ProcessPath;
4357
#else
44-
string processPath = Process.GetCurrentProcess().MainModule.FileName;
58+
string processPath = Process.GetCurrentProcess().MainModule.FileName;
4559
#endif
4660

47-
// The current process should be dotnet in most normal scenarios except when dotnet.dll is loaded in a custom host like the testhost
48-
if (processPath is not null && !Path.GetFileNameWithoutExtension(processPath).Equals("dotnet", StringComparison.OrdinalIgnoreCase))
49-
{
50-
// SDK sets DOTNET_HOST_PATH as absolute path to current dotnet executable
51-
processPath = Environment.GetEnvironmentVariable("DOTNET_HOST_PATH");
52-
if (processPath is null)
53-
{
54-
// fallback to DOTNET_ROOT which typically holds some dotnet executable
55-
var root = Environment.GetEnvironmentVariable("DOTNET_ROOT");
56-
if (root is not null)
57-
{
58-
processPath = Path.Combine(root, $"dotnet{Constants.ExeSuffix}");
59-
}
60-
}
61-
}
61+
// The current process should be dotnet in most normal scenarios except when dotnet.dll is loaded in a custom host like the testhost
62+
if (processPath is not null && !Path.GetFileNameWithoutExtension(processPath).Equals("dotnet", StringComparison.OrdinalIgnoreCase))
63+
{
64+
// SDK sets DOTNET_HOST_PATH as absolute path to current dotnet executable
65+
processPath = Environment.GetEnvironmentVariable("DOTNET_HOST_PATH");
66+
if (processPath is null)
67+
{
68+
// fallback to DOTNET_ROOT which typically holds some dotnet executable
69+
var root = Environment.GetEnvironmentVariable("DOTNET_ROOT");
70+
if (root is not null)
71+
{
72+
processPath = Path.Combine(root, $"dotnet{Constants.ExeSuffix}");
73+
}
74+
}
75+
}
6276

63-
_muxerPath = processPath;
77+
_muxerPath = processPath;
78+
}
6479
}
6580

6681
public static string? GetDataFromAppDomain(string propertyName)

0 commit comments

Comments
 (0)