Skip to content

Commit

Permalink
Implement: Finalize default levels for linting rules (#13847)
Browse files Browse the repository at this point in the history
Fixes #8013 

NOTE: breaking change (changes 3 rules that were warning by default to
off by default):

- explicit-values-for-loc-params
- no-hardcoded-location
- no-loc-expr-outside-params

- [ ] QUESTION: Should the "no unused..." rules be in the category "best
practice"? "style"? something else?

###### Microsoft Reviewers: [Open in
CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/Azure/bicep/pull/13847)

---------

Co-authored-by: Stephen Weatherford <Stephen.Weatherford.com>
Co-authored-by: Bicep Automation <bicep@noreply.github.com>
  • Loading branch information
StephenWeatherford and Bicep Automation authored Apr 16, 2024
1 parent 2120e6c commit a2d5557
Show file tree
Hide file tree
Showing 76 changed files with 318 additions and 277 deletions.
2 changes: 1 addition & 1 deletion src/Bicep.Core.IntegrationTests/ScenarioTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5681,7 +5681,7 @@ public void Test_Issue12347()
var result = CompilationHelper.Compile(
Services
.WithFeatureOverrides(new(TestContext, ExtensibilityEnabled: true))
.WithConfigurationPatch(x => x.WithAnalyzers(x.Analyzers.SetValue("core.rules.use-recent-api-versions.level", "error"))),
.WithConfigurationPatch(x => x.WithAnalyzersConfiguration(x.Analyzers.SetValue("core.rules.use-recent-api-versions.level", "error"))),
("main.bicep", """
provider kubernetes with {
kubeConfig: 'config'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ var vmProperties = {
resource vm 'Microsoft.Compute/virtualMachines@2020-12-01' = {
name: 'vm'
location: 'West US'
//@[12:21) [no-hardcoded-location (Warning)] A resource location should not use a hard-coded string or variable value. Please use a parameter value, an expression, or the string 'global'. Found: 'West US' (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-hardcoded-location)) |'West US'|
#disable-next-line BCP036 BCP037
properties: vmProperties
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ var vmProperties = {
resource vm 'Microsoft.Compute/virtualMachines@2020-12-01' = {
name: 'vm'
location: 'West US'
//@[12:21) [no-hardcoded-location (Warning)] A resource location should not use a hard-coded string or variable value. Please use a parameter value, an expression, or the string 'global'. Found: 'West US' (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-hardcoded-location)) |'West US'|
properties: vmProperties
//@[14:26) [BCP036 (Warning)] The property "enabled" expected a value of type "bool | null" but the provided value in source declaration "vmProperties" is of type "123". If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |vmProperties|
//@[14:26) [BCP036 (Warning)] The property "evictionPolicy" expected a value of type "null | string" but the provided value in source declaration "vmProperties" is of type "true". If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |vmProperties|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ resource res1 'Microsoft.Storage/storageAccounts@2019-06-01' = {
name: res2.n
//@[08:12) [BCP080 (Error)] The expression is involved in a cycle ("res2" -> "res1"). (CodeDescription: none) |res2|
location: 'l'
//@[12:15) [no-hardcoded-location (Warning)] A resource location should not use a hard-coded string or variable value. Please use a parameter value, an expression, or the string 'global'. Found: 'l' (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-hardcoded-location)) |'l'|
sku: {
name: 'Premium_LRS'
// #completionTest(15) -> empty
Expand All @@ -75,7 +74,6 @@ resource res2 'Microsoft.Storage/storageAccounts@2019-06-01' = {
name: res1.name
//@[08:12) [BCP080 (Error)] The expression is involved in a cycle ("res1" -> "res2"). (CodeDescription: none) |res1|
location: 'l'
//@[12:15) [no-hardcoded-location (Warning)] A resource location should not use a hard-coded string or variable value. Please use a parameter value, an expression, or the string 'global'. Found: 'l' (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-hardcoded-location)) |'l'|
sku: {
name: 'Premium_LRS'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ var inArray = [
resource stg 'Microsoft.Storage/storageAccounts@2021-09-01' = [for i in range(0, 2): {
name: 'antteststg${i}'
location: 'West US'
//@[12:21) [no-hardcoded-location (Warning)] A resource location should not use a hard-coded string or variable value. Please use a parameter value, an expression, or the string 'global'. Found: 'West US' (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-hardcoded-location)) |'West US'|
sku: {
name: 'Standard_LRS'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ module moduleWithBadScope './empty.bicep' = {
resource runtimeValidRes1 'Microsoft.Storage/storageAccounts@2019-06-01' = {
name: 'runtimeValidRes1Name'
location: 'westeurope'
//@[012:024) [no-hardcoded-location (Warning)] A resource location should not use a hard-coded string or variable value. Please use a parameter value, an expression, or the string 'global'. Found: 'westeurope' (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-hardcoded-location)) |'westeurope'|
kind: 'Storage'
sku: {
name: 'Standard_GRS'
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
resource foo 'Microsoft.Storage/storageAccounts@2022-09-01' = {
name: 'foo'
location: 'westus'
//@[12:20) [no-hardcoded-location (Warning)] A resource location should not use a hard-coded string or variable value. Please use a parameter value, an expression, or the string 'global'. Found: 'westus' (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-hardcoded-location)) |'westus'|
sku: {
name: 'Standard_LRS'
}
Expand All @@ -14,7 +13,6 @@ resource foo 'Microsoft.Storage/storageAccounts@2022-09-01' = {
resource foos 'Microsoft.Storage/storageAccounts@2022-09-01' = [for i in range(0, 2): {
name: 'foo-${i}'
location: 'westus'
//@[12:20) [no-hardcoded-location (Warning)] A resource location should not use a hard-coded string or variable value. Please use a parameter value, an expression, or the string 'global'. Found: 'westus' (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-hardcoded-location)) |'westus'|
sku: {
name: 'Standard_LRS'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ var subName = subscription().name
// this does not work at the resource group scope
var invalidLocationVar = deployment().location
//@[04:22) [no-unused-vars (Warning)] Variable "invalidLocationVar" is declared but never used. (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-unused-vars)) |invalidLocationVar|
//@[25:46) [no-loc-expr-outside-params (Warning)] Use a parameter here instead of 'deployment().location'. 'resourceGroup().location' and 'deployment().location' should only be used as a default value for parameters. (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-loc-expr-outside-params)) |deployment().location|
//@[38:46) [BCP053 (Error)] The type "deployment" does not contain property "location". Available properties include "name", "properties". (CodeDescription: none) |location|

var invalidEnvironmentVar = environment().aosdufhsad
Expand Down Expand Up @@ -349,7 +348,6 @@ var zoneInput = []
resource zones 'Microsoft.Network/dnsZones@2018-05-01' = [for (zone, i) in zoneInput: {
name: zone
location: az.resourceGroup().location
//@[12:39) [no-loc-expr-outside-params (Warning)] Use a parameter here instead of 'resourceGroup().location'. 'resourceGroup().location' and 'deployment().location' should only be used as a default value for parameters. (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-loc-expr-outside-params)) |az.resourceGroup().location|
//@[15:28) [BCP067 (Error)] Cannot call functions on type "1". An "object" type is required. (CodeDescription: none) |resourceGroup|
}]
var inlinedVariable = zones[0].properties.zoneType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ param index int
resource singleResource 'Microsoft.Storage/storageAccounts@2019-06-01' = {
name: '${name}single-resource-name'
location: resourceGroup().location
//@[12:036) [no-loc-expr-outside-params (Warning)] Use a parameter here instead of 'resourceGroup().location'. 'resourceGroup().location' and 'deployment().location' should only be used as a default value for parameters. (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-loc-expr-outside-params)) |resourceGroup().location|
kind: 'StorageV2'
sku: {
name: 'Standard_LRS'
Expand Down Expand Up @@ -109,7 +108,6 @@ resource storageAccounts2 'Microsoft.Storage/storageAccounts@2019-06-01' = [for
resource firstSet 'Microsoft.Storage/storageAccounts@2019-06-01' = [for (i,ii) in range(0, length(accounts)): {
name: '${name}-set1-${i}-${ii}'
location: resourceGroup().location
//@[12:036) [no-loc-expr-outside-params (Warning)] Use a parameter here instead of 'resourceGroup().location'. 'resourceGroup().location' and 'deployment().location' should only be used as a default value for parameters. (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-loc-expr-outside-params)) |resourceGroup().location|
kind: 'StorageV2'
sku: {
name: 'Standard_LRS'
Expand All @@ -119,7 +117,6 @@ resource firstSet 'Microsoft.Storage/storageAccounts@2019-06-01' = [for (i,ii) i
resource secondSet 'Microsoft.Storage/storageAccounts@2019-06-01' = [for (i,iii) in range(0, length(accounts)): {
name: '${name}-set2-${i}-${iii}'
location: resourceGroup().location
//@[12:036) [no-loc-expr-outside-params (Warning)] Use a parameter here instead of 'resourceGroup().location'. 'resourceGroup().location' and 'deployment().location' should only be used as a default value for parameters. (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-loc-expr-outside-params)) |resourceGroup().location|
kind: 'StorageV2'
sku: {
name: 'Standard_LRS'
Expand All @@ -133,7 +130,6 @@ resource secondSet 'Microsoft.Storage/storageAccounts@2019-06-01' = [for (i,iii)
resource anotherSingleResource 'Microsoft.Storage/storageAccounts@2019-06-01' = {
name: '${name}single-resource-name'
location: resourceGroup().location
//@[12:036) [no-loc-expr-outside-params (Warning)] Use a parameter here instead of 'resourceGroup().location'. 'resourceGroup().location' and 'deployment().location' should only be used as a default value for parameters. (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-loc-expr-outside-params)) |resourceGroup().location|
kind: 'StorageV2'
sku: {
name: 'Standard_LRS'
Expand All @@ -149,7 +145,6 @@ var vnetConfigurations = [
{
name: 'one'
location: resourceGroup().location
//@[14:038) [no-loc-expr-outside-params (Warning)] Use a parameter here instead of 'resourceGroup().location'. 'resourceGroup().location' and 'deployment().location' should only be used as a default value for parameters. (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-loc-expr-outside-params)) |resourceGroup().location|
}
{
name: 'two'
Expand Down Expand Up @@ -276,15 +271,13 @@ resource duplicatedNames 'Microsoft.Network/dnsZones@2018-05-01' = [for (zone,i)
//@[09:024) [BCP179 (Warning)] Unique resource or deployment name is required when looping. The loop item variable "zone" or the index variable "i" must be referenced in at least one of the value expressions of the following properties in the loop body: "name" (CodeDescription: none) |duplicatedNames|
name: 'no loop variable'
location: 'eastus'
//@[12:020) [no-hardcoded-location (Warning)] A resource location should not use a hard-coded string or variable value. Please use a parameter value, an expression, or the string 'global'. Found: 'eastus' (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-hardcoded-location)) |'eastus'|
}]

// reference to a resource collection whose name expression does not reference any loop variables
resource referenceToDuplicateNames 'Microsoft.Network/dnsZones@2018-05-01' = [for (zone,i) in []: {
//@[09:034) [BCP179 (Warning)] Unique resource or deployment name is required when looping. The loop item variable "zone" or the index variable "i" must be referenced in at least one of the value expressions of the following properties in the loop body: "name" (CodeDescription: none) |referenceToDuplicateNames|
name: 'no loop variable 2'
location: 'eastus'
//@[12:020) [no-hardcoded-location (Warning)] A resource location should not use a hard-coded string or variable value. Please use a parameter value, an expression, or the string 'global'. Found: 'eastus' (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-hardcoded-location)) |'eastus'|
dependsOn: [
duplicatedNames[index]
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ param index int
resource singleResource 'Microsoft.Storage/storageAccounts@2019-06-01' = {
name: '${name}single-resource-name'
location: resourceGroup().location
//@[12:036) [no-loc-expr-outside-params (Warning)] Use a parameter here instead of 'resourceGroup().location'. 'resourceGroup().location' and 'deployment().location' should only be used as a default value for parameters. (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-loc-expr-outside-params)) |resourceGroup().location|
kind: 'StorageV2'
sku: {
name: 'Standard_LRS'
Expand Down Expand Up @@ -109,7 +108,6 @@ resource storageAccounts2 'Microsoft.Storage/storageAccounts@2019-06-01' = [for
resource firstSet 'Microsoft.Storage/storageAccounts@2019-06-01' = [for i in range(0, length(accounts)): {
name: '${name}-set1-${i}'
location: resourceGroup().location
//@[12:036) [no-loc-expr-outside-params (Warning)] Use a parameter here instead of 'resourceGroup().location'. 'resourceGroup().location' and 'deployment().location' should only be used as a default value for parameters. (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-loc-expr-outside-params)) |resourceGroup().location|
kind: 'StorageV2'
sku: {
name: 'Standard_LRS'
Expand All @@ -119,7 +117,6 @@ resource firstSet 'Microsoft.Storage/storageAccounts@2019-06-01' = [for i in ran
resource secondSet 'Microsoft.Storage/storageAccounts@2019-06-01' = [for i in range(0, length(accounts)): {
name: '${name}-set2-${i}'
location: resourceGroup().location
//@[12:036) [no-loc-expr-outside-params (Warning)] Use a parameter here instead of 'resourceGroup().location'. 'resourceGroup().location' and 'deployment().location' should only be used as a default value for parameters. (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-loc-expr-outside-params)) |resourceGroup().location|
kind: 'StorageV2'
sku: {
name: 'Standard_LRS'
Expand All @@ -133,7 +130,6 @@ resource secondSet 'Microsoft.Storage/storageAccounts@2019-06-01' = [for i in ra
resource anotherSingleResource 'Microsoft.Storage/storageAccounts@2019-06-01' = {
name: '${name}single-resource-name'
location: resourceGroup().location
//@[12:036) [no-loc-expr-outside-params (Warning)] Use a parameter here instead of 'resourceGroup().location'. 'resourceGroup().location' and 'deployment().location' should only be used as a default value for parameters. (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-loc-expr-outside-params)) |resourceGroup().location|
kind: 'StorageV2'
sku: {
name: 'Standard_LRS'
Expand All @@ -149,7 +145,6 @@ var vnetConfigurations = [
{
name: 'one'
location: resourceGroup().location
//@[14:038) [no-loc-expr-outside-params (Warning)] Use a parameter here instead of 'resourceGroup().location'. 'resourceGroup().location' and 'deployment().location' should only be used as a default value for parameters. (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-loc-expr-outside-params)) |resourceGroup().location|
}
{
name: 'two'
Expand Down Expand Up @@ -273,15 +268,13 @@ resource duplicatedNames 'Microsoft.Network/dnsZones@2018-05-01' = [for zone in
//@[09:024) [BCP179 (Warning)] Unique resource or deployment name is required when looping. The loop item variable "zone" must be referenced in at least one of the value expressions of the following properties: "name" (CodeDescription: none) |duplicatedNames|
name: 'no loop variable'
location: 'eastus'
//@[12:020) [no-hardcoded-location (Warning)] A resource location should not use a hard-coded string or variable value. Please use a parameter value, an expression, or the string 'global'. Found: 'eastus' (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-hardcoded-location)) |'eastus'|
}]

// reference to a resource collection whose name expression does not reference any loop variables
resource referenceToDuplicateNames 'Microsoft.Network/dnsZones@2018-05-01' = [for zone in []: {
//@[09:034) [BCP179 (Warning)] Unique resource or deployment name is required when looping. The loop item variable "zone" must be referenced in at least one of the value expressions of the following properties: "name" (CodeDescription: none) |referenceToDuplicateNames|
name: 'no loop variable 2'
location: 'eastus'
//@[12:020) [no-hardcoded-location (Warning)] A resource location should not use a hard-coded string or variable value. Please use a parameter value, an expression, or the string 'global'. Found: 'eastus' (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-hardcoded-location)) |'eastus'|
dependsOn: [
duplicatedNames[index]
]
Expand Down Expand Up @@ -406,7 +399,6 @@ resource indexedResourceCollectionDependency 'Microsoft.Network/frontDoors@2020-
resource filteredZones 'Microsoft.Network/dnsZones@2018-05-01' = [for i in range(0,10): if(i % 3 == 0) {
name: 'zone${i}'
location: resourceGroup().location
//@[12:036) [no-loc-expr-outside-params (Warning)] Use a parameter here instead of 'resourceGroup().location'. 'resourceGroup().location' and 'deployment().location' should only be used as a default value for parameters. (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-loc-expr-outside-params)) |resourceGroup().location|
}]

module filteredModules 'passthrough.bicep' = [for i in range(0,6): if(i % 2 == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ var scripts = take(groups, 2)
resource resourceGroups 'Microsoft.Resources/resourceGroups@2020-06-01' = [for name in groups: {
name: '${prefix}-${name}'
location: 'westus'
//@[12:20) [no-hardcoded-location (Warning)] A resource location should not use a hard-coded string or variable value. Please use a parameter value, an expression, or the string 'global'. Found: 'westus' (CodeDescription: bicep core(https://aka.ms/bicep/linter/no-hardcoded-location)) |'westus'|
}]

module scopedToSymbolicName 'hello.bicep' = [for (name, i) in scripts: {
Expand Down
Loading

0 comments on commit a2d5557

Please sign in to comment.