Skip to content

Fix S2234 Bug: AD0001 is thrown due to referencing a location outside of the current compilation #8577

@rjgotten

Description

@rjgotten

Description

Warning	AD0001	Analyzer 'SonarAnalyzer.Rules.CSharp.ParametersCorrectOrder' threw an exception of type 'System.ArgumentException' with message 'Reported diagnostic 'S2234' has a source location in file '[[redacted-project]]\Primitives\DateTimeRange.cs', which is not part of the compilation being analyzed.
Parameter name: diagnostic'.
Exception occurred with following context:
Compilation: [[redacted-project]].Tests
SyntaxTree: [[redacted-project]].Tests\Primitives\DateTimeRangeTests.cs
SyntaxNode: new DateTimeRange(end, start) [ObjectCreationExpressionSyntax]@[1395..1424) (37,21)-(37,50)

System.ArgumentException: Reported diagnostic 'S2234' has a source location in file '[[redacted-project]]\Primitives\DateTimeRange.cs', which is not part of the compilation being analyzed.
Parameter name: diagnostic
   at Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalysisContextHelpers.VerifyDiagnosticLocationInCompilation(String id, Location location, Compilation compilation)
   at Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalysisContextHelpers.VerifyDiagnosticLocationsInCompilation(Diagnostic diagnostic, Compilation compilation)
   at Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalysisContextHelpers.VerifyArguments(Diagnostic diagnostic, Compilation compilation, Func`3 isSupportedDiagnostic, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Diagnostics.SyntaxNodeAnalysisContext.ReportDiagnostic(Diagnostic diagnostic)
   at SonarAnalyzer.AnalysisContext.SonarReportingContextBase`1.ReportIssueCore(Diagnostic diagnostic)
   at SonarAnalyzer.Rules.ParametersCorrectOrderBase`1.<Initialize>b__6_0(SonarSyntaxNodeReportingContext c)
   at SonarAnalyzer.AnalysisContext.SonarCompilationStartAnalysisContext.<>c__DisplayClass10_0`1.<RegisterNodeAction>b__0(SyntaxNodeAnalysisContext x)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__49`1.<ExecuteSyntaxNodeAction>b__49_0(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info, CancellationToken cancellationToken)
-----

Suppress the following diagnostics to disable this analyzer: S2234	[[redacted-project]].Tests.csproj	[[redacted-project]].Tests	1

Repro steps

Impossible to provide as an inline code sample or as a sample project.
Issue occurs with a complex setup where the class being analyzed ends up not being part of the current partial compilation unit handled by Roslyn. This can likely only be accurately reproduced with the full solution which I am not at liberty to share access to.

This worked with SonarLint v7.4.0.80741 and SonarC# v9.12.0.78982.
It broke with SonarLint v7.5.0.82292 and SonarC# v9.15.0.81779.

So at least you have a bisection window.

Known workarounds

  • Suppress the rule wholesale via .editorconfig; situational suppressing via pragma or attribute does not work.
  • Downgrade SonarLint v7.5.0.82292 back to prior version v7.4.0.80741, which shipped with SonarC# v9.12.0.78982 and didn't exhibit this issue.

Related information

  • C#/VB.NET Plugins version: v9.15.0.81779 according to what shipped with SonarLint 7.5.0.82292
  • Visual Studio version: VS 2022 v17.8.5
  • MSBuild / dotnet version: dotnet SDK 8.0.11
  • SonarScanner for .NET version (if used): N/A
  • Operating System: Windows 11

Metadata

Metadata

Labels

BugExceptions and blocking issues during analysis.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions