Skip to content

Commit f3ac0c1

Browse files
authored
fix: osdetector logic for windows (#3193)
1 parent 7067dfd commit f3ac0c1

1 file changed

Lines changed: 20 additions & 11 deletions

File tree

src/BenchmarkDotNet/Detectors/OsDetector.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
using BenchmarkDotNet.Extensions;
21
using BenchmarkDotNet.Helpers;
32
using Microsoft.Win32;
43
using Perfolizer.Models;
5-
using System.Runtime.InteropServices;
64
using System.Runtime.Versioning;
7-
using static System.Runtime.InteropServices.RuntimeInformation;
5+
using System.Runtime.InteropServices;
6+
7+
#if NETSTANDARD
8+
using BenchmarkDotNet.Extensions;
9+
#endif
810

911
namespace BenchmarkDotNet.Detectors;
1012

@@ -52,14 +54,21 @@ private static OsInfo ResolveOs()
5254
}
5355
}
5456

55-
string operatingSystem = OSDescription;
56-
string operatingSystemVersion = Environment.OSVersion.ToString();
5757
if (IsWindows())
5858
{
59+
var osVersion = RuntimeInformation.OSDescription.Split(' ').LastOrDefault() ?? ""; // `Microsoft Windows 10.0.26200`
5960
int? ubr = GetWindowsUbr();
60-
if (ubr != null)
61-
operatingSystemVersion += $".{ubr}";
61+
return new OsInfo
62+
{
63+
Name = "Windows",
64+
Version = ubr == null
65+
? osVersion
66+
: $"{osVersion}.{ubr}",
67+
};
6268
}
69+
70+
string operatingSystem = RuntimeInformation.OSDescription;
71+
string operatingSystemVersion = Environment.OSVersion.ToString();
6372
return new OsInfo
6473
{
6574
Name = operatingSystem,
@@ -115,7 +124,7 @@ internal static bool IsWindows() =>
115124
#if NET6_0_OR_GREATER
116125
OperatingSystem.IsWindows(); // prefer linker-friendly OperatingSystem APIs
117126
#else
118-
IsOSPlatform(OSPlatform.Windows);
127+
RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
119128
#endif
120129

121130

@@ -124,7 +133,7 @@ internal static bool IsLinux() =>
124133
#if NET6_0_OR_GREATER
125134
OperatingSystem.IsLinux();
126135
#else
127-
IsOSPlatform(OSPlatform.Linux);
136+
RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
128137
#endif
129138

130139
[SupportedOSPlatformGuard("macos")]
@@ -133,7 +142,7 @@ internal static bool IsMacOS() =>
133142
#if NET6_0_OR_GREATER
134143
OperatingSystem.IsMacOS();
135144
#else
136-
IsOSPlatform(OSPlatform.OSX);
145+
RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
137146
#endif
138147

139148
[SupportedOSPlatformGuard("android")]
@@ -159,6 +168,6 @@ internal static bool IsTvOS() =>
159168
#if NET6_0_OR_GREATER
160169
OperatingSystem.IsTvOS();
161170
#else
162-
IsOSPlatform(OSPlatform.Create("TVOS"));
171+
RuntimeInformation.IsOSPlatform(OSPlatform.Create("TVOS"));
163172
#endif
164173
}

0 commit comments

Comments
 (0)