Azure / PSRule.Rules.Azure

Rules to validate Azure resources and infrastructure as code (IaC) using PSRule.
https://azure.github.io/PSRule.Rules.Azure/
MIT License
389 stars 84 forks source link

Export-AzRuleTemplateData fails after adding unused parameter with type expression #2432

Closed dmgonch closed 9 months ago

dmgonch commented 1 year ago

Description of the issue

Only reproes in PowerShell 7. Running repro.ps1 below results in the error:

Unable to expand resources because the template 'path\to\repro.json' was not valid with parameters 'path\to\repro\repro.parameters.json'. 
An error occurred evaluating expression '[concat(reference('modTwo').outputs.result.value)]' line 42. 
The arguments for 'Concat' are not in the expected format or type.

But then simply commenting out notUsedTypedParam get rids of the error.

To Reproduce

repro.ps1

$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest

$baseName = "$PSScriptRoot/repro"

& az bicep build --file "$baseName.bicep"
& az bicep generate-params --file "$baseName.bicep" --outfile "$baseName.json"
Get-AzRuleTemplateLink -InputPath "$baseName.parameters.json" | Export-AzRuleTemplateData -TemplateFile "$baseName.json" -OutputPath "$baseName-export.json"

repro.bicep

#disable-next-line no-unused-params
param notUsedTypedParam {
  Name: string
 }[] = []

 module modOne 'module-one.bicep' = {
   name: 'one'
   params: {
   }
 }

 module modTwo 'module-two.bicep' = {
   name: 'two'
   params: {
    input: modOne.outputs.result
   }
 }

 var fakeOptions = concat(
  modTwo.outputs.result
 )
 resource taskDeployment 'Microsoft.Resources/deployments@2020-10-01' = {
   name: 'name'
   properties: {
     mode: 'Incremental'
     template: {
       '$schema': 'https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#'
       contentVersion: '1.0.0.0'
       resources: [
         {
           apiVersion: '2019-12-01'
           type: 'Microsoft.ManagedIdentity/userAssignedIdentities'
           name: 'example'
           properties: {
            fakeOptions: fakeOptions
           }
         }
       ]
     }
   }
 }

module-one.bicep

output result string = 'one'

module-two.bicep

#disable-next-line no-unused-params
param input string
output result array = ['a', 'b']

Expected behaviour

No error and correctly generated repro-export.json

Module in use and version:

Captured output from $PSVersionTable:

Name                           Value
----                           -----
PSVersion                      7.3.6
PSEdition                      Core
GitCommitId                    7.3.6
OS                             Microsoft Windows 10.0.22621
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
dmgonch commented 1 year ago

After moving the resource deployment to a separate .bicep file (as suggested in https://github.com/Azure/bicep/issues/11846) the error does not repro anymore.

BernieWhite commented 1 year ago

Thanks for logging the issue @dmgonch. That good additional context.

Looks like there may be an issue with some updates to custom types.

Leave it open for now and we'll have a look.

BernieWhite commented 9 months ago

Double checked and concat expansion is occurring as expected. Thanks @dmgonch .