Azure / Azure/template-analyzer

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

Open
#343 3 comments 0 reactions 0 assignees View on GitHub
🔍 needs-triage bug
Dominant language
C#
Stars
143
Forks
47
PR merge metrics
No merged PRs in 30d

Description

### 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](https://github.com/Azure/template-analyzer/assets/48105366/b93d182a-8aa5-4911-a33a-ec6815f61496)

### 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
}
}
```

2. Sample bicep file:

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

3. 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)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.