From bcd5fadaa89096c1cbae32789cdbca40ee98b0d6 Mon Sep 17 00:00:00 2001 From: filzrev <103790468+filzrev@users.noreply.github.com> Date: Sat, 18 Jul 2026 20:32:43 +0900 Subject: [PATCH 1/2] chore: fix error when running benchmark with file-based app --- .../Detectors/Cpu/Windows/MosCpuDetector.cs | 2 +- src/BenchmarkDotNet/Portability/RuntimeInformation.cs | 5 +++++ src/BenchmarkDotNet/Validators/ShadowCopyValidator.cs | 9 ++++++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/BenchmarkDotNet/Detectors/Cpu/Windows/MosCpuDetector.cs b/src/BenchmarkDotNet/Detectors/Cpu/Windows/MosCpuDetector.cs index 9cbe145650..f2ce01b750 100644 --- a/src/BenchmarkDotNet/Detectors/Cpu/Windows/MosCpuDetector.cs +++ b/src/BenchmarkDotNet/Detectors/Cpu/Windows/MosCpuDetector.cs @@ -10,7 +10,7 @@ namespace BenchmarkDotNet.Detectors.Cpu.Windows; internal class MosCpuDetector : ICpuDetector { [SupportedOSPlatform("windows")] - public bool IsApplicable() => OsDetector.IsWindows() && !RuntimeInformation.IsMono; + public bool IsApplicable() => OsDetector.IsWindows() && !RuntimeInformation.IsNativeAOT && !RuntimeInformation.IsMono; [SupportedOSPlatform("windows")] public CpuInfo? Detect() diff --git a/src/BenchmarkDotNet/Portability/RuntimeInformation.cs b/src/BenchmarkDotNet/Portability/RuntimeInformation.cs index 2e026d8f07..dd4e1f6022 100644 --- a/src/BenchmarkDotNet/Portability/RuntimeInformation.cs +++ b/src/BenchmarkDotNet/Portability/RuntimeInformation.cs @@ -76,6 +76,11 @@ public static bool IsNativeAOT && IsAot && !IsWasm && !IsMono; // Wasm and MonoAOTLLVM are also AOT + // File-based apps contains specific RuntimeHostConfigurationOptions. + // https://github.com/dotnet/dotnet/blob/v10.0.302/src/sdk/documentation/general/dotnet-run-file.md + public static bool IsFileBasedApp => AppContext.GetData("EntryPointFilePath") != null + && AppContext.GetData("EntryPointFileDirectoryPath") != null; + public static readonly bool IsRunningInContainer = string.Equals(Environment.GetEnvironmentVariable("DOTNET_RUNNING_IN_CONTAINER"), "true"); internal static string GetArchitecture() => GetCurrentPlatform().ToString(); diff --git a/src/BenchmarkDotNet/Validators/ShadowCopyValidator.cs b/src/BenchmarkDotNet/Validators/ShadowCopyValidator.cs index c5a0a3b373..97b09e6948 100644 --- a/src/BenchmarkDotNet/Validators/ShadowCopyValidator.cs +++ b/src/BenchmarkDotNet/Validators/ShadowCopyValidator.cs @@ -1,3 +1,4 @@ +using BenchmarkDotNet.Portability; using System.Reflection; namespace BenchmarkDotNet.Validators @@ -11,7 +12,12 @@ private ShadowCopyValidator() { } public bool TreatsWarningsAsErrors => false; public IAsyncEnumerable ValidateAsync(ValidationParameters validationParameters) - => validationParameters + { + // Skip validation when running on file-based app. because it's executed on temp directory. + if (RuntimeInformation.IsFileBasedApp) + return AsyncEnumerable.Empty(); + + return validationParameters .Benchmarks .Select(benchmark => benchmark.Descriptor.Type.GetTypeInfo().Assembly) .Distinct() @@ -22,5 +28,6 @@ public IAsyncEnumerable ValidateAsync(ValidationParameters vali $"Assembly {assembly} is located in temp. If you are running benchmarks from xUnit you need to disable shadow copy. It's not supported by design.") ) .ToAsyncEnumerable(); + } } } \ No newline at end of file From 49bcf238b7585f5090cbafb7c03ac84d5476e1f7 Mon Sep 17 00:00:00 2001 From: filzrev <103790468+filzrev@users.noreply.github.com> Date: Thu, 23 Jul 2026 13:42:30 +0900 Subject: [PATCH 2/2] chore: modify code to catch exception --- .../Detectors/Cpu/Windows/MosCpuDetector.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/BenchmarkDotNet/Detectors/Cpu/Windows/MosCpuDetector.cs b/src/BenchmarkDotNet/Detectors/Cpu/Windows/MosCpuDetector.cs index f2ce01b750..6c2a39b361 100644 --- a/src/BenchmarkDotNet/Detectors/Cpu/Windows/MosCpuDetector.cs +++ b/src/BenchmarkDotNet/Detectors/Cpu/Windows/MosCpuDetector.cs @@ -10,7 +10,7 @@ namespace BenchmarkDotNet.Detectors.Cpu.Windows; internal class MosCpuDetector : ICpuDetector { [SupportedOSPlatform("windows")] - public bool IsApplicable() => OsDetector.IsWindows() && !RuntimeInformation.IsNativeAOT && !RuntimeInformation.IsMono; + public bool IsApplicable() => OsDetector.IsWindows() && !RuntimeInformation.IsMono; [SupportedOSPlatform("windows")] public CpuInfo? Detect() @@ -24,8 +24,9 @@ internal class MosCpuDetector : ICpuDetector double maxFrequency = 0; double nominalFrequency = 0; - using (var mosProcessor = new ManagementObjectSearcher("SELECT * FROM Win32_Processor")) + try { + using var mosProcessor = new ManagementObjectSearcher("SELECT * FROM Win32_Processor"); foreach (var moProcessor in mosProcessor.Get().Cast()) { string? name = moProcessor[WmiCpuInfoKeyNames.Name]?.ToString(); @@ -45,6 +46,12 @@ internal class MosCpuDetector : ICpuDetector } } } + catch + { + // System.TypeInitializationException: The type initializer for 'System.Management.ManagementPath' threw an exception. + // --->System.NotSupportedException: Built-in COM has been disabled via a feature switch.See https://aka.ms/dotnet-illink/com for more information. + return null; + } string? processorName = processorModelNames.Count > 0 ? string.Join(", ", processorModelNames) : null; Frequency? maxFrequencyActual = maxFrequency > 0 && processorsCount > 0 @@ -64,4 +71,4 @@ internal class MosCpuDetector : ICpuDetector MaxFrequencyHz = maxFrequencyActual?.Hertz.RoundToLong() }; } -} \ No newline at end of file +}