@@ -18,44 +18,59 @@ internal static class DotnetMuxer
1818 /// <exception cref="InvalidOperationException">Thrown when the dotnet executable cannot be located.</exception>
1919 public static string GetMuxerPath ( )
2020 {
21- string muxerPath ;
22-
2321 // Most scenarios are running dotnet.dll as the app
2422 // Root directory with muxer should be two above app base: <root>/sdk/<version>
2523 string rootPath = Path . GetDirectoryName ( Path . GetDirectoryName ( AppContext . BaseDirectory . TrimEnd ( Path . DirectorySeparatorChar ) ) ) ;
2624 if ( rootPath is not null )
2725 {
28- muxerPath = Path . Combine ( rootPath , $ "{ _muxerName } { _exeSuffix } ") ;
26+ string muxerPath = Path . Combine ( rootPath , $ "{ _muxerName } { _exeSuffix } ") ;
2927 if ( File . Exists ( muxerPath ) )
3028 {
3129 return muxerPath ;
3230 }
3331 }
3432
35- // Best-effort search for muxer.
36- muxerPath = Environment . ProcessPath ;
33+ // Check if the current process is dotnet
34+ // In most scenarios, the process is dotnet, except when dotnet.dll is loaded
35+ // in a custom host like testhost, in which case we fall through to other checks
36+ string processPath = Environment . ProcessPath ;
37+ if ( processPath is not null && Path . GetFileNameWithoutExtension ( processPath ) . Equals ( _muxerName , StringComparison . OrdinalIgnoreCase ) )
38+ {
39+ return processPath ;
40+ }
3741
38- // The current process should be dotnet in most normal scenarios except when dotnet.dll is loaded in a custom host like the testhost
39- if ( muxerPath is not null && ! Path . GetFileNameWithoutExtension ( muxerPath ) . Equals ( "dotnet" , StringComparison . OrdinalIgnoreCase ) )
42+ // SDK sets DOTNET_HOST_PATH as absolute path to current dotnet executable
43+ string envPath = Environment . GetEnvironmentVariable ( "DOTNET_HOST_PATH" ) ;
44+ if ( envPath is not null )
4045 {
41- // SDK sets DOTNET_HOST_PATH as absolute path to current dotnet executable
42- muxerPath = Environment . GetEnvironmentVariable ( "DOTNET_HOST_PATH" ) ;
43- if ( muxerPath is null )
46+ return envPath ;
47+ }
48+
49+ // Fallback to DOTNET_ROOT which typically holds some dotnet executable
50+ string dotnetRoot = Environment . GetEnvironmentVariable ( "DOTNET_ROOT" ) ;
51+ if ( dotnetRoot is not null )
52+ {
53+ string muxerPath = Path . Combine ( dotnetRoot , $ "{ _muxerName } { _exeSuffix } ") ;
54+ if ( File . Exists ( muxerPath ) )
4455 {
45- // fallback to DOTNET_ROOT which typically holds some dotnet executable
46- string root = Environment . GetEnvironmentVariable ( "DOTNET_ROOT" ) ;
47- if ( root is not null )
48- {
49- muxerPath = Path . Combine ( root , $ "dotnet{ _exeSuffix } ") ;
50- }
56+ return muxerPath ;
5157 }
5258 }
5359
54- if ( muxerPath is null )
60+ // Search PATH environment variable
61+ string pathVariable = Environment . GetEnvironmentVariable ( "PATH" ) ;
62+ if ( pathVariable is not null )
5563 {
56- throw new InvalidOperationException ( "Unable to locate dotnet multiplexer" ) ;
64+ foreach ( string directory in pathVariable . Split ( Path . PathSeparator ) )
65+ {
66+ string potentialPath = Path . Combine ( directory , $ "{ _muxerName } { _exeSuffix } ") ;
67+ if ( File . Exists ( potentialPath ) )
68+ {
69+ return potentialPath ;
70+ }
71+ }
5772 }
5873
59- return muxerPath ;
74+ throw new InvalidOperationException ( $ "Unable to locate { _muxerName } multiplexer" ) ;
6075 }
6176}
0 commit comments