dotnet / dotnet/roslyn

error AD0001: Analyzer 'Microsoft.NetCore.CSharp.Analyzers.Runtime.CSharpDetectPreviewFeatureAnalyzer' threw an exception of type 'System.NullReferenceException'

Open
#81,645 3 comments 1 reaction 0 assignees View on GitHub
Area-Compilers untriaged
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

### Analyzer

**Diagnostic ID**: AD0001: Analyzer 'Microsoft.NetCore.CSharp.Analyzers.Runtime.CSharpDetectPreviewFeatureAnalyzer' threw an exception of type 'System.NullReferenceException'

### Analyzer source

**SDK**: [Built-in CA analyzers in .NET 5 SDK or later](https://learn.microsoft.com/dotnet/fundamentals/productivity/code-analysis)

**Version**: SDK 9.0.105

### Describe the bug

I randomly get build failures like this:

> CSC : error AD0001: Analyzer 'Microsoft.NetCore.CSharp.Analyzers.Runtime.CSharpDetectPreviewFeatureAnalyzer' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'

### Steps To Reproduce

I don't know, it's random. Doing the same thing again usually succeeds.

### Expected behavior

No random build failure.

### Actual behavior

Random build failure.

## Additional context

Binlog:

[build-2025-03-25-112058.binlog.zip](https://github.com/user-attachments/files/19446359/build-2025-03-25-112058.binlog.zip)

The binlog shows this:

```
Exception occurred with following context:
Compilation: EmbeddedResources
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.CodeAnalysis.MetadataHelpers.GetInfoForImmediateNamespaceMembers(Boolean isGlobalNamespace, Int32 namespaceNameLength, IEnumerable`1 typesByNS, StringComparer nameComparer, IEnumerable`1& types, IEnumerable`1& namespaces)
at Microsoft.CodeAnalysis.CSharp.Symbols.Metadata.PE.PENamespaceSymbol.LoadAllMembers(IEnumerable`1 typesByNS)
at Microsoft.CodeAnalysis.CSharp.Symbols.Metadata.PE.PENamespaceSymbol.GetMembers(ReadOnlyMemory`1 name)
at Microsoft.CodeAnalysis.CSharp.Symbols.MergedNamespaceSymbol.SlowGetChildrenOfName(ReadOnlyMemory`1 name)
at Microsoft.CodeAnalysis.Collections.CachingDictionary`2.AddToConcurrentMap(ConcurrentDictionary`2 map, TKey key)
at Microsoft.CodeAnalysis.Collections.CachingDictionary`2.GetOrCreateValue(TKey key)
at Microsoft.CodeAnalysis.CSharp.Binder.GetCandidateMembers(NamespaceOrTypeSymbol nsOrType, String name, LookupOptions options, Binder originalBinder)
at Microsoft.CodeAnalysis.CSharp.Binder.LookupMembersInNamespace(LookupResult result, NamespaceSymbol ns, String name, Int32 arity, LookupOptions options, Binder originalBinder, Boolean diagnose, CompoundUseSiteInfo`1& useSiteInfo)
at Microsoft.CodeAnalysis.CSharp.Binder.BindNonGenericSimpleNamespaceOrTypeOrAliasSymbol(IdentifierNameSyntax node, BindingDiagnosticBag diagnostics, ConsList`1 basesBeingResolved, Boolean suppressUseSiteDiagnostics, NamespaceOrTypeSymbol qualifierOpt)
at Microsoft.CodeAnalysis.CSharp.Binder.BindQualifiedName(ExpressionSyntax leftName, SimpleNameSyntax rightName, BindingDiagnosticBag diagnostics, ConsList`1 basesBeingResolved, Boolean suppressUseSiteDiagnostics)
at Microsoft.CodeAnalysis.CSharp.Binder.BindNamespaceOrTypeOrAliasSymbol(ExpressionSyntax syntax, BindingDiagnosticBag diagnostics, ConsList`1 basesBeingResolved, Boolean suppressUseSiteDiagnostics)
at Microsoft.CodeAnalysis.CSharp.Binder.BindNamespaceOrTypeSymbol(ExpressionSyntax syntax, BindingDiagnosticBag diagnostics, ConsList`1 basesBeingResolved, Boolean suppressUseSiteDiagnostics)
at Microsoft.CodeAnalysis.CSharp.Binder.BindQualifiedName(ExpressionSyntax leftName, SimpleNameSyntax rightName, BindingDiagnosticBag diagnostics, ConsList`1 basesBeingResolved, Boolean suppressUseSiteDiagnostics)
at Microsoft.CodeAnalysis.CSharp.Binder.BindNamespaceOrTypeOrAliasSymbol(ExpressionSyntax syntax, BindingDiagnosticBag diagnostics, ConsList`1 basesBeingResolved, Boolean suppressUseSiteDiagnostics)
at Microsoft.CodeAnalysis.CSharp.Binder.BindNamespaceOrTypeSymbol(ExpressionSyntax syntax, BindingDiagnosticBag diagnostics, ConsList`1 basesBeingResolved, Boolean suppressUseSiteDiagnostics)
at Microsoft.CodeAnalysis.CSharp.Binder.BindQualifiedName(ExpressionSyntax leftName, SimpleNameSyntax rightName, BindingDiagnosticBag diagnostics, ConsList`1 basesBeingResolved, Boolean suppressUseSiteDiagnostics)
at Microsoft.CodeAnalysis.CSharp.Binder.BindNamespaceOrTypeOrAliasSymbol(ExpressionSyntax syntax, BindingDiagnosticBag diagnostics, ConsList`1 basesBeingResolved, Boolean suppressUseSiteDiagnostics)
at Microsoft.CodeAnalysis.CSharp.Binder.BindTypeOrAlias(ExpressionSyntax syntax, BindingDiagnosticBag diagnostics, ConsList`1 basesBeingResolved, Boolean suppressUseSiteDiagnostics)
at Microsoft.CodeAnalysis.CSharp.Symbol.LoadAndValidateAttributes(OneOrMany`1 attributesSyntaxLists, CustomAttributesBag`1& lazyCustomAttributesBag, AttributeLocation symbolPart, Boolean earlyDecodingOnly, Binder binderOpt, Func`2 attributeMatchesOpt, Action`1 beforeAttributePartBound, Action`1 afterAttributePartBound)
at Microsoft.CodeAnalysis.CSharp.Symbols.SourceAssemblySymbol.GetAttributes()
at Microsoft.CodeAnalysis.CSharp.Symbols.PublicModel.Symbol.Microsoft.CodeAnalysis.ISymbol.GetAttributes()
at Analyzer.Utilities.Extensions.ISymbolExtensions.HasAnyAttribute(ISymbol symbol, INamedTypeSymbol attribute)
at Microsoft.NetCore.Analyzers.Runtime.DetectPreviewFeatureAnalyzer.b__33_0(CompilationStartAnalysisContext context)
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: CA2252
CompilerServer: server - server processed compilation - EmbeddedResources (net9.0-tvos)

```

Contributor guide

Open the contributing guide

Research direction

Start by examining the attached binlog and the DetectPreviewFeatureAnalyzer entry point, especially the stack through MetadataHelpers.GetInfoForImmediateNamespaceMembers and PENamespaceSymbol.LoadAllMembers. The first task is to identify a reproducible trigger for the random AD0001 failure in SDK 9.0.105; done means the analyzer no longer throws and the scenario has regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.