StefanMaron / BusinessCentral.LinterCop

Community driven code linter for AL (MS Dynamics 365 Business Central)
https://stefanmaron.com
MIT License
77 stars 31 forks source link

LC0044 - threw an exception of type 'System.InvalidCastException' #786

Open NAVFreak opened 1 week ago

NAVFreak commented 1 week ago

Hi,

This morning I got the following exception in my pipelines below. We are not not running prerelease and this is the "lastversion" we get: 2024-10-02T13:20:25Z

We currently can't use the code analyzer at all for this project 😫

##[warning]warning AD000***: Analyzer 'BusinessCentral.LinterCop.Design.Rule0044AnalyzeTransferFields' threw an exception of type 'System.InvalidCastException' with message 'System.InvalidCastException: Unable to cast object of type 'Microsoft.Dynamics.Nav.CodeAnalysis.Syntax.ReportExtensionSyntax' to type 'Microsoft.Dynamics.Nav.CodeAnalysis.Syntax.TableExtensionSyntax'.
##[warning]at BusinessCentral.LinterCop.Design.Rule0044AnalyzeTransferFields.AnalyzeTableExtension(SyntaxNodeAnalysisContext ctx)
##[warning]at Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__DisplayClass53_***.<ExecuteSyntaxNodeAction>b__***() in X:\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\DiagnosticAnalyzer\AnalyzerExecutor.cs:line 75***
##[warning]at Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock(DiagnosticAnalyzer analyzer, Action analyze, Nullable`*** info) in X:\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\DiagnosticAnalyzer\AnalyzerExecutor.cs:line ***095'
##[warning]warning AD000***: Analyzer 'BusinessCentral.LinterCop.Design.Rule0044AnalyzeTransferFields' threw an exception of type 'System.InvalidCastException' with message 'System.InvalidCastException: Unable to cast object of type 'Microsoft.Dynamics.Nav.CodeAnalysis.Syntax.ReportExtensionSyntax' to type 'Microsoft.Dynamics.Nav.CodeAnalysis.Syntax.TableExtensionSyntax'.
##[warning]at BusinessCentral.LinterCop.Design.Rule0044AnalyzeTransferFields.AnalyzeTableExtension(SyntaxNodeAnalysisContext ctx)
##[warning]at Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__DisplayClass53_***.<ExecuteSyntaxNodeAction>b__***() in X:\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\DiagnosticAnalyzer\AnalyzerExecutor.cs:line 75***
##
NAVFreak commented 1 week ago

It didn't help with removing the rule LC0044 in the ruleset

    {
        "id": "LC0044",
        "action": "None",
        "justification": "transferfields, currently bug in analyzer"
    },
Arthurvdv commented 4 days ago

I've added an try/catch which now should raise the LC0000 diagnostic in the pre-release v0.31.2 version of the LinterCop.

Would be great if you could share a code snippet that causes this error.

NAVFreak commented 2 days ago

@Arthurvdv, now it compiles with version 2024-10-13T14:19 pre-release, but it seems LC0002 stopped working in that version.

Here is the diagnostics.

2024-10-15T06:15:48.8642457Z Char         : ***
2024-10-15T06:15:48.8643093Z AlertMessage : There was an Error in Rule "Rule0044" of type "Exception" at line ***6
2024-10-15T06:15:48.8645156Z AlertLevel   : info
2024-10-15T06:15:48.8645915Z RelativePath : \vsts-agent-win-x64-***.***3.***\_work\***83\s\App\Src\FooMakeShipment\NBAFooMakeShipment.ReportExt.al
2024-10-15T06:15:48.8647879Z Line         : ***
2024-10-15T06:15:48.8648243Z AlertCode    : LC0000
2024-10-15T06:15:48.8649731Z File         : NBAFooMakeShipment.ReportExt.al

Code for object that gets this error

reportextension 50001 "NBA Foo Make Shipment" extends "Foo Make Shipment"
{
    requestpage
    {
        layout
        {
            addafter(ShipToDateFld)
            {
                field(ShipToTimeFld; ShipToTime)
                {
                    ApplicationArea = All;
                    Caption = 'Ship To Time';
                    ToolTip = 'Specifies the value of the Ship To Time field.';
                    trigger OnValidate()
                    var
                        FooRentalSetup: Record "Foo Rental Setup";
                        NBAMakeShptSingleInstance: Codeunit "NBA Make Shpt Single Instance";
                        lShipToDate: Date;
                    begin
                        FooRentalSetup.Get();
                        lShipToDate := NBAMakeShptSingleInstance.GetShipDate();
                        if ShipToTime > FooRentalSetup."NBA Time for new On-rent day" then
                            SetOnRentDate(CalcDate('<+1D>', lShipToDate))
                        else
                            SetOnRentDate(lShipToDate);
                    end;
                }
            }
        }
        procedure SetShipToTime(NewShipToTime: Time)
        begin
            ShipToTime := NewShipToTime;
        end;

        var
            FooMakeReturn: Report "Foo Make Return";
            ShipToTime: Time;

    }

}