Stylecopanalyzers: SA1135 throws null reference exception when using dotnet core 3.1.2

Created on 23 Mar 2020  路  2Comments  路  Source: DotNetAnalyzers/StyleCopAnalyzers

I am currently updating the iotedge repo to from dotnet core 2.1.13 to 3.1.2.

I ran into the following error when executing dotnet build -c release, which uses stylecop.

Microsoft (R) Build Engine version 16.5.0+d4cbfca49 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 358.8 ms for /home/als5ev/Desktop/iotedge/edge-util/src/Microsoft.Azure.Devices.Edge.Util/Microsoft.Azure.Devices.Edge.Util.csproj.
  Restore completed in 358.8 ms for /home/als5ev/Desktop/iotedge/edge-util/src/Microsoft.Azure.Devices.Edge.Storage/Microsoft.Azure.Devices.Edge.Storage.csproj.
  Restore completed in 358.79 ms for /home/als5ev/Desktop/iotedge/edge-agent/src/Microsoft.Azure.Devices.Edge.Agent.Core/Microsoft.Azure.Devices.Edge.Agent.Core.csproj.
  Microsoft.Azure.Devices.Edge.Util -> /home/als5ev/Desktop/iotedge/edge-util/src/Microsoft.Azure.Devices.Edge.Util/bin/release/netcoreapp3.1/Microsoft.Azure.Devices.Edge.Util.dll
  Microsoft.Azure.Devices.Edge.Storage -> /home/als5ev/Desktop/iotedge/edge-util/src/Microsoft.Azure.Devices.Edge.Storage/bin/release/netcoreapp3.1/Microsoft.Azure.Devices.Edge.Storage.dll
CSC : error AD0001: Analyzer 'StyleCop.Analyzers.ReadabilityRules.SA1135UsingDirectivesMustBeQualified' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'. [/home/als5ev/Desktop/iotedge/edge-agent/src/Microsoft.Azure.Devices.Edge.Agent.Core/Microsoft.Azure.Devices.Edge.Agent.Core.csproj]

I also tested on dotnet core 3.1.102 and this does not occur.

bug fixed

Most helpful comment

This issue is caused by the Roslyn compiler change introduced with https://github.com/dotnet/roslyn/pull/41828.

Since then, INamedTypeSymbol.TupleUnderlyingType returns null for value tuples without named fields. So, this line sets symbol to null:

https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/7c80b00dd380f74db76e8a82fcb1b5458d269c9c/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1135UsingDirectivesMustBeQualified.cs#L98

And then this line throws:

https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/b6b9b0249a142132453873a677b7f09d7d65f34b/StyleCop.Analyzers/StyleCop.Analyzers/Helpers/SymbolNameHelpers.cs#L40

Interestingly enough, there are a couple of unit tests that should cover this use case:

  • TestFullyQualifiedAliasInsideNamespaceAsync
  • TestFullyQualifiedAliasAsync
  • etc.

However, they seem to not cover the expected case. Despite the passing results, the tests don't actually run the code causing the NRE.

The reason is: INamedTypeSymbol.IsTupleType returns false for the type-in-test System.ValueTuple<System.Collections.IList, int>.

(The concrete symbol descriptor object is Microsoft.CodeAnalysis.CSharp.Symbols.ConstructedNamedTypeSymbol from Microsoft.CodeAnalysis.CSharp, Version=1.3.1.0.)

I tried to use
C# typeSymbol.TupleUnderlyingType() ?? typeSymbol
(like it's being used in Roslyn now, see e.g. this), but that didn't really work because TupleTypeSyntaxWrapper.IsInstance always returns false then and we get a StackOverflowException in SymbolNameHelpers.AppendQualifiedSymbolName.

@sharwell Could you maybe outline a solution for this or give me an idea? I'd be glad to help by providing a PR. I'm confused by the fact that INamedTypeSymbol.IsTupleType is false in the unit tests. Do we have a problem with Microsoft.CodeAnalysis.CSharp?

All 2 comments

This issue is caused by the Roslyn compiler change introduced with https://github.com/dotnet/roslyn/pull/41828.

Since then, INamedTypeSymbol.TupleUnderlyingType returns null for value tuples without named fields. So, this line sets symbol to null:

https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/7c80b00dd380f74db76e8a82fcb1b5458d269c9c/StyleCop.Analyzers/StyleCop.Analyzers/ReadabilityRules/SA1135UsingDirectivesMustBeQualified.cs#L98

And then this line throws:

https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/b6b9b0249a142132453873a677b7f09d7d65f34b/StyleCop.Analyzers/StyleCop.Analyzers/Helpers/SymbolNameHelpers.cs#L40

Interestingly enough, there are a couple of unit tests that should cover this use case:

  • TestFullyQualifiedAliasInsideNamespaceAsync
  • TestFullyQualifiedAliasAsync
  • etc.

However, they seem to not cover the expected case. Despite the passing results, the tests don't actually run the code causing the NRE.

The reason is: INamedTypeSymbol.IsTupleType returns false for the type-in-test System.ValueTuple<System.Collections.IList, int>.

(The concrete symbol descriptor object is Microsoft.CodeAnalysis.CSharp.Symbols.ConstructedNamedTypeSymbol from Microsoft.CodeAnalysis.CSharp, Version=1.3.1.0.)

I tried to use
C# typeSymbol.TupleUnderlyingType() ?? typeSymbol
(like it's being used in Roslyn now, see e.g. this), but that didn't really work because TupleTypeSyntaxWrapper.IsInstance always returns false then and we get a StackOverflowException in SymbolNameHelpers.AppendQualifiedSymbolName.

@sharwell Could you maybe outline a solution for this or give me an idea? I'd be glad to help by providing a PR. I'm confused by the fact that INamedTypeSymbol.IsTupleType is false in the unit tests. Do we have a problem with Microsoft.CodeAnalysis.CSharp?

I have failing unit tests after updating the Microsoft.CodeAnalysis package in the StyleCop.Analyzers.Test.CSharp8 project to version 3.6.0. I'll have a look on how to fix this.

Was this page helpful?
0 / 5 - 0 ratings