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

[BUG] AL 4.0 Opening project with app.json as active file causes error Rule210SuboptimalIndex #5313

Closed JuricaB closed 5 years ago

JuricaB commented 5 years ago

Describe the bug When opening a project I previously closed while app.json was open, AL language causes error as below. It also happens if app.json was among last active files, but not currently active.

To Reproduce Steps and to reproduce the behavior:

  1. Create project using AL language 4.0 (tested on 4.0.176004 and 4.0.182565)
  2. Add code that would raise informational message AA0210 (table xx does not contain the key...)
  3. Save all files. Close VS Code while app.json is one of active files
  4. Open VS Code - after few seconds, the error is shown

Notes:

AL Code to reproduce the issue

Expected behavior Not to get the error. Editing app.json should not affect the problems pane in this way.

Screenshots If applicable, add screenshots to help explain your problem. image

5. Versions:

BartPermentier commented 5 years ago

Jup, very annoying

qutreson commented 5 years ago

Hi @JuricaB and @BartPermentier, thank you for reporting this issue. Can you copy-paste here the stack trace that you are getting when using the analyzer?

The screenshot above is cropped and doesn't show us exactly what the InvalidCastException is and where it happens.

BartPermentier commented 5 years ago

Analyzer 'Microsoft.Dynamics.Nav.CodeCop.Design.Rule210SuboptimalIndex' threw an exception of type 'System.InvalidCastException' with message 'System.InvalidCastException: Unable to cast object of type 'Microsoft.Dynamics.Nav.CodeAnalysis.Symbols.SourceParameterSymbol' to type 'Microsoft.Dynamics.Nav.CodeAnalysis.IVariableSymbol'. at Microsoft.Dynamics.Nav.CodeCop.Design.Rule210SuboptimalIndex.AnalyzeInvocations(SyntaxNodeAnalysisContext syntaxNodeAnalysisContex, ImmutableDictionary2 processInvocations) in F:\a\586\s\source\Prod\Microsoft.Dynamics.Nav.CodeCop\Design\Rule210SuboptimalIndex.cs:line 86 at Microsoft.Dynamics.Nav.CodeCop.Design.Rule210SuboptimalIndex.<>c.<Analyze>b__5_0(SyntaxNodeAnalysisContext syntaxNodeAnalysisContex) in F:\a\586\s\source\Prod\Microsoft.Dynamics.Nav.CodeCop\Design\Rule210SuboptimalIndex.cs:line 66 at Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__DisplayClass52_1.<ExecuteSyntaxNodeAction>b__1() in F:\a\586\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\DiagnosticAnalyzer\AnalyzerExecutor.cs:line 742 at Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock(DiagnosticAnalyzer analyzer, Action analyze, Nullable1 info) in F:\a\586\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\DiagnosticAnalyzer\AnalyzerExecutor.cs:line 1079'

atoader commented 5 years ago

@BartPermentier can you please provide a snippet of code that reproduces the issue?

JuricaB commented 5 years ago

Hi @atoader,

Please see below for the snippet I can use to replicate this:

codeunit 50100 "TestError"
{
    procedure Test210(var GLEntry: Record "G/L Entry");
    var
        GLReg: Record "G/L Register";
    begin
        GLEntry.FindFirst();
        GLReg.SetRange("From Entry No.", 0, GLEntry."Entry No.");
        if GLReg.IsEmpty() then;
    end;

}

In addition, the project needs to have settings.json file with CodeCop enabled.

Thanks,

Jurica

JuricaB commented 5 years ago

As a side note, I can't see how to copy full stack trace of error from VS Code Problems pane - is there a trick to it?

atoader commented 5 years ago

That might be a limitation of VS Code, but you should be able to find the exception in the EditorServices.log file in the .vscode/extensions/microsoft.al-*/bin

JuricaB commented 5 years ago

Sorry @atoader - I looked under .vscode\extensions\ms-dynamics-smb.al-4.0.182565\bin\EditorServices.log and there is nothing of importance logged in that file.

atoader commented 5 years ago

Can you set the: image to Verbose, restart your editor, reproduce the issue, and inspect the file.

After you are done, do not forget to change it back to Normal.

JuricaB commented 5 years ago

Thanks @atoader after settig to Verbose I can copy the message: Analyzer 'Microsoft.Dynamics.Nav.CodeCop.Design.Rule210SuboptimalIndex' threw an exception of type 'System.InvalidCastException' with message 'System.InvalidCastException: Unable to cast object of type 'Microsoft.Dynamics.Nav.CodeAnalysis.Symbols.SourceParameterSymbol' to type 'Microsoft.Dynamics.Nav.CodeAnalysis.IVariableSymbol'. at Microsoft.Dynamics.Nav.CodeCop.Design.Rule210SuboptimalIndex.AnalyzeInvocations(SyntaxNodeAnalysisContext syntaxNodeAnalysisContex, ImmutableDictionary2 processInvocations) in F:\a\586\s\source\Prod\Microsoft.Dynamics.Nav.CodeCop\Design\Rule210SuboptimalIndex.cs:line 86 at Microsoft.Dynamics.Nav.CodeCop.Design.Rule210SuboptimalIndex.<>c.b5_0(SyntaxNodeAnalysisContext syntaxNodeAnalysisContex) in F:\a\586\s\source\Prod\Microsoft.Dynamics.Nav.CodeCop\Design\Rule210SuboptimalIndex.cs:line 66 at Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__DisplayClass52_1.b1() in F:\a\586\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\DiagnosticAnalyzer\AnalyzerExecutor.cs:line 742 at Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock(DiagnosticAnalyzer analyzer, Action analyze, Nullable1 info) in F:\a\586\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\DiagnosticAnalyzer\AnalyzerExecutor.cs:line 1079'

jwikman commented 5 years ago

I think this is related to some characters in the app.json that the AL Language extension does not like.

Auto format the app.json with AL Language does not help, but if you instead use the VSCode function "Format Document with..." and select "JSON Language Features". I've had issues with "hard spaces" (char 160) and a trailing new line. After I've reformatted my app.json, the error disappears.

The hard spaces also caused issues when publishing...

rdebath commented 5 years ago

@jwikman The fact that these errors disappear when you save the app.json file is an artefact of the poor caching of the errors generated by the background processes (Like the fact that the errors disappear when you click on one). Random unexpected errors are thrown against the app.json file not the file that triggered them. Saving any change to the app.json file deletes all errors attached to it and causes a regeneration of the errors that are supposed to be attached to just that file. These errors are not "supposed" to be attached so they don't come back until a full regeneration is triggered. Restarting vscode does this as does toggling "al.enableCodeAnalysis": in settings.json

NB: There is another error triggered by comments in app.json for a couple of versions of the extension, but that's not this.

JuricaB commented 5 years ago

@jwikman : @rdebath is right - it is not related to app.json characters issue (I have seen that one before too). Using Format Document With...JSON appears to fix the issue- but so does making any change in app.json and saving it. After reopening the project, same error shows up again.

jwikman commented 5 years ago

@JuricaB @rdebath, you are right guys. The error is back now. :(

AndreyKorepanov commented 5 years ago

Hi @JuricaB, I am trying to reproduce on 4.0.182565 using the steps above. I am using the code from your snippet (with the swapped SetRange and FindFirst lines) to provoke the AA0210 warning, but still can not get repro. The problem might happen for some specific app.json items. Could you share your app.json. Could you also try to repro the bug with the latest AL extension daily build, it might be already fixed there as we recently fixed similar issue. Thank you.

JuricaB commented 5 years ago

Hi @AndreyKorepanov

See below for app.json:

{
  "id": "fb4a6873-0ac6-4345-9f3c-767576998a3c",
  "name": "WAVE2",
  "publisher": "Default publisher",
  "version": "1.0.0.0",
  "brief": "",
  "description": "",
  "privacyStatement": "",
  "EULA": "",
  "help": "",
  "url": "",
  "logo": "",
  "dependencies": [
    {
      "appId": "63ca2fa4-4f03-4f2b-a480-172fef340d3f",
      "publisher": "Microsoft",
      "name": "System Application",
      "version": "1.0.0.0"
    },
    {
      "appId": "437dbf0e-84ff-417a-965d-ed2bb9650972",
      "publisher": "Microsoft",
      "name": "Base Application",
      "version": "15.0.0.0"
    }
  ],
  "screenshots": [],
  "platform": "15.0.0.0",
  "idRanges": [
    {
      "from": 50100,
      "to": 50149
    }
  ],
  "contextSensitiveHelpUrl": "https://WAVE2.com/help/",
  "showMyCode": false,
  "runtime": "4.0",
  "features": ["TranslationFile"]
}

You also need to have settings.json with CodeCop enabled:

{
    "al.enableCodeAnalysis": true,
    "al.codeAnalyzers": [        
        "${CodeCop}"
    ]
}

Note also I am still replicating this on 4.0.182565 - I will try daily build later today and let you know.

qutreson commented 5 years ago

@JuricaB, closing this issue as it has been fixed in our latest preview builds from master (version 5.0.0 and above) and for 15.x (from version 4.0.186154 and above). The fix will also be available in the next update of the AL extension in the marketplace. If you can still reproduce this issue on our latest developer preview, please open a new issue and reference this one.

rdebath commented 5 years ago

I can reproduce this in the latest preview; but that's over two weeks old. When is the bcsandbox-master going to be starting up again then?

Should this be @freddydk ?

qutreson commented 5 years ago

@rdebath, which is the version of the AL extension that you are getting from the container?

rdebath commented 5 years ago

In the Run directory: al-4.0.176004.vsix In the 150/AL Dev: Identity Language="en-US" Id="al" Version="4.0.176004" Microsoft.Dynamics.Nav.Server.exe version: 15.0.36510 Docker images version: 15.0.36560

atoader commented 4 years ago

@rdebath we have pushed out a new VSIX update to the marketplace. Does it still reproduce on that version? Could you provide a sample to reproduce the issue?

rdebath commented 4 years ago

I no longer see this error.