microsoft / PSRule

Validate infrastructure as code (IaC) and objects using PowerShell rules.
https://microsoft.github.io/PSRule/v2/
MIT License
390 stars 51 forks source link

Failed to expand bicep source when reading JsonContent into a parameter and referencing these in another param. #1311

Closed akata72 closed 1 year ago

akata72 commented 1 year ago

Description of the issue Hi Bernie,

We are using your excellent PSRule tool, but are running into the issue below.

We have this Bicep Module:

targetScope = 'subscription'

@description('Common settings used across the platform deployment.')
**param config object = loadJsonContent('../../primaryLocationVariables.json')**

**param location string = config.common.location**
param resourceGroups array = config.resourceGroups

module resourceGroup 'br/modules:microsoft.resources.resourcegroups:0.4' = [for resourceGroup in resourceGroups: {
  scope: subscription()
  name: '${resourceGroup}-depl'
  params: {
    name: resourceGroup
    location: location
    tags: config.common.tags
    enableDefaultTelemetry: false
    roleAssignments: []
    //lock: 'CanNotDelete'  
  }
}]

When running Assert-PSRule on this bicep template, I get the following error message.

ERROR Failed to expand bicep source 'C:\VSCodeRepo\platform-deployment-example-dev\.bicep\microsoft.resources\resourcegroups\deploy.rg.pri.bicep'. Exception calling "GetBicepResources" with "3" argument(s): "Unable to expand resources because the source file 'C:\VSCodeRepo\platform-deployment-example-dev\.bicep\microsoft.resources\resourcegroups\deploy.rg.pri.bicep' was not valid. An error occurred evaluating expression '[format('{0}', **parameters('config').common.location)]**' line 21. An error occurred evaluating expression '[variables('$fxv#0')]' line 14. The variable named '$fxv#0' was not defined in the template." To Reproduce

It can be reproduced by reading some content into a param and then referencing this in a different parameter. (to me it looks like the parameter is not interpreted/read correctly). The bicep file is validated and deployed on the next steps in the pipeline. (it also has the same behaviour when running locally).

I am running the latest versions of PSRule and PSRule.Azure

BernieWhite commented 1 year ago

@akata72 Thanks for reporting the issue. We'll have a look and let you know.

BernieWhite commented 1 year ago

@akata72 I've moved the issue over here for tracking and we're working on a fix within PSRule for Azure for v1.20.1. https://github.com/Azure/PSRule.Rules.Azure/issues/1780

BernieWhite commented 1 year ago

Fixed upstream.