Skip to content

Commit 004c9ed

Browse files
authored
Merge pull request #287100 from mumian/0919-linter-what-if-short-circuiting
new linter rule - what-if-short-circuiting
2 parents ba0a875 + 8ad1498 commit 004c9ed

File tree

4 files changed

+89
-2
lines changed

4 files changed

+89
-2
lines changed

articles/azure-resource-manager/bicep/bicep-config-linter.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Linter settings for Bicep config
33
description: Describes how to customize configuration values for the Bicep linter
44
ms.topic: conceptual
55
ms.custom: devx-track-bicep
6-
ms.date: 07/30/2024
6+
ms.date: 09/19/2024
77
---
88

99
# Add linter settings in the Bicep config file
@@ -135,6 +135,9 @@ The following example shows the rules that are available for configuration.
135135
},
136136
"use-stable-vm-image": {
137137
"level": "warning"
138+
},
139+
"what-if-short-circuiting": {
140+
"level": "warning"
138141
}
139142
}
140143
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
title: Linter rule - what-if short circuiting
3+
description: Linter rule - what-if short circuiting
4+
ms.topic: reference
5+
ms.custom: devx-track-bicep
6+
ms.date: 09/19/2024
7+
---
8+
9+
# Linter rule - what-if short circuiting
10+
11+
This rule detects when runtime values are passed as parameters to modules, which in turn use them to determine resource IDs (such as when the parameter is used to determine the name, subscriptionId, resourceGroup, condition, scope, or apiVersion of one or more resources within the module) , and flags potential what-if short-circuiting.
12+
13+
> [!NOTE]
14+
> This rule is off by default, change the level in [bicepconfig.json](./bicep-config-linter.md) to enable it.
15+
16+
## Linter rule code
17+
18+
Use the following value in the [Bicep configuration file](bicep-config-linter.md) to customize rule settings:
19+
20+
`what-if-short-circuiting`
21+
22+
## Solution
23+
24+
This rule checks for runtime values used to determine resource IDs within modules. It alerts you if your Bicep code could cause what-if short-circuiting. In the example below, **appServiceOutputs** and **appServiceTests** would be flagged for what-if short-circuiting because they pass runtime values as parameters to the module, which uses them when naming the resource:
25+
26+
**main.bicep**
27+
28+
```bicep
29+
resource storageAccount 'Microsoft.Storage/storageAccounts@2023-05-01' = {
30+
name: 'storageAccountName'
31+
location: 'eastus'
32+
sku: {
33+
name: 'Standard_LRS'
34+
}
35+
kind: 'StorageV2'
36+
}
37+
38+
module appServiceModule 'modules/appService.bicep' = {
39+
name: 'appService2'
40+
params: {
41+
appServiceName: 'test'
42+
}
43+
}
44+
45+
module appServiceOutputs 'modules/appService.bicep' = {
46+
name: 'appService3'
47+
params: {
48+
appServiceName: appServiceModule.outputs.outputName
49+
}
50+
}
51+
52+
module appServiceTest 'modules/appService.bicep' = {
53+
name:'test3'
54+
params: {
55+
appServiceName: storageAccount.properties.accessTier
56+
}
57+
}
58+
```
59+
60+
**modules/appService.bicep**
61+
62+
```bicep
63+
param appServiceName string
64+
65+
resource appServiceApp 'Microsoft.Web/sites@2023-12-01' = {
66+
name: appServiceName
67+
location: 'eastus'
68+
properties: {
69+
httpsOnly: true
70+
}
71+
}
72+
73+
output outputName string = 'outputName'
74+
```
75+
76+
To avoid this issue, use deployment-time constants for values that are used in determining resource IDs.
77+
78+
## Next steps
79+
80+
For more information about the linter, see [Use Bicep linter](./linter.md).

articles/azure-resource-manager/bicep/linter.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Use Bicep linter
33
description: Learn how to use Bicep linter.
44
ms.topic: how-to
55
ms.custom: devx-track-bicep
6-
ms.date: 07/30/2024
6+
ms.date: 09/19/2024
77
---
88

99
# Use Bicep linter
@@ -55,6 +55,7 @@ The default set of linter rules is minimal and taken from [arm-ttk test cases](.
5555
- [use-secure-value-for-secure-inputs](./linter-rule-use-secure-value-for-secure-inputs.md)
5656
- [use-stable-resource-identifiers](./linter-rule-use-stable-resource-identifier.md)
5757
- [use-stable-vm-image](./linter-rule-use-stable-vm-image.md)
58+
- [what-if-short-circuiting](./linter-rule-what-if-short-circuiting.md)
5859

5960
You can customize how the linter rules are applied. To overwrite the default settings, add a **bicepconfig.json** file and apply custom settings. For more information about applying those settings, see [Add custom settings in the Bicep config file](bicep-config-linter.md).
6061

articles/azure-resource-manager/bicep/toc.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,9 @@
563563
- name: Use stable VM image
564564
displayName: linter
565565
href: linter-rule-use-stable-vm-image.md
566+
- name: What-if short-circuiting
567+
displayName: linter
568+
href: linter-rule-what-if-short-circuiting.md
566569
- name: Deploy
567570
items:
568571
- name: What-if check

0 commit comments

Comments
 (0)