diff --git a/src/coreclr/System.Private.CoreLib/src/Internal/Runtime/InteropServices/InMemoryAssemblyLoader.cs b/src/coreclr/System.Private.CoreLib/src/Internal/Runtime/InteropServices/InMemoryAssemblyLoader.cs index d8eea0900de438..496d0e36895ebf 100644 --- a/src/coreclr/System.Private.CoreLib/src/Internal/Runtime/InteropServices/InMemoryAssemblyLoader.cs +++ b/src/coreclr/System.Private.CoreLib/src/Internal/Runtime/InteropServices/InMemoryAssemblyLoader.cs @@ -16,8 +16,7 @@ namespace Internal.Runtime.InteropServices internal static class InMemoryAssemblyLoader { [FeatureSwitchDefinition("System.Runtime.InteropServices.EnableCppCLIHostActivation")] - private static bool IsSupported { get; } = InitializeIsSupported(); - private static bool InitializeIsSupported() => AppContext.TryGetSwitch("System.Runtime.InteropServices.EnableCppCLIHostActivation", out bool isSupported) ? isSupported : true; + private static bool IsSupported { get; } = AppContext.TryGetSwitch("System.Runtime.InteropServices.EnableCppCLIHostActivation", out bool isSupported) ? isSupported : true; /// /// Loads an assembly that has already been loaded into memory by the OS loader as a native module diff --git a/src/coreclr/System.Private.CoreLib/src/System/Diagnostics/StackFrameHelper.cs b/src/coreclr/System.Private.CoreLib/src/System/Diagnostics/StackFrameHelper.cs index 763259a20a257e..d51f5d13f87add 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Diagnostics/StackFrameHelper.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Diagnostics/StackFrameHelper.cs @@ -90,12 +90,12 @@ public StackFrameHelper() // rgiLineNumber and rgiColumnNumber fields using the portable PDB reader if not already // done by GetStackFramesInternal (on Windows for old PDB format). // - internal void InitializeSourceInfo(bool fNeedFileInfo, Exception? exception) { StackTrace.GetStackFramesInternal(this, fNeedFileInfo, exception); - if (!fNeedFileInfo) + + if (!StackTrace.IsLineNumberSupported || !fNeedFileInfo) return; // Check if this function is being reentered because of an exception in the code below diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/Meter.cs b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/Meter.cs index ccacb53e5f8460..35c4d953140b3a 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/Meter.cs +++ b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Metrics/Meter.cs @@ -22,10 +22,7 @@ public class Meter : IDisposable internal bool Disposed { get; private set; } [FeatureSwitchDefinition("System.Diagnostics.Metrics.Meter.IsSupported")] - internal static bool IsSupported { get; } = InitializeIsSupported(); - - private static bool InitializeIsSupported() => - AppContext.TryGetSwitch("System.Diagnostics.Metrics.Meter.IsSupported", out bool isSupported) ? isSupported : true; + internal static bool IsSupported { get; } = AppContext.TryGetSwitch("System.Diagnostics.Metrics.Meter.IsSupported", out bool isSupported) ? isSupported : true; /// /// Initialize a new instance of the Meter using the . diff --git a/src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs b/src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs index df4306da1b7ec8..7ee23e01a469db 100644 --- a/src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs +++ b/src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs @@ -30,9 +30,7 @@ internal static partial class ComponentActivator private const int HostFeatureDisabled = unchecked((int)0x800080a7); [FeatureSwitchDefinition("System.Runtime.InteropServices.EnableConsumingManagedCodeFromNativeHosting")] - private static bool IsSupported { get; } = InitializeIsSupported(); - - private static bool InitializeIsSupported() => AppContext.TryGetSwitch("System.Runtime.InteropServices.EnableConsumingManagedCodeFromNativeHosting", out bool isSupported) ? isSupported : true; + private static bool IsSupported { get; } = AppContext.TryGetSwitch("System.Runtime.InteropServices.EnableConsumingManagedCodeFromNativeHosting", out bool isSupported) ? isSupported : true; public delegate int ComponentEntryPoint(IntPtr args, int sizeBytes); diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Debugger.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Debugger.cs index 27ce5dc0095bd5..4b37817fbf80f1 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Debugger.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Debugger.cs @@ -29,13 +29,6 @@ public static void BreakForUserUnhandledException(Exception exception) } [FeatureSwitchDefinition("System.Diagnostics.Debugger.IsSupported")] - internal static bool IsSupported { get; } = InitializeIsSupported(); - - private static bool InitializeIsSupported() - { - return AppContext.TryGetSwitch("System.Diagnostics.Debugger.IsSupported", out bool isSupported) - ? isSupported - : true; - } + internal static bool IsSupported { get; } = AppContext.TryGetSwitch("System.Diagnostics.Debugger.IsSupported", out bool isSupported) ? isSupported : true; } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/StackTrace.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/StackTrace.cs index aebe43c1640289..dae3e70156c9ed 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/StackTrace.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/StackTrace.cs @@ -18,10 +18,10 @@ namespace System.Diagnostics public partial class StackTrace { [FeatureSwitchDefinition("System.Diagnostics.StackTrace.IsSupported")] - internal static bool IsSupported { get; } = InitializeIsSupported(); + internal static bool IsSupported { get; } = AppContext.TryGetSwitch("System.Diagnostics.StackTrace.IsSupported", out bool isSupported) ? isSupported : true; - private static bool InitializeIsSupported() => - AppContext.TryGetSwitch("System.Diagnostics.StackTrace.IsSupported", out bool isSupported) ? isSupported : true; + [FeatureSwitchDefinition("System.Diagnostics.StackTrace.IsLineNumberSupported")] + internal static bool IsLineNumberSupported { get; } = AppContext.TryGetSwitch("System.Diagnostics.StackTrace.IsLineNumberSupported", out bool isSupported) ? isSupported : true; public const int METHODS_TO_SKIP = 0; diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs index bf527c3acbe0bc..2571a3a6229f08 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs @@ -277,16 +277,10 @@ public partial class EventSource : IDisposable private static readonly bool AllowDuplicateSourceNames = AppContext.TryGetSwitch(DuplicateSourceNamesSwitch, out bool isEnabled) ? isEnabled : false; [FeatureSwitchDefinition("System.Diagnostics.Tracing.EventSource.IsSupported")] - internal static bool IsSupported { get; } = InitializeIsSupported(); - - private static bool InitializeIsSupported() => - AppContext.TryGetSwitch("System.Diagnostics.Tracing.EventSource.IsSupported", out bool isSupported) ? isSupported : true; + internal static bool IsSupported { get; } = AppContext.TryGetSwitch("System.Diagnostics.Tracing.EventSource.IsSupported", out bool isSupported) ? isSupported : true; [FeatureSwitchDefinition("System.Diagnostics.Metrics.Meter.IsSupported")] - internal static bool IsMeterSupported { get; } = InitializeIsMeterSupported(); - - private static bool InitializeIsMeterSupported() => - AppContext.TryGetSwitch("System.Diagnostics.Metrics.Meter.IsSupported", out bool isSupported) ? isSupported : true; + internal static bool IsMeterSupported { get; } = AppContext.TryGetSwitch("System.Diagnostics.Metrics.Meter.IsSupported", out bool isSupported) ? isSupported : true; #if FEATURE_EVENTSOURCE_XPLAT #pragma warning disable CA1823 // field is used to keep listener alive diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/TrimmingTests/BrowserDoesNotIncludeStackTraceSymbols.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/TrimmingTests/BrowserDoesNotIncludeStackTraceSymbols.cs new file mode 100644 index 00000000000000..792eba9e8c0806 --- /dev/null +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/TrimmingTests/BrowserDoesNotIncludeStackTraceSymbols.cs @@ -0,0 +1,40 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Linq; +using System.Reflection; + + +/// +/// Tests that StackTraceSymbols and dependencies are not included in a trimmed app targeting browser. +/// The idea is that the runtime should not depend on these types when running in a browser with DebugSymbols == false. +/// Motivation: application size. +/// +class Program +{ + static int Main(string[] args) + { + var symbolsType = GetTypeByName("StackTraceSymbols"); + if (symbolsType != null) + { + Console.WriteLine("Failed StackTraceSymbols"); + return -1; + } + return 100; + } + + // make reflection trimmer incompatible + static Type GetTypeByName(string typeName) + { + foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) + { + var type = assembly.GetType("System.Diagnostics." + typeName); + if (type != null) + { + return type; + } + } + return null; + } +} \ No newline at end of file diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/TrimmingTests/System.Runtime.TrimmingTests.proj b/src/libraries/System.Runtime/tests/System.Runtime.Tests/TrimmingTests/System.Runtime.TrimmingTests.proj index 4e654c7e5cc1d6..31e6d7814c97bd 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/TrimmingTests/System.Runtime.TrimmingTests.proj +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/TrimmingTests/System.Runtime.TrimmingTests.proj @@ -81,6 +81,9 @@ + + DebugSymbols + diff --git a/src/mono/nuget/Microsoft.NET.Sdk.WebAssembly.Pack/build/Microsoft.NET.Sdk.WebAssembly.Browser.targets b/src/mono/nuget/Microsoft.NET.Sdk.WebAssembly.Pack/build/Microsoft.NET.Sdk.WebAssembly.Browser.targets index 5b8b38e6c5c825..b9a8373cf97696 100644 --- a/src/mono/nuget/Microsoft.NET.Sdk.WebAssembly.Pack/build/Microsoft.NET.Sdk.WebAssembly.Browser.targets +++ b/src/mono/nuget/Microsoft.NET.Sdk.WebAssembly.Pack/build/Microsoft.NET.Sdk.WebAssembly.Browser.targets @@ -230,9 +230,17 @@ Copyright (c) .NET Foundation. All rights reserved. <_WasmBundlerFriendlyBootConfig>$(WasmBundlerFriendlyBootConfig) <_WasmBundlerFriendlyBootConfig Condition="'$(_WasmBundlerFriendlyBootConfig)' == ''">false - + $(OutputPath)$(PublishDirName)\ + + + + false + + + +