diff --git a/src/Platform/Microsoft.Testing.Platform.MSBuild/Tasks/StackTraceHelper.cs b/src/Platform/Microsoft.Testing.Platform.MSBuild/Tasks/StackTraceHelper.cs index 18179c2292..d96464bc6b 100644 --- a/src/Platform/Microsoft.Testing.Platform.MSBuild/Tasks/StackTraceHelper.cs +++ b/src/Platform/Microsoft.Testing.Platform.MSBuild/Tasks/StackTraceHelper.cs @@ -43,13 +43,13 @@ internal static bool TryFindLocationFromStackFrame(string? errorStackTrace, [Not private static bool TryGetStackFrameLocation(string stackFrame, out int line, [NotNullWhen(true)] out string? file, out string? place) { - InitializeRegex(); + Regex regex = GetOrCreateRegex(); // stack frame looks like this ' at Program.
$(String[] args) in S:\t\ConsoleApp81\ConsoleApp81\Program.cs:line 9' Match match; try { - match = s_regex.Match(stackFrame); + match = regex.Match(stackFrame); } catch (RegexMatchTimeoutException) { @@ -78,18 +78,12 @@ private static bool TryGetStackFrameLocation(string stackFrame, out int line, [N return hasLocation; } - [MemberNotNull(nameof(s_regex))] - private static void InitializeRegex() - { - if (s_regex is not null) - { - return; - } - - // Keep this location-only pattern because MSBuild only reports frames that can provide a file and line. - s_regex = new Regex( - StackTraceRegexHelper.CreateFrameRegexPattern(matchFramesWithoutLocation: false), - RegexOptions.Compiled, - StackTraceRegexHelper.MatchTimeout); - } + private static Regex GetOrCreateRegex() + => LazyInitializer.EnsureInitialized( + ref s_regex, + // Keep this location-only pattern because MSBuild only reports frames that can provide a file and line. + static () => new Regex( + StackTraceRegexHelper.CreateFrameRegexPattern(matchFramesWithoutLocation: false), + RegexOptions.Compiled, + StackTraceRegexHelper.MatchTimeout))!; } diff --git a/src/Platform/Microsoft.Testing.Platform/Helpers/StackTraceHelper.cs b/src/Platform/Microsoft.Testing.Platform/Helpers/StackTraceHelper.cs index 19c2c7907a..9fc840e1c7 100644 --- a/src/Platform/Microsoft.Testing.Platform/Helpers/StackTraceHelper.cs +++ b/src/Platform/Microsoft.Testing.Platform/Helpers/StackTraceHelper.cs @@ -16,20 +16,13 @@ internal static partial class StackTraceHelper #else private static Regex? s_regex; - [MemberNotNull(nameof(s_regex))] public static Regex GetFrameRegex() - { - if (s_regex is not null) - { - return s_regex; - } - - // Specifying no timeout, the regex is linear. And the timeout does not measure the regex only, but measures also any - // thread suspends, so the regex gets blamed incorrectly. - s_regex = new Regex( - StackTraceRegexHelper.CreateFrameRegexPattern(matchFramesWithoutLocation: true), - RegexOptions.Compiled | RegexOptions.ExplicitCapture); - return s_regex; - } + => LazyInitializer.EnsureInitialized( + ref s_regex, + // Specifying no timeout, the regex is linear. And the timeout does not measure the regex only, but measures also any + // thread suspends, so the regex gets blamed incorrectly. + static () => new Regex( + StackTraceRegexHelper.CreateFrameRegexPattern(matchFramesWithoutLocation: true), + RegexOptions.Compiled | RegexOptions.ExplicitCapture))!; #endif }