Do not crash on an invalid regex in the .editorconfig (MA0003, MA0104) - #1345
Merged
Conversation
- RegexCache validates the pattern once and caches the failure, so an invalid value in the .editorconfig no longer throws (AD0001) - The regexes use a finite timeout instead of Timeout.InfiniteTimeSpan - MA0104 falls back to its default pattern, MA0003 excludes no method - New rule MA0218 reports the invalid values
This was referenced Aug 26, 2026
Closed
Bump Meziantou.Analyzer from 3.0.103 to 3.0.182
Analogy-LogViewer/Analogy.LogViewer.OpenTelemetry#98
Closed
Closed
Closed
Closed
Bump Meziantou.Analyzer from 3.0.139 to 3.0.189
Analogy-LogViewer/Analogy.LogViewer.NLog.Targets#556
Closed
This was referenced Aug 31, 2026
Open
Open
Open
Open
Open
Open
Open
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1329
A regex coming from the
.editorconfigwas passed straight tonew Regex(...), with notry/catchand withTimeout.InfiniteTimeSpan. One typo in a shared.editorconfig(MA0104.namespaces_regex = [) crashed the analyzer withAD0001on every build, and a pattern with catastrophic backtracking would hang the compilation thread instead of failing.What changed
RegexCachenow owns both concerns:ArgumentExceptionis caught, and the failure (plus its message) is cached, so an invalid pattern never throws and is never re-parsed.Timeout.InfiniteTimeSpan, andIsMatchcatchesRegexMatchTimeoutException, so a catastrophic pattern degrades to "no match" instead of hanging or crashing.RegexOptions.NonBacktrackingwas not an option: the analyzer also targetsnetstandard2.0.Call sites
MA0104: an invalidnamespaces_regex(or the legacynamepaces_regex) falls back to the default^System($|\.).MA0003: an invalidexcluded_methods_regexexcludes no method.MA0003no longer passesRegexOptions.Compiledfor a pattern matched a handful of times per compilation:RegexCacheis a process-lifetime static, so the compiled entries were pinning emitted IL that is never reclaimed. The cache is still unbounded, but it now only holds interpretedRegexobjects keyed by the few patterns of an.editorconfig.New rule
MA0220- The configured regular expression is not validSilently ignoring the value would leave the user with a rule that does not behave as configured and no way to know why, so the invalid values are reported:
It is a dedicated analyzer with a single
RegisterCompilationAction, not a diagnostic reported byMA0003/MA0104themselves, because the error is not tied to a symbol or a node, reporting it from the rule analyzers would repeat it for every symbol or argument, andMA0104is disabled by default so its analyzer may never run. The action walks the syntax trees of the compilation (the options can differ per tree) and reports each distinct invalid(key, value)once. The keys come from the existingConfigurationDefinitionfields, nowinternalinstead ofprivate, so there is no second list of key names to keep in sync.Notes for the reviewer
.editorconfigis not part of the compilation. It shows as a project level warning, and it can be configured throughNoWarnor a global AnalyzerConfig (which is what the packagedconfiguration/*.editorconfigfiles are), but not through a[*.cs]section.Warning, enabled by default. The issue suggested an informational diagnostic, but the closest precedent in the repository,MA0125(LoggerParameterType_InvalidType), is a default-on warning, and anInfodiagnostic without a location is nearly invisible in a normal build. Happy to switch it toInfo.RegexConfigurationsarray is already the extension point.Int32_ExcludedMethod_ShouldNotReportDiagnostictest was passing vacuously (its source reported nothing under the defaultexpression_kinds, so the exclusion regex was never exercised); it now setsMA0003.expression_kinds = numeric.Validation
dotnet run --project src/DocumentationGeneratorreports no further change.dotnet build: 0 warnings.dotnet test: 18754 tests pass on Roslyn 4.8, 4.14, 5.0, 5.6 and 5.9.