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

Code analyzer error when accessing protected variable in report extension #6958

Closed MikeGlue closed 2 years ago

MikeGlue commented 2 years ago

1. Describe the bug The following CodeCop error appears when you StrSubstNo with a protected variable that has a placeholder in it.

2. To Reproduce I replicated this with a report extension, not sure if it can happen with other extension objects. I've attached the example projects that I used to replicate the issue.

  1. Create a report with a protected variable that has a placeholder in it.
  2. Create a report extension to the report in step 1.
  3. Use StrSubstNo to inject a value into the placeholder of the protected variable.

I have attached a zip of both the parent and child projects that replicate the issue: CodeAnalyserError.zip

3. Expected behavior No code analysis error.

4. Actual behavior The following error shows (it repeats if you have multiple instances of StrSubstNo being used:

[{
    "resource": "/c:/_dev/repos/Misc/CodeAnalyserErrorChild/app.json",
    "owner": "_generated_diagnostic_collection_name_#0",
    "code": "AD0001",
    "severity": 4,
    "message": "Analyzer 'Microsoft.Dynamics.Nav.CodeCop.Design.Rule131StringParamsMustMatchPlaceholders' threw an exception of type 'System.InvalidCastException' with message 'System.InvalidCastException: Unable to cast object of type 'Microsoft.Dynamics.Nav.CodeAnalysis.Symbols.SimpleNavTypeSymbol' to type 'Microsoft.Dynamics.Nav.CodeAnalysis.ILabelTypeSymbol'.\r\n   at Microsoft.Dynamics.Nav.CodeCop.Design.Rule131StringParamsMustMatchPlaceholders.GetExpressionParamValue(IOperation expression) in D:\\a\\1\\s\\source\\Prod\\Microsoft.Dynamics.Nav.CodeCop\\Design\\Rule131StringParamsMustMatchPlaceholders.cs:line 103\r\n   at Microsoft.Dynamics.Nav.CodeCop.Design.Rule131StringParamsMustMatchPlaceholders.AnalyzeInvocation(OperationAnalysisContext ctx) in D:\\a\\1\\s\\source\\Prod\\Microsoft.Dynamics.Nav.CodeCop\\Design\\Rule131StringParamsMustMatchPlaceholders.cs:line 71\r\n   at Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__DisplayClass53_1.<ExecuteOperationAction>b__1() in D:\\a\\1\\s\\source\\Prod\\Microsoft.Dynamics.Nav.CodeAnalysis\\DiagnosticAnalyzer\\AnalyzerExecutor.cs:line 764\r\n   at Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock(DiagnosticAnalyzer analyzer, Action analyze, Nullable`1 info) in D:\\a\\1\\s\\source\\Prod\\Microsoft.Dynamics.Nav.CodeAnalysis\\DiagnosticAnalyzer\\AnalyzerExecutor.cs:line 1100'",
    "source": "AL",
    "startLineNumber": 1,
    "startColumn": 1,
    "endLineNumber": 1,
    "endColumn": 1
}]

5. Versions:

Final Checklist

Please remember to do the following:

sergiudina commented 2 years ago

Hi, @MikeGlue could you please share your AL settings from the settings.json file?

MikeGlue commented 2 years ago

Here you go:

{
    "al.codeAnalyzers": [
        "${CodeCop}"
    ],
    "al.enableCodeAnalysis": true,
    "al.backgroundCodeAnalysis": true,
    "al.enableCodeActions": true
}
nndobrev commented 2 years ago

@MikeGlue, we are unable to reproduce the issue, please check again on the latest version of our VSCode extension and make sure you don't have any other extensions installed. image

MikeGlue commented 2 years ago

This is definitely still happening with the latest AL extension (v9.0.605172) and the sample project I provided to you. I confirmed that all extensions have been removed except for AL Language. I also confirmed that another developer on my team is seeing the same issue.

I've attached a fresh copy of the "child" project that just needs the base BC symbols added. Everything in this project was compiled and validated using the latest AL extension and using the official release of v20.

Same error happens:

Analyzer 'Microsoft.Dynamics.Nav.CodeCop.Design.Rule131StringParamsMustMatchPlaceholders' threw an exception of type 'System.InvalidCastException' with message 'System.InvalidCastException: Unable to cast object of type 'Microsoft.Dynamics.Nav.CodeAnalysis.Symbols.SimpleNavTypeSymbol' to type 'Microsoft.Dynamics.Nav.CodeAnalysis.ILabelTypeSymbol'.
   at Microsoft.Dynamics.Nav.CodeCop.Design.Rule131StringParamsMustMatchPlaceholders.GetExpressionParamValue(IOperation expression) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeCop\Design\Rule131StringParamsMustMatchPlaceholders.cs:line 103
   at Microsoft.Dynamics.Nav.CodeCop.Design.Rule131StringParamsMustMatchPlaceholders.AnalyzeInvocation(OperationAnalysisContext ctx) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeCop\Design\Rule131StringParamsMustMatchPlaceholders.cs:line 71
   at Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__DisplayClass53_1.<ExecuteOperationAction>b__1() in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\DiagnosticAnalyzer\AnalyzerExecutor.cs:line 764
   at Microsoft.Dynamics.Nav.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock(DiagnosticAnalyzer analyzer, Action analyze, Nullable`1 info) in D:\a\1\s\source\Prod\Microsoft.Dynamics.Nav.CodeAnalysis\DiagnosticAnalyzer\AnalyzerExecutor.cs:line 1078'

CodeAnalyserErrorChild.zip

JesperSchulz commented 2 years ago

The fix for this issue has been checked in to the master branch. It will be available in the bcinsider.azurecr.io/bcsandbox-master Docker image starting from platform build number 39052 and VS Code Extension Version .

If you don’t have access to these images you need to become part of the Ready2Go program: aka.ms/readytogo

For more details on code branches and docker images please read: https://blogs.msdn.microsoft.com/nav/2018/05/03/al-developer-previews-multiple-releases-and-github/ https://freddysblog.com/2020/06/25/working-with-artifacts/

JesperSchulz commented 2 years ago

The fix for this issue has been checked in to the master branch. It will be available in the bcinsider.azurecr.io/bcsandbox-master Docker image starting from platform build number 39197 and VS Code Extension Version 10.0.611940.

If you don’t have access to these images you need to become part of the Ready2Go program: aka.ms/readytogo

For more details on code branches and docker images please read: https://blogs.msdn.microsoft.com/nav/2018/05/03/al-developer-previews-multiple-releases-and-github/ https://freddysblog.com/2020/06/25/working-with-artifacts/