Skip to content

Commit 9efa2c8

Browse files
committed
Fix IsNetCore check for single-file apps without AOT, and IsNativeAOT check.
1 parent ab703aa commit 9efa2c8

1 file changed

Lines changed: 24 additions & 12 deletions

File tree

src/BenchmarkDotNet/Portability/RuntimeInformation.cs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,13 @@ internal static class RuntimeInformation
5050

5151
public static readonly bool IsNetNative = FrameworkDescription.StartsWith(".NET Native", StringComparison.OrdinalIgnoreCase);
5252

53-
public static readonly bool IsNetCore =
54-
((Environment.Version.Major >= 5) || FrameworkDescription.StartsWith(".NET Core", StringComparison.OrdinalIgnoreCase))
55-
&& !string.IsNullOrEmpty(typeof(object).Assembly.Location);
56-
5753
#if NET6_0_OR_GREATER
5854
[System.Runtime.Versioning.SupportedOSPlatformGuard("browser")]
5955
public static readonly bool IsWasm = OperatingSystem.IsBrowser();
6056
#else
6157
public static readonly bool IsWasm = IsOSPlatform(OSPlatform.Create("BROWSER"));
6258
#endif
6359

64-
public static readonly bool IsNativeAOT =
65-
Environment.Version.Major >= 5
66-
&& string.IsNullOrEmpty(typeof(object).Assembly.Location) // it's merged to a single .exe and .Location returns null
67-
&& !IsWasm; // Wasm also returns "" for assembly locations
68-
6960
#if NETSTANDARD2_0
7061
public static readonly bool IsAot = IsAotMethod();
7162

@@ -88,6 +79,16 @@ private static bool IsAotMethod()
8879
public static readonly bool IsAot = !System.Runtime.CompilerServices.RuntimeFeature.IsDynamicCodeCompiled;
8980
#endif
9081

82+
public static bool IsNetCore
83+
=> ((Environment.Version.Major >= 5) || FrameworkDescription.StartsWith(".NET Core", StringComparison.OrdinalIgnoreCase))
84+
&& !IsAot;
85+
86+
public static bool IsNativeAOT
87+
=> Environment.Version.Major >= 5
88+
&& IsAot
89+
&& !IsWasm && !IsMono; // Wasm and MonoAOTLLVM are also AOT
90+
91+
9192
public static readonly bool IsTieredJitEnabled =
9293
IsNetCore
9394
&& (Environment.Version.Major < 3
@@ -173,10 +174,21 @@ private static string GetNetCoreVersion()
173174
}
174175
else
175176
{
176-
var coreclrAssemblyInfo = FileVersionInfo.GetVersionInfo(typeof(object).GetTypeInfo().Assembly.Location);
177-
var corefxAssemblyInfo = FileVersionInfo.GetVersionInfo(typeof(Regex).GetTypeInfo().Assembly.Location);
177+
string coreclrLocation = typeof(object).GetTypeInfo().Assembly.Location;
178+
string corefxLocation = typeof(Regex).GetTypeInfo().Assembly.Location;
179+
180+
// Handle cases where assembly location is empty (e.g. single-file publish, AOT, some test runners)
181+
if (string.IsNullOrEmpty(coreclrLocation) || string.IsNullOrEmpty(corefxLocation))
182+
{
183+
return CoreRuntime.TryGetVersion(out var ver) && ver.Major >= 5
184+
? $".NET {ver} (assembly location unavailable)"
185+
: $".NET Core {ver?.ToString() ?? Unknown} (assembly location unavailable)";
186+
}
187+
188+
var coreclrAssemblyInfo = FileVersionInfo.GetVersionInfo(coreclrLocation);
189+
var corefxAssemblyInfo = FileVersionInfo.GetVersionInfo(corefxLocation);
178190

179-
if (CoreRuntime.TryGetVersion(out var version) && version >= new Version(5, 0))
191+
if (CoreRuntime.TryGetVersion(out var version) && version.Major >= 5)
180192
{
181193
// after the merge of dotnet/corefx and dotnet/coreclr into dotnet/runtime the version should always be the same
182194
Debug.Assert(coreclrAssemblyInfo.FileVersion == corefxAssemblyInfo.FileVersion);

0 commit comments

Comments
 (0)