Azure/template-analyzer

[BUG] - Error when analyzing template with experimentalFeaturesEnabled in bicepconfig.json

pczar1 opened this issue · 3 comments

pczar1 commented

Describe the bug

It seems that analyzing template which has resource inside always is failing when bicepconfig.json has experimentalFeaturesEnabled.

As an example we can take official sample from Azure github:

https://github.com/Azure/azure-docs-bicep-samples/blob/main/samples/create-storage-account/main.bicep

Exception details:

Microsoft.Azure.Templates.Analyzer.Core.TemplateAnalyzerException: Error while evaluating rules.
 ---> System.InvalidCastException: Cannot cast Newtonsoft.Json.Linq.JProperty to Newtonsoft.Json.Linq.JToken.
   at Newtonsoft.Json.Linq.Extensions.Convert[T,U](T token)
   at Newtonsoft.Json.Linq.Extensions.Convert[T,U](IEnumerable`1 source)+MoveNext()
   at Microsoft.Azure.Templates.Analyzer.RuleEngines.PowerShellEngine.PowerShellRuleEngine.AnalyzeTemplate(TemplateContext templateContext)
   at Microsoft.Azure.Templates.Analyzer.Core.TemplateAnalyzer.AnalyzeAllIncludedTemplates(String populatedTemplate, String parameters, String templateFilePath, TemplateContext parentContext, String pathPrefix)
   --- End of inner exception stack trace ---
   at Microsoft.Azure.Templates.Analyzer.Core.TemplateAnalyzer.AnalyzeAllIncludedTemplates(String populatedTemplate, String parameters, String templateFilePath, TemplateContext parentContext, String pathPrefix)
   at Microsoft.Azure.Templates.Analyzer.Core.TemplateAnalyzer.AnalyzeTemplate(String template, String templateFilePath, String parameters)
   at Microsoft.Azure.Templates.Analyzer.Cli.CommandLineParser.AnalyzeTemplate(TemplateAndParams templateAndParameters)

When I was debugging I found out that temporary json files created from bicep during analyzing differs depend on experimentalFeaturesEnabled, which seems to be causing exception.

image

Expected behavior

Bicep is analyzed without throwing exception when experimentalFeaturesEnabled in bicepconfig.json

Reproduction Steps

  1. Sample bicepconfig.json:
{
    "analyzers": {
        "core": {
            "enabled": true,
            "rules": {
              "max-outputs": {
                "level": "warning"
              },
                "explicit-values-for-loc-params": {
                    "level": "off"
                }
            }
        }
    },
    "experimentalFeaturesEnabled": {
        "userDefinedTypes": true
    }
}
  1. Sample bicep file:

https://github.com/Azure/azure-docs-bicep-samples/blob/main/samples/create-storage-account/main.bicep

  1. Run:

TemplateAnalyzer.exe analyze-template C:\main.bicep -v

Environment

Template Analyzer Version: 0.5.2
.NET version: 7.0.102
IDE: Visual Studio 2022 (17.4.4)

nonik0 commented

Thanks for reporting. Template-Analyzer is currently using an older version of bicep since template-analyzer is still targeting .NET6. We'll be moving to the latest Bicep.Core on .NET7 which may fix this issue with the next release.

Template Analyzer was recently updated to the latest version of Bicep and .NET 8, so it's possible this issue is resolved. Could you try with version 0.7.0?

still open even with 0.8.0