microsoft / AL

Home of the Dynamics 365 Business Central AL Language extension for Visual Studio Code. Used to track issues regarding the latest version of the AL compiler and developer tools available in the Visual Studio Code Marketplace or as part of the AL Developer Preview builds for Dynamics 365 Business Central.
MIT License
744 stars 245 forks source link

Applying Code Actions upon Save does not work #5918

Closed fvet closed 2 years ago

fvet commented 4 years ago

Describe the bug

Performing Code Actions upon save crashes 9 out of 10.

AL file

table 2060423 "NVT Alert Entry Remark"
{
    // Currently used as 'parameter' table (not in UI) to add additional context in the 'Agent Alert' (remarks)
    Caption = 'Alert Entry';
    DataPerCompany = false;

    fields
    {
        field(1; "Entry No."; BigInteger)
        {
            Caption = 'Entry No.';
            TableRelation = "NVT Alert Entry"."Entry No.";
            NotBlank = true;
            DataClassification = CustomerContent;
        }
        field(2; "Line No."; Integer)
        {
            Caption = 'Line No.';
            DataClassification = CustomerContent;
        }
        field(10; "Message"; Text[250])
        {
            Caption = 'Message';
            DataClassification = CustomerContent;
        }
    }

    keys
    {
        key(PK; "Entry No.", "Line No.")
        {
            Clustered = true;
        }
    }

    trigger OnInsert()
    var
        AlertEntryRemark: record "NVT Alert Entry Remark";
    begin
        Testfield("Entry No.");

        If "Line No." = 0 then begin
            AlertEntryRemark.Reset;
            AlertEntryRemark.setrange("Entry No.", "Entry No.");
            If AlertEntryRemark.Findlast then
                "Line No." := AlertEntryRemark."Line No." + 1
            else
                "Line No." := 1;
        end;
    end;
}

Settings.json (reduced version)


{
    "al.enableCodeAnalysis": true,
    "al.backgroundCodeAnalysis": true,
    "al.codeAnalyzers": [
        "${CodeCop}"
    ],
    "al.enableCodeActions": true,
    "al.incrementalBuild": true,
    "editor.codeActionsOnSave": {
        "source.fixAll": true
    },
    "alOutline.codeActionsOnSave": [
        "SortVariables"
    ],
    "alOutline.fixCodeCopMissingParenthesesOnSave": true
}

Whenever I save a file, I would expect the CodeCop warning for missing Parentheses to be fix (by the alOutline extension), although it always results in the error below:

[Error - 22:58:45] Please report this issue to https://github.com/microsoft/al/issues including information on how to reproduce it, if possible.
Processing of message 'textDocument/codeAction' failed with error: 'Specified argument was out of the range of valid values. (Parameter 'span')'
Details:
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. (Parameter 'span')
   at Microsoft.Dynamics.Nav.CodeAnalysis.SyntaxNode.FindNode(TextSpan span, Boolean findInsideTrivia, Boolean getInnermostNodeForTie) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\Syntax\SyntaxNode.cs:line 1240
   at Microsoft.Dynamics.Nav.CodeCop.CodeFixes.UseParenthesisForFunctionCallCodeFixProvider.RegisterCodeFixesAsync(CodeFixContext context) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeCop\CodeFixes\UseParenthesisForFunctionCallCodeFixProvider.cs:line 47
   at Microsoft.Dynamics.Nav.CodeAnalysis.CodeFixes.CodeFixService.GetCodeFixesAsync(Document document, TextSpan span, ICodeFixProvider fixer, ImmutableArray`1 diagnostics, CancellationToken cancellationToken) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces\CodeFixes\CodeFixService.cs:line 149
   at Microsoft.Dynamics.Nav.CodeAnalysis.CodeFixes.CodeFixService.AppendFixesAsync(Document document, TextSpan span, IEnumerable`1 diagnosticsWithSameSpan, ArrayBuilder`1 result, ICodeFixProvider fixer, Func`2 hasFix, Func`2 getFixes, CancellationToken cancellationToken) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces\CodeFixes\CodeFixService.cs:line 131
   at Microsoft.Dynamics.Nav.CodeAnalysis.CodeFixes.CodeFixService.AppendFixesAsync(Document document, TextSpan span, IEnumerable`1 diagnostics, ArrayBuilder`1 result, CancellationToken cancellationToken) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces\CodeFixes\CodeFixService.cs:line 92
   at Microsoft.Dynamics.Nav.CodeAnalysis.CodeFixes.CodeFixService.GetFixesAsync(Document document, TextSpan textSpan, CancellationToken cancellationToken) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces\CodeFixes\CodeFixService.cs:line 69
   at Microsoft.Dynamics.Nav.CodeAnalysis.CodeActions.ALCodeActionService.GetCodeActionsAsync(Document document, TextSpan textSpan, CancellationToken cancellationToken) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis.Workspaces\CodeActions\ALCodeActionService.cs:line 45
   at Microsoft.Dynamics.Nav.EditorServices.Protocol.LanguageServer.CodeActionRequestHandler.HandleAsync(ProtocolCodeActionRequest request, Int32 requestId, CancellationToken cancellationToken) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.EditorServices.Protocol\LanguageServer\CodeActionRequestHandler.cs:line 55
   at Microsoft.Dynamics.Nav.EditorServices.Protocol.MessageProtocol.RequestHandlerBase`1.HandleAsync(JToken requestContents, Int32 requestId, CancellationToken cancellationToken) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.EditorServices.Protocol\MessageProtocol\RequestHandlerBase.cs:line 54
   at Microsoft.Dynamics.Nav.EditorServices.Protocol.RequestRegistry.Process(Message message) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.EditorServices.Protocol\Endpoints\RequestRegistry.cs:line 80

5. Versions:

anzwdev commented 4 years ago

My extension creates separate code action containing document edits for each missing parenthesis warning reported by the Microsoft al code analyzer. VS Code applies these edits when file is saved. Your error is thrown by AL compiler that runs in the background and rebuilds syntax tree and runs code analyzers when you edit al files.

I will check how VS Code sends notification about document changes to the language server when multiple code actions are applied during save. Maybe it triggers "DocumentChanged" event after each code action, so language server receivers very quickly a lot of notifications while background code analyzer is still processing al file? If that's the case, I will modify my code to report one big code action for all missing brackets and check if it helps. I will also stop creating my "Missing parenthesis" code action if you don't want to run it automatically on save as Microsoft AL Compiler also creates it and that implementation is better than mine.

thloke commented 2 years ago

@fvet - are you still able to reproduce this on the latest version of the VSIX?

fvet commented 2 years ago

@thloke I've stopped using any 'OnSave...' settings (CodeActions, FormatDocument, ...) since it slowed down the development experience a lot. Instead, I'm now manually running the 'Cleanup' actions from Andrzej Zwierzchowski extension, which is way faster than running actions upon save.

So for me, this issue can be closed if needed.