Skip to content
Merged
Changes from 1 commit
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
51 changes: 31 additions & 20 deletions src/Cli/Microsoft.DotNet.Cli.Utils/Muxer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,42 @@ public string MuxerPath

public Muxer()
{
// Best-effort search for muxer.
// SDK sets DOTNET_HOST_PATH as absolute path to current dotnet executable
// Most scenarios are running dotnet.dll as the app
// Root directory with muxer should be two above app base: <root>/sdk/<version>
string? rootPath = Path.GetDirectoryName(Path.GetDirectoryName(AppContext.BaseDirectory.TrimEnd(Path.DirectorySeparatorChar)));
if (rootPath is not null)
{
_muxerPath = Path.Combine(rootPath, $"{MuxerName}{FileNameSuffixes.CurrentPlatform.Exe}");
}

if (_muxerPath is null || !File.Exists(_muxerPath))
{
// Best-effort search for muxer.
// SDK sets DOTNET_HOST_PATH as absolute path to current dotnet executable
#if NET6_0_OR_GREATER
string? processPath = Environment.ProcessPath;
string? processPath = Environment.ProcessPath;
#else
string processPath = Process.GetCurrentProcess().MainModule.FileName;
string processPath = Process.GetCurrentProcess().MainModule.FileName;
#endif

// The current process should be dotnet in most normal scenarios except when dotnet.dll is loaded in a custom host like the testhost
if (processPath is not null && !Path.GetFileNameWithoutExtension(processPath).Equals("dotnet", StringComparison.OrdinalIgnoreCase))
{
// SDK sets DOTNET_HOST_PATH as absolute path to current dotnet executable
processPath = Environment.GetEnvironmentVariable("DOTNET_HOST_PATH");
if (processPath is null)
{
// fallback to DOTNET_ROOT which typically holds some dotnet executable
var root = Environment.GetEnvironmentVariable("DOTNET_ROOT");
if (root is not null)
{
processPath = Path.Combine(root, $"dotnet{Constants.ExeSuffix}");
}
}
}
// The current process should be dotnet in most normal scenarios except when dotnet.dll is loaded in a custom host like the testhost
if (processPath is not null && !Path.GetFileNameWithoutExtension(processPath).Equals("dotnet", StringComparison.OrdinalIgnoreCase))
{
// SDK sets DOTNET_HOST_PATH as absolute path to current dotnet executable
processPath = Environment.GetEnvironmentVariable("DOTNET_HOST_PATH");
if (processPath is null)
{
// fallback to DOTNET_ROOT which typically holds some dotnet executable
var root = Environment.GetEnvironmentVariable("DOTNET_ROOT");
if (root is not null)
{
processPath = Path.Combine(root, $"dotnet{Constants.ExeSuffix}");
}
}
}

_muxerPath = processPath;
_muxerPath = processPath;
}
}

public static string? GetDataFromAppDomain(string propertyName)
Expand Down
Loading