Skip to content

Commit 8c28c87

Browse files
authored
add basic snap support (#1652)
1 parent b4e2b69 commit 8c28c87

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/BenchmarkDotNet/Toolchains/DotNetCli/DotNetCliCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class DotNetCliCommand
3131
public DotNetCliCommand(string cliPath, string arguments, GenerateResult generateResult, ILogger logger,
3232
BuildPartition buildPartition, IReadOnlyList<EnvironmentVariable> environmentVariables, TimeSpan timeout)
3333
{
34-
CliPath = cliPath;
34+
CliPath = cliPath ?? DotNetCliCommandExecutor.DefaultDotNetCliPath.Value;
3535
Arguments = arguments;
3636
GenerateResult = generateResult;
3737
Logger = logger;

src/BenchmarkDotNet/Toolchains/DotNetCli/DotNetCliCommandExecutor.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.ComponentModel;
44
using System.Diagnostics;
55
using System.Linq;
6+
using System.Runtime.InteropServices;
67
using System.Text;
78
using System.Text.RegularExpressions;
89
using BenchmarkDotNet.Extensions;
@@ -16,6 +17,8 @@ namespace BenchmarkDotNet.Toolchains.DotNetCli
1617
[PublicAPI]
1718
public static class DotNetCliCommandExecutor
1819
{
20+
internal static readonly Lazy<string> DefaultDotNetCliPath = new Lazy<string>(GetDefaultDotNetCliPath);
21+
1922
[PublicAPI]
2023
public static DotNetCliCommandResult Execute(DotNetCliCommand parameters)
2124
{
@@ -82,7 +85,7 @@ internal static ProcessStartInfo BuildStartInfo(string customDotNetCliPath, stri
8285

8386
var startInfo = new ProcessStartInfo
8487
{
85-
FileName = customDotNetCliPath ?? "dotnet",
88+
FileName = customDotNetCliPath ?? DefaultDotNetCliPath.Value,
8689
WorkingDirectory = workingDirectory,
8790
Arguments = arguments,
8891
UseShellExecute = false,
@@ -107,5 +110,26 @@ internal static ProcessStartInfo BuildStartInfo(string customDotNetCliPath, stri
107110

108111
return startInfo;
109112
}
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();
110134
}
111135
}

0 commit comments

Comments
 (0)