Skip to content

Add linter rule to warn for unrecognized resource types in reference/list* functions#19303

Merged
majastrz merged 2 commits intoAzure:mainfrom
brendandburns:linter_rule
Mar 31, 2026
Merged

Add linter rule to warn for unrecognized resource types in reference/list* functions#19303
majastrz merged 2 commits intoAzure:mainfrom
brendandburns:linter_rule

Conversation

@brendandburns
Copy link
Copy Markdown
Member

@brendandburns brendandburns commented Mar 30, 2026

Description

Fixes #19172

Adds a new linter rule use-recognized-resource-type that emits a warning when a reference or list* function call uses a resource type that is not recognized by Bicep's type system.

What the rule does

  • Inspects the first argument of reference() and list*() function calls
  • Extracts resource types from string literals, variable references, and resourceId() wrapper calls
  • Checks the extracted type against known Azure resource types via ApiVersionProvider
  • Suggests similar type names via spell-checking when the type is unrecognized
  • Defaults to Warning level (PotentialCodeIssues category)

Example

// This will emit a warning because 'Microsoft.Foo/bar' is not a recognized resource type
output foo object = reference('Microsoft.Foo/bar', '2020-01-01')

Files changed

File Purpose
src/Bicep.Core/Analyzers/Linter/Rules/UseRecognizedResourceTypeRule.cs New linter rule implementation
src/Bicep.Core/CoreResources.resx Resource strings for description and messages
src/Bicep.Core/CoreResources.Designer.cs Auto-generated resource accessors
src/vscode-bicep/schemas/bicepconfig.schema.json Schema entry for configuring the rule
src/Bicep.Core.UnitTests/.../UseRecognizedResourceTypeRuleTests.cs Unit tests
Microsoft Reviewers: Open in CodeFlow

@brendandburns
Copy link
Copy Markdown
Member Author

@copilot please fix CI/CD errors.

public new const string Code = "use-recognized-resource-type";

private static readonly Regex ResourceTypeRegex = new(
"^ [a-z]+\\.[a-z]+ (\\/ [a-z]+)+ $",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a copy of this regex in UseRecentApiVersionRule. Could we move this into some sort of "constants" or "shared" class in the same directory?

…list* functions

Fixes Azure#19172

Adds a new linter rule 'use-recognized-resource-type' that emits a
warning when a 'reference' or 'list*' function call uses a resource type
that is not recognized by Bicep's type system.

The rule:
- Inspects the first argument of reference() and list*() calls
- Extracts resource types from string literals, variable references,
  and resourceId() wrapper calls
- Checks the type against known Azure resource types
- Suggests similar type names via spell-checking when available
- Defaults to Warning level (PotentialCodeIssues category)
- Extract ResourceTypeRegex into LinterResourceTypePatterns shared class
- Update UseRecognizedResourceTypeRule and UseRecentApiVersionRule to use shared regex
- Update Outputs_CRLF and Variables_LF baseline diagnostics for new rule
@majastrz majastrz merged commit 88cd223 into Azure:main Mar 31, 2026
41 checks passed
@github-actions
Copy link
Copy Markdown
Contributor

Test this change out locally with the following install scripts (Action run 23817454767)

VSCode
  • Mac/Linux
    bash <(curl -Ls https://aka.ms/bicep/nightly-vsix.sh) --run-id 23817454767
  • Windows
    iex "& { $(irm https://aka.ms/bicep/nightly-vsix.ps1) } -RunId 23817454767"
Azure CLI
  • Mac/Linux
    bash <(curl -Ls https://aka.ms/bicep/nightly-cli.sh) --run-id 23817454767
  • Windows
    iex "& { $(irm https://aka.ms/bicep/nightly-cli.ps1) } -RunId 23817454767"

Copilot AI pushed a commit that referenced this pull request Apr 1, 2026
…list* functions (#19303)

## Description

Fixes #19172

Adds a new linter rule `use-recognized-resource-type` that emits a
warning when a `reference` or `list*` function call uses a resource type
that is not recognized by Bicep's type system.

### What the rule does

- Inspects the first argument of `reference()` and `list*()` function
calls
- Extracts resource types from string literals, variable references, and
`resourceId()` wrapper calls
- Checks the extracted type against known Azure resource types via
`ApiVersionProvider`
- Suggests similar type names via spell-checking when the type is
unrecognized
- Defaults to **Warning** level (`PotentialCodeIssues` category)

### Example

```bicep
// This will emit a warning because 'Microsoft.Foo/bar' is not a recognized resource type
output foo object = reference('Microsoft.Foo/bar', '2020-01-01')
```

### Files changed

| File | Purpose |
|------|---------|
|
`src/Bicep.Core/Analyzers/Linter/Rules/UseRecognizedResourceTypeRule.cs`
| New linter rule implementation |
| `src/Bicep.Core/CoreResources.resx` | Resource strings for description
and messages |
| `src/Bicep.Core/CoreResources.Designer.cs` | Auto-generated resource
accessors |
| `src/vscode-bicep/schemas/bicepconfig.schema.json` | Schema entry for
configuring the rule |
| `src/Bicep.Core.UnitTests/.../UseRecognizedResourceTypeRuleTests.cs` |
Unit tests |

###### Microsoft Reviewers: [Open in
CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/Azure/bicep/pull/19303)
Co-authored-by: brendandburns <5751682+brendandburns@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a linter rule to emit warnings for unrecognized resource types in the reference function

2 participants