SteveDunn / Vogen

A semi-opinionated library which is a source generator and a code analyser. It Source generates Value Objects
Apache License 2.0
890 stars 46 forks source link

AD0001: Analyzer 'Vogen.Rules.AddValidationAnalyzer' threw an exception #450

Closed CheloXL closed 1 year ago

CheloXL commented 1 year ago

Describe the bug

Using Rider, I'm getting the following error as a "warning" message:

AD0001: Analyzer 'Vogen.Rules.AddValidationAnalyzer' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'. 
Exception occurred with following context: Compilation: Blinkay.Control ISymbol: ParkingRightId (NamedType)  System.NullReferenceException: Object reference not set to an instance of an object. at Vogen.Rules.AddValidationAnalyzer.AnalyzeSymbol(SymbolAnalysisContext context) in /_/src/Vogen/Rules/AddValidationAnalyzer.cs:line 72 at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.c. b__48_1(ValueTuple`2 data) in Z:\BuildAgent\work\3b7ce003563d6f8f\src\Compilers\Core\Portable\DiagnosticAnalyzer\AnalyzerExecutor.cs:line 448 at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info) in Z:\BuildAgent\work\3b7ce003563d6f8f\src\Compilers\Core\Portable\DiagnosticAnalyzer\AnalyzerExecutor.cs:line 1199 -----

There is nothing strange in the ValueObject, defined as following:

[ValueObject]
public sealed partial class ParkingRightId : IValidEntityId
{
    public bool IsValid
    {
        get { return _isInitialized && !string.IsNullOrEmpty(_value); }
    }
}

The solution is quite complex, so I don't think I can provide a simplified sample.

Steps to reproduce

image

Expected behaviour

No warning should be set, nor any error should occur.

SteveDunn commented 1 year ago

Thanks for reporting, I'll try to get this fixed ASAP! A few questions:

  1. Do you have any global config for Vogen defaults? I can see from the attribute in your example that you default the underlying type to string (it's normally an int)
  2. I take it IsValid is part of IValidEntityId?
  3. Is there anything in particular that you're doing when you see this? Maybe building, or editing, or a nuget restore?
CheloXL commented 1 year ago
  1. Yes:

    [assembly: VogenDefaults(
    typeof(string),
    Conversions.TypeConverter | Conversions.SystemTextJson | Conversions.EfCoreValueConverter,
    customizations: Customizations.TreatNumberAsStringInSystemTextJson,
    debuggerAttributes: DebuggerAttributeGeneration.Basic
    )]
  2. Correct. That interface has just the bool IsValid { get; } property.

  3. No. Just opening the solution gives me the error.

SteveDunn commented 1 year ago

Thanks, I'm sure I'll spot it if it does occur on my Rider. I'm using 2023.1.3 (Build #RD-231.9161.46, built on June 21, 2023); where do you see this AD0001 error pop up?

SteveDunn commented 1 year ago

There's obviously something wrong with that analyzer, as it's supposed to show this, but it only shows it when the type is on the attribute (and not in global config)

image

I'll continue looking!

CheloXL commented 1 year ago

I'm using 2023.2 EAP 9. EAP has a lot more information when source generators fail. In fact, it has a special debugger for source generators (of course you must have access to the source of the source generator and know what are you doing... a thing I'm not qualified in)

SteveDunn commented 1 year ago

Getting somewhere - I now have an integration test that generates the error you're seeing:

Expected diagnostic:
    // /0/Test0.cs(20,26,20,34): info AddValidationMethod: Type name 'TypeName' can have validation
VerifyCS.Diagnostic().WithSpan(20, 26, 20, 34).WithArguments("TypeName"),

Actual diagnostic:
    // error AD0001: Analyzer 'Vogen.Rules.AddValidationAnalyzer' threw an exception of type 'System.NullReferenceException' with message 'Object reference not set to an instance of an object.'.
SteveDunn commented 1 year ago

This is fixed in version 3.0.20 - just pushing to NuGet now. Thanks very much for the bug report - much appreciated!