Skip to content

Instantly share code, notes, and snippets.

@alex-frankel
Last active April 26, 2024 15:55
Show Gist options
  • Save alex-frankel/79a92c5832ea49093a3cac52d5d280dd to your computer and use it in GitHub Desktop.
Save alex-frankel/79a92c5832ea49093a3cac52d5d280dd to your computer and use it in GitHub Desktop.
Tests multiple different cases where a resource, module, or property condition evaluates to false, and therefore should never evaluate in the first place.
{
// See https://aka.ms/bicep/config for more information on Bicep configuration options
// Press CTRL+SPACE/CMD+SPACE at any location to see Intellisense suggestions
"analyzers": {
"core": {
"rules": {
"no-unused-params": {
"level": "warning"
}
}
}
},
"experimentalFeaturesEnabled": {
// "resourceDerivedTypes": true,
// "symbolicNameCodegen": true, //enable or disable according to test
"optionalModuleNames": true
}
}
// *** TEST 1 ***
// preflight failure for condition false resource, payload is invalid according to RP-specific preflight
resource falseResource 'Microsoft.Storage/storageAccounts@2023-01-01' = if(false) {
name: 'abc123'
location: resourceGroup().location
kind: 'StorageV2'
sku: {
name: 'Standard_LRS'
}
}
// // result without symbolic name: success
// // result with symbolic name: success
// // *** TEST 2 ***
// // parameter reference causes failure, but params shouldn't even be evaluated
// // seems to be the issue described in both:
// // * https://github.com/Azure/bicep/issues/2371
// // * https://github.com/Azure/bicep/issues/3990
module falseModule 'mod.bicep' = if(false) {
params: {
test: falseResource.properties.primaryEndpoints.blob
}
}
// // result without symbolic name: !! FAILURE !!
// // result with symbolic name: success
// // *** TEST 3 ***
// // https://github.com/Azure/bicep/issues/3750
// // ternary where first operand is false, so the second operand should never evaluate
module trueModuleNullReferenceShouldEvaluate 'mod.bicep' = {
params: {
test: false ? falseResource.properties.primaryEndpoints.blob : ''
}
}
// // result without symbolic name: !!! FAILURE !!!
// // result with symbolic name: success
// // *** TEST 4 ***
// // https://github.com/Azure/bicep/issues/3750
// // THIS TEST SHOULD FAIL
// ternary where first operand is true, so second operand SHOULD evaluate and fail
module trueModuleNullReferenceShouldNotEvaluate 'mod.bicep' = {
params: {
test: true ? falseResource.properties.primaryEndpoints.blob : ''
}
}
// // result without symbolic name: !!! FAILURE !!!
// // result with symbolic name: !!! FAILURE !!!
// *** TEST 5 ***
// foo expression evaluates to false based on parameter value, payload is invalid according to Deployments
param foo string = ''
resource bar 'Microsoft.Storage/storageAccounts@2023-01-01' = if (foo != '') {
name: foo
location: resourceGroup().location
kind: 'StorageV2'
sku: {
name: 'Standard_LRS'
}
}
// result without symbolic name: !!! FAILURE !!!
// result with symbolic name: !!! FAILURE !!!
// sidenote: with condiiton true, it passed storage preflight if the properties were missing
@anthony-c-martin
Copy link

anthony-c-martin commented Apr 22, 2024

The other thing I've seen some complaints about is the scenario like this one:

param foo string = ''

resource bar 'Microsoft.Storage/storageAccounts@2023-01-01' = if (foo != '') {
  name: foo
}

It's not a huge deal to work around, but it does force awkward code like this:

param foo string = ''

resource bar 'Microsoft.Storage/storageAccounts@2023-01-01' = if (foo != '') {
  name: foo != '' ? foo : 'NOTUSED'
}

@alex-frankel
Copy link
Author

alex-frankel commented Apr 22, 2024

Just updated. This example succeeds in both cases.

Fixed adding the new case, which appears to be unresolved for symbolic names too.

Adding internal ADO work item for tracking: https://msazure.visualstudio.com/One/_workitems/edit/27778828

Fixing this item should also resolve: Azure/bicep#13937

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment