DSC icon indicating copy to clipboard operation
DSC copied to clipboard

Encountering an error using loops in bicep config files

Open snehanagendra opened this issue 1 month ago • 2 comments

Prerequisites

  • [x] Write a descriptive title.
  • [x] Make sure you are able to repro it on the latest version
  • [x] 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/[email protected]' = [
  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

snehanagendra avatar Nov 18 '25 19:11 snehanagendra

The problem is that because name isn't specified, when DSC unrolls the loop each instance is not unique which is required. However, looking at the bicep documentation on loops, each example explicitly defines a name property that has the index to ensure each instance is unique.

Not sure if this is required since Bicep doesn't complain that name is missing, so one option is that if name isn't specified, we can autogenerate it with the index suffix.

SteveL-MSFT avatar Nov 18 '25 19:11 SteveL-MSFT

After discussion with bicep team, DSC needs to relax the strict validation that each resource name+type combination is unique. Basically, resources with the same name+type are allowed and treated as a "collection".

SteveL-MSFT avatar Nov 19 '25 20:11 SteveL-MSFT