Skip to content

Encountering an error using loops in bicep config files #1270

@snehanagendra

Description

@snehanagendra

Prerequisites

  • Write a descriptive title.
  • Make sure you are able to repro it on the latest version
  • Search the existing issues.

Summary

I am seeing an error using for loop function in bicep. The error I am getting is Error: Validation: Resource named 'simple_loop' for type 'Microsoft.Windows/Registry' is specified more than once in the configuration. I

Steps to reproduce

  1. Use this bicep that has loops
targetScope = 'desiredStateConfiguration'

// Base path for all test keys
@description('Base registry path where results are written')
param basePath string = 'HKEY_LOCAL_MACHINE\\SOFTWARE\\DSCBicepTests'

// Test data for loops
var items = ['A', 'B', 'C']

// Simple array loop - creates individual resources for each item (modern for syntax)
resource simple_loop 'Microsoft.Windows/Registry@1.0.0' = [
  for item in items: {
    keyPath: '${basePath}\\loops_simple_${item}'
    valueName: 'item'
    valueData: { String: item }
  }
]

// Output some loop results for verification
output simpleLoopCount int = length(items)
  1. Apply the bicep config via DSC

  2. Observe error

  3. For debugging purpose, this is the generated json for bicep config

  "$schema": "https://aka.ms/dsc/schemas/v3/bundled/config/document.json",
  "languageVersion": "2.2-experimental",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_EXPERIMENTAL_WARNING": "This template uses ARM features that are experimental. Experimental features should be enabled for testing purposes only, as there are no guarantees about the quality or stability of these features. Do not enable these settings for any production usage, or your production environment may be subject to breaking.",
    "_EXPERIMENTAL_FEATURES_ENABLED": [
      "Enable defining extension configs for modules"
    ],
    "_generator": {
      "name": "bicep",
      "version": "0.36.177.2456",
      "templateHash": "4940191824837793026"
    }
  },
  "parameters": {
    "basePath": {
      "type": "string",
      "defaultValue": "HKEY_LOCAL_MACHINE\\SOFTWARE\\DSCBicepTests",
      "metadata": {
        "description": "Base registry path where results are written"
      }
    }
  },
  "variables": {
    "items": [
      "A",
      "B",
      "C"
    ]
  },
  "extensions": {
    "dsc": {
      "name": "DesiredStateConfiguration",
      "version": "0.1.0"
    }
  },
  "resources": {
    "simple_loop": {
      "copy": {
        "name": "simple_loop",
        "count": "[length(variables('items'))]"
      },
      "extension": "dsc",
      "type": "Microsoft.Windows/Registry",
      "apiVersion": "1.0.0",
      "properties": {
        "keyPath": "[format('{0}\\loops_simple_{1}', parameters('basePath'), variables('items')[copyIndex()])]",
        "valueName": "item",
        "valueData": {
          "String": "[variables('items')[copyIndex()]]"
        }
      }
    }
  },
  "outputs": {
    "simpleLoopCount": {
      "type": "int",
      "value": "[length(variables('items'))]"
    }
  }
}```

### Expected behavior

```console
DSC is able to apply the config

Actual behavior

Getting an error `Error: Validation: Resource named 'simple_loop' for type 'Microsoft.Windows/Registry' is specified more than once in the configuration.`

Error details

`Error: Validation: Resource named 'simple_loop' for type 'Microsoft.Windows/Registry' is specified more than once in the configuration.`

Environment data

Name                           Value
----                           -----
PSVersion                      7.5.4
PSEdition                      Core
GitCommitId                    7.5.4
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

Version

dsc 3.2.0-preview.8

Visuals

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions