Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Unable to expand resources because the source file ... The function "references" was not found. when using languageVersion 2.0 #2922

Closed
GABRIELNGBTUC opened this issue Jun 7, 2024 · 1 comment · Fixed by #2958
Assignees
Labels
bug Something isn't working feature: pre-flight-expansion Issues relating to expansion of ARM and Bicep.
Milestone

Comments

@GABRIELNGBTUC
Copy link

Existing rule

No response

Description of the issue

When referencing the outputs of a collection of deployed modules, it seems that PSRule is unable to recognize the references function used to access the properties of the modules.

Error messages

##[error]Failed to expand bicep source '/home/vsts/work/1/s/main.bicep'. Exception calling "GetBicepResources" with "2" argument(s): "Unable to expand resources because the source file '/home/vsts/work/1/s/main.bicep' was not valid. An error occurred evaluating expression '[map(references('acc_loop'), lambda('acc', createObject(format('{0}', lambdaVariables('acc').outputs.acc.value), createObject('name', lambdaVariables('acc').outputs.acc.value))))]' line 257. The function "references" was not found."

Reproduction

module1.bicep

param acc int = 0

metadata name = 'Create a Windows Virtual Machine'

//
// Parameters
//
@minLength(1)
@maxLength(64)
@description('Name of the Virtual Machine')
param vm_name string = replace(resourceGroup().name, '-rg-', '-vm-')

@description('Location of the Virtual Machine')
param location string = resourceGroup().location

@description('Tags for the Virtual Machine')
param tags object = resourceGroup().tags

@description('Id of the Network Interface to attach to the Virtual Machine')
param network_interface_id string

@minLength(1)
@maxLength(16)
@description('Username for Virtual Machine admin')
param admin_username string

@description('Admin password')
@secure()
param admin_password string

@minLength(1)
@maxLength(80)
@description('Name of the Operating System Disk for the Virtual Machine')
param os_disk_name string = replace(resourceGroup().name, '-rg-', '-osdisk-')

@description('ID of the Data Disk for the Virtual Machine')
param data_disk_id string

@minLength(1)
@maxLength(80)
@description('Name of the Data Disk for the Virtual Machine')
param data_disk_name string

@description('Virtual Machine Size')
param vm_size string = 'Standard_D2s_v3'

@description('Size in GB of Operating System Disk')
param disk_size_gb int = 127

@description('Storage Account Type of Managed Disk')
@allowed([
  'Standard_LRS'
  'Premium_LRS'
  'Premium_ZRS'
  'StandardSSD_LRS'
  'StandardSSD_ZRS'
  'UltraSSD_LRS'
])
param storage_account_type string = 'StandardSSD_LRS'

@allowed([
  'None'
  'SystemAssigned'
])
param identity string = 'None'

//
// Resources
//
resource virtual_machine 'Microsoft.Compute/virtualMachines@2021-07-01' = {
  name: vm_name
  location: location
  tags: tags
  identity: {
    type: identity
  }
  properties: {
    hardwareProfile: {
      vmSize: vm_size
    }

    networkProfile: {
      networkInterfaces: [
        {
          id: network_interface_id
           properties: {
             deleteOption: 'Detach'
           }
        }
      ]
    }

    osProfile: {
      adminUsername: admin_username
      adminPassword: admin_password
      computerName: take(vm_name, 15)
      windowsConfiguration: {
        enableAutomaticUpdates: true
        provisionVMAgent: true
        timeZone: 'Romance Standard Time'
      }
    }

    storageProfile: {
      osDisk: {
        createOption: 'FromImage'
        deleteOption: 'Detach'
        diskSizeGB: disk_size_gb
        managedDisk: {
          storageAccountType: storage_account_type
        }
        name: os_disk_name
        osType: 'Windows'
      }
      dataDisks: [
        {
          lun: 0
          createOption: 'Attach'
          deleteOption: 'Detach'
          caching: 'ReadOnly'
          writeAcceleratorEnabled: false
          managedDisk: {
            id: data_disk_id
          }
          diskSizeGB: 40
          name: data_disk_name
        }
      ]

      imageReference: {
        publisher: 'MicrosoftWindowsServer'
        offer: 'WindowsServer'
        sku: '2022-datacenter-azure-edition'
        version: 'latest'
      }
    }
    licenseType: 'Windows_Server'
  }
}

@description('Name of the created VM')
output output_vm_name string = virtual_machine.name


output acc string = guid(string(acc))

main.bicep

module acc_loop 'module1.bicep' = [for (item, index) in range(0, 5): {
  name: 'deploy-${index}'
  params: {
    acc: index
    admin_username: 'sqdsqdqqs'
    admin_password: uniqueString('sqdsqdsqsq')
    data_disk_id: 'sqdsqsq'
    data_disk_name: 'sqdsqdssq'
    network_interface_id: 'qsdsdq'
  }

}]

var output_array = map(acc_loop, acc => {
  '${acc.outputs.acc}': {
    name: acc.outputs.acc
  }
})

output output array = output_array

The compoilation of main.bicep fails with:

##[error]Failed to expand bicep source '/home/vsts/work/1/s/main.bicep'. Exception calling "GetBicepResources" with "2" argument(s): "Unable to expand resources because the source file '/home/vsts/work/1/s/main.bicep' was not valid. An error occurred evaluating expression '[map(references('acc_loop'), lambda('acc', createObject(format('{0}', lambdaVariables('acc').outputs.acc.value), createObject('name', lambdaVariables('acc').outputs.acc.value))))]' line 257. The function "references" was not found."

Version of PSRule

2.9.0

Version of PSRule for Azure

1.37.0

Additional context

No response

@GABRIELNGBTUC GABRIELNGBTUC added bug Something isn't working Needs: Triage 🔍 labels Jun 7, 2024
@BernieWhite
Copy link
Collaborator

Thanks for reporting the issue @GABRIELNGBTUC

@BernieWhite BernieWhite added feature: pre-flight-expansion Issues relating to expansion of ARM and Bicep. and removed Needs: Triage 🔍 labels Jun 7, 2024
@BernieWhite BernieWhite added this to the v1.38.0 milestone Jun 19, 2024
BernieWhite added a commit to BernieWhite/PSRule.Rules.Azure that referenced this issue Jun 29, 2024
@BernieWhite BernieWhite self-assigned this Jun 29, 2024
This was referenced Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working feature: pre-flight-expansion Issues relating to expansion of ARM and Bicep.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants