Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New linter rule no-unnecessary-dependson #4838

Merged
merged 9 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions docs/linter-rules/no-unnecessary-dependson.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# No unnecessary dependsOn

**Code**: no-unnecessary-dependson

**Description**: To reduce confusion in your template, delete any dependsOn entries which are not necessary. Bicep automatically infers most resource dependencies as long as you use expressions that reference other resources instead of hard-coded ids and names.

The following example fails this test because the dependsOn entry `appServicePlan` is already implied by the expression `appServicePlan.id` in the `serverFarmId` property's value.

```bicep
resource appServicePlan 'Microsoft.Web/serverfarms@2020-12-01' = {
name: 'name'
location: resourceGroup().location
sku: {
name: 'F1'
capacity: 1
}
}

resource webApplication 'Microsoft.Web/sites@2018-11-01' = {
name: 'name'
location: resourceGroup().location
properties: {
serverFarmId: appServicePlan.id
}
dependsOn: [
appServicePlan
]
}
```

The following example passes this test.

```bicep
resource appServicePlan 'Microsoft.Web/serverfarms@2020-12-01' = {
name: 'name'
location: resourceGroup().location
sku: {
name: 'F1'
capacity: 1
}
}

resource webApplication 'Microsoft.Web/sites@2018-11-01' = {
name: 'name'
location: resourceGroup().location
properties: {
serverFarmId: appServicePlan.id
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public void HasBuiltInRules()
[DataTestMethod]
[DataRow(AdminUsernameShouldNotBeLiteralRule.Code)]
[DataRow(NoHardcodedEnvironmentUrlsRule.Code)]
[DataRow(NoUnnecessaryDependsOnRule.Code)]
[DataRow(NoUnusedParametersRule.Code)]
[DataRow(NoUnusedVariablesRule.Code)]
[DataRow(PreferInterpolationRule.Code)]
Expand Down
Loading