Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSharpMatchFolderAndNamespaceDiagnosticAnalyzer throws exception #51602

Closed
jmarolf opened this issue Mar 2, 2021 · 9 comments · Fixed by #52912
Closed

CSharpMatchFolderAndNamespaceDiagnosticAnalyzer throws exception #51602

jmarolf opened this issue Mar 2, 2021 · 9 comments · Fixed by #52912
Assignees
Labels
Area-Analyzers Bug help wanted The issue is "up for grabs" - add a comment if you are interested in working on it
Milestone

Comments

@jmarolf
Copy link
Contributor

jmarolf commented Mar 2, 2021

installed .NET 6 preview SDK

Opened default console app in VS 2019.10 Preview 1

Severity	Code	Description	Project	File	Line	Suppression State
Error	AD0001	Analyzer 'Microsoft.CodeAnalysis.CSharp.Analyzers.MatchFolderAndNamespace.CSharpMatchFolderAndNamespaceDiagnosticAnalyzer' threw an exception of type 'System.IndexOutOfRangeException' with message 'Index was outside the bounds of the array.'.
Exception occurred with following context:
Compilation: net6-analyzers-bug
SyntaxTree: C:\source\jmarolf\scratch\net6-analyzers-bug\Program.cs
SyntaxNode: namespace ConsoleCore { class Program ... [NamespaceDeclarationSyntax]@[15..173) (1,0)-(9,1)

System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at Roslyn.Utilities.PathUtilities.GetRelativePath(String directory, String fullPath)
   at Microsoft.CodeAnalysis.Analyzers.MatchFolderAndNamespace.AbstractMatchFolderAndNamespaceDiagnosticAnalyzer`1.IsFileAndNamespaceMismatch(TNamespaceSyntax namespaceDeclaration, String rootNamespace, String projectDir, String currentNamespace, String& targetNamespace)
   at Microsoft.CodeAnalysis.Analyzers.MatchFolderAndNamespace.AbstractMatchFolderAndNamespaceDiagnosticAnalyzer`1.AnalyzeNamespaceNode(SyntaxNodeAnalysisContext context)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__62`1.<ExecuteSyntaxNodeAction>b__62_0(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Analyzers untriaged Issues and PRs which have not yet been triaged by a lead labels Mar 2, 2021
@CyrusNajmabadi
Copy link
Member

Looks like a bug in PathUtilities.GetRelativePath. That function should never throw an index out of range.

@jinujoseph jinujoseph added Bug and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Mar 11, 2021
@jinujoseph jinujoseph added this to the 16.10 milestone Mar 11, 2021
@jinujoseph jinujoseph added the help wanted The issue is "up for grabs" - add a comment if you are interested in working on it label Mar 11, 2021
@benaadams
Copy link
Member

Have 20 of these errors since upgrading

Severity	Code	Description	Project	File	Line	Suppression State
Warning	AD0001	Analyzer 'Microsoft.CodeAnalysis.CSharp.Analyzers.MatchFolderAndNamespace.CSharpMatchFolderAndNamespaceDiagnosticAnalyzer' threw an exception of type 'System.IndexOutOfRangeException' with message 'Index was outside the bounds of the array.'.
Exception occurred with following context:
Compilation: AoA.Compressor
SyntaxTree: D:\Work\AoA\tools\AoA.Compressor\Program.cs
SyntaxNode: namespace AoA.Compressor { class ... [NamespaceDeclarationSyntax]@[65..955) (4,0)-(30,1)

System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at Roslyn.Utilities.PathUtilities.GetRelativePath(String directory, String fullPath)
   at Microsoft.CodeAnalysis.Analyzers.MatchFolderAndNamespace.AbstractMatchFolderAndNamespaceDiagnosticAnalyzer`1.IsFileAndNamespaceMismatch(TNamespaceSyntax namespaceDeclaration, String rootNamespace, String projectDir, String currentNamespace, String& targetNamespace)
   at Microsoft.CodeAnalysis.Analyzers.MatchFolderAndNamespace.AbstractMatchFolderAndNamespaceDiagnosticAnalyzer`1.AnalyzeNamespaceNode(SyntaxNodeAnalysisContext context)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__62`1.<ExecuteSyntaxNodeAction>b__62_0(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)
-----

image

@benaadams
Copy link
Member

Doesn't looks like the method has changed in 4 years, so is a bit odd.

@tmat
Copy link
Member

tmat commented Mar 24, 2021

The issue is with the following inputs:
PathUtilities.GetRelativePath(@"C:\A\B\", @"C:\A\B")

@benaadams
Copy link
Member

I get 594 of these errors when opening dotnet/aspnetcore/Servers/Kestrel

image

jmarolf added a commit to jmarolf/roslyn that referenced this issue Apr 26, 2021
jmarolf added a commit to jmarolf/roslyn that referenced this issue May 30, 2021
@gakera
Copy link

gakera commented Jun 24, 2021

I'm getting this error on Microsoft Visual Studio Enterprise 2022 Preview (64-bit), how do apply the fix?

@ryzngard
Copy link
Contributor

@gakera the fix will be coming in Preview 2 for VS 2022

@sharwell sharwell modified the milestones: 16.10, 17.0.P2 Aug 13, 2021
@george-tsiros
Copy link

george-tsiros commented Oct 10, 2021

For what it's worth, I am getting internal errors like this in preview 4.1

Severity	Code	Description	Project	File	Line	Suppression State
Warning	AD0001	Analyzer 'Microsoft.NetCore.Analyzers.Runtime.DetectPreviewFeatureAnalyzer' threw an exception of type 'System.ArgumentNullException' with message 'Value cannot be null.
Parameter name: key'.
Exception occurred with following context:
Compilation: Engine5
IOperation: PropertyReference
SyntaxTree: C:\projects\source\Engine\Engine5\GlWindowBase.cs
SyntaxNode: Window [IdentifierNameSyntax]@[621..627) (29,8)-(29,14)

System.ArgumentNullException: Value cannot be null.
Parameter name: key
   at System.Collections.Concurrent.ConcurrentDictionary`2.TryGetValue(TKey key, TValue& value)
   at Microsoft.NetCore.Analyzers.Runtime.DetectPreviewFeatureAnalyzer.SymbolIsAnnotatedAsPreview(ISymbol symbol, ConcurrentDictionary`2 requiresPreviewFeaturesSymbols, INamedTypeSymbol previewFeatureAttribute)
   at Microsoft.NetCore.Analyzers.Runtime.DetectPreviewFeatureAnalyzer.SymbolIsAnnotatedOrUsesPreviewTypes(ISymbol symbol, ConcurrentDictionary`2 requiresPreviewFeaturesSymbols, INamedTypeSymbol previewFeatureAttributeSymbol, ISymbol& referencedPreviewSymbol)
   at Microsoft.NetCore.Analyzers.Runtime.DetectPreviewFeatureAnalyzer.OperationUsesPreviewFeatures(OperationAnalysisContext context, ConcurrentDictionary`2 requiresPreviewFeaturesSymbols, INamedTypeSymbol previewFeatureAttributeSymbol, ISymbol& referencedPreviewSymbol)
   at Microsoft.NetCore.Analyzers.Runtime.DetectPreviewFeatureAnalyzer.BuildSymbolInformationFromOperations(OperationAnalysisContext context, ConcurrentDictionary`2 requiresPreviewFeaturesSymbols, INamedTypeSymbol previewFeatureAttributeSymbol)
   at Microsoft.NetCore.Analyzers.Runtime.DetectPreviewFeatureAnalyzer.<>c__DisplayClass26_0.<Initialize>b__1(OperationAnalysisContext context)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c.<ExecuteOperationAction>b__63_0(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)
-----

Suppress the following diagnostics to disable this analyzer: CA2252	Engine5		1	Active

edit: why did i specify 'cs' for md? Finally, this may be a different issue to the original one, but here's all the info I have for it, do with it as you please.

@CyrusNajmabadi
Copy link
Member

@george-tsiros that is about a different analyzer. Please open a new issue for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Analyzers Bug help wanted The issue is "up for grabs" - add a comment if you are interested in working on it
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants