3
3
using System . ComponentModel ;
4
4
using System . Diagnostics ;
5
5
using System . Linq ;
6
+ using System . Runtime . InteropServices ;
6
7
using System . Text ;
7
8
using System . Text . RegularExpressions ;
8
9
using BenchmarkDotNet . Extensions ;
@@ -16,6 +17,8 @@ namespace BenchmarkDotNet.Toolchains.DotNetCli
16
17
[ PublicAPI ]
17
18
public static class DotNetCliCommandExecutor
18
19
{
20
+ internal static readonly Lazy < string > DefaultDotNetCliPath = new Lazy < string > ( GetDefaultDotNetCliPath ) ;
21
+
19
22
[ PublicAPI ]
20
23
public static DotNetCliCommandResult Execute ( DotNetCliCommand parameters )
21
24
{
@@ -82,7 +85,7 @@ internal static ProcessStartInfo BuildStartInfo(string customDotNetCliPath, stri
82
85
83
86
var startInfo = new ProcessStartInfo
84
87
{
85
- FileName = customDotNetCliPath ?? "dotnet" ,
88
+ FileName = customDotNetCliPath ?? DefaultDotNetCliPath . Value ,
86
89
WorkingDirectory = workingDirectory ,
87
90
Arguments = arguments ,
88
91
UseShellExecute = false ,
@@ -107,5 +110,26 @@ internal static ProcessStartInfo BuildStartInfo(string customDotNetCliPath, stri
107
110
108
111
return startInfo ;
109
112
}
113
+
114
+ private static string GetDefaultDotNetCliPath ( )
115
+ {
116
+ if ( ! Portability . RuntimeInformation . IsLinux ( ) )
117
+ return "dotnet" ;
118
+
119
+ using ( var parentProcess = Process . GetProcessById ( getppid ( ) ) )
120
+ {
121
+ string parentPath = parentProcess . MainModule ? . FileName ?? string . Empty ;
122
+ // sth like /snap/dotnet-sdk/112/dotnet and we should use the exact path instead of just "dotnet"
123
+ if ( parentPath . StartsWith ( "/snap/" , StringComparison . Ordinal ) )
124
+ {
125
+ return parentPath ;
126
+ }
127
+
128
+ return "dotnet" ;
129
+ }
130
+ }
131
+
132
+ [ DllImport ( "libc" ) ]
133
+ private static extern int getppid ( ) ;
110
134
}
111
135
}
0 commit comments