Skip to content

Add no-module-name linter rule to enforce omitting explicit module names#19556

Merged
anthony-c-martin merged 5 commits into
mainfrom
copilot/add-optional-linter-rule
Apr 30, 2026
Merged

Add no-module-name linter rule to enforce omitting explicit module names#19556
anthony-c-martin merged 5 commits into
mainfrom
copilot/add-optional-linter-rule

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 28, 2026

Bicep supports omitting the name property on module declarations, generating a deterministic name automatically. This adds an opt-in linter rule to enforce that pattern, preventing accidental deployment name collisions between nested modules.

New rule: no-module-name

  • Off by default — enable explicitly in bicepconfig.json
  • Flags any module declaration with an explicit name: property
  • Provides a quick fix to remove the property

Flagged:

module foo 'foo.bicep' = {
  name: 'foo'
  params: { ... }
}

Expected:

module foo 'foo.bicep' = {
  params: { ... }
}

Enable via bicepconfig.json:

{
  "analyzers": {
    "core": {
      "rules": {
        "no-module-name": { "level": "warning" }
      }
    }
  }
}

Changes

  • NoModuleNameRule.cs — rule implementation with code fix (SyntaxModifier.TryRemoveProperty)
  • CoreResources.resx / CoreResources.Designer.cs — description and fix title strings
  • bicepconfig.schema.json — schema entry with rule-def-level-off default
  • NoModuleNameRuleTests.cs — unit tests for diagnostics and code fix
  • Updated ExplicitValuesForLocationParamsRuleTests, WhatIfShortCircuitingRuleTests, NoHardcodedLocationRuleTests to remove explicit module name: properties that would now trigger the rule when all analyzers are enabled in tests

Agent-Logs-Url: https://github.com/Azure/bicep/sessions/a5e60229-a7e9-4051-bc54-3963785984e5

Co-authored-by: anthony-c-martin <38542602+anthony-c-martin@users.noreply.github.com>
Copilot AI changed the title [WIP] Add optional linter rule to enforce optional module names Add no-module-name linter rule to enforce omitting explicit module names Apr 28, 2026
Copilot AI requested a review from anthony-c-martin April 28, 2026 19:01
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 28, 2026

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

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

@anthony-c-martin
Copy link
Copy Markdown
Member

@copilot fix the tests pls

…on tests

Agent-Logs-Url: https://github.com/Azure/bicep/sessions/520ebcc3-5d88-499b-8394-b2e9f1a95a0b

Co-authored-by: anthony-c-martin <38542602+anthony-c-martin@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 28, 2026

@copilot fix the tests pls

Fixed in e20d1bf. The baseline integration tests were failing because WithAnalyzersCodesToDisableConfiguration (used in baseline test setup) calls WithAllAnalyzers() which enabled the new off-by-default no-module-name rule across all sample files. Added NoModuleNameRule.Code to BicepTestConstants.TestAnalyzersToSkip so the rule is excluded from baseline tests (consistent with other opt-in rules like NoHardcodedLocationRule and ExplicitValuesForLocationParamsRule).

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • aka.ms
    • Triggering command: /home/REDACTED/work/bicep/bicep/src/Bicep.Core.IntegrationTests/bin/Debug/net10.0/Bicep.Core.IntegrationTests /home/REDACTED/work/bicep/bicep/src/Bicep.Core.IntegrationTests/bin/Debug/net10.0/Bicep.Core.IntegrationTests --internal-msbuild-node /tmp/0220ea8115f342a09d222086bb97a890/.p --ignore-exit-code 8 data_CRLF/main.bicep LF/main.bicep _LF/main.bicep main.bicep nsions_CRLF/main.bicep F/main.bicep nagementGroup_LF/main.bicep das_�� LF/main.bicep ns_CRLF/main.bicep (dns block)
    • Triggering command: /home/REDACTED/work/bicep/bicep/src/Bicep.Core.IntegrationTests/bin/Debug/net10.0/Bicep.Core.IntegrationTests /home/REDACTED/work/bicep/bicep/src/Bicep.Core.IntegrationTests/bin/Debug/net10.0/Bicep.Core.IntegrationTests --internal-msbuild-node /tmp/01a62709f2644f929c91362debb83c02/.p --ignore-exit-code 8 (dns block)
    • Triggering command: /home/REDACTED/work/bicep/bicep/src/Bicep.Core.IntegrationTests/bin/Debug/net10.0/Bicep.Core.IntegrationTests /home/REDACTED/work/bicep/bicep/src/Bicep.Core.IntegrationTests/bin/Debug/net10.0/Bicep.Core.IntegrationTests --internal-msbuild-node /tmp/760f70c6845f416d9b15ed86e3ab0c34/.p --ignore-exit-code 8 (dns block)
  • eastus.data.mcr.microsoft.com
    • Triggering command: /home/REDACTED/work/bicep/bicep/src/Bicep.Core.IntegrationTests/bin/Debug/net10.0/Bicep.Core.IntegrationTests /home/REDACTED/work/bicep/bicep/src/Bicep.Core.IntegrationTests/bin/Debug/net10.0/Bicep.Core.IntegrationTests --internal-msbuild-node /tmp/0220ea8115f342a09d222086bb97a890/.p --ignore-exit-code 8 data_CRLF/main.bicep LF/main.bicep _LF/main.bicep main.bicep nsions_CRLF/main.bicep F/main.bicep nagementGroup_LF/main.bicep das_�� LF/main.bicep ns_CRLF/main.bicep (dns block)
    • Triggering command: /home/REDACTED/work/bicep/bicep/src/Bicep.Core.IntegrationTests/bin/Debug/net10.0/Bicep.Core.IntegrationTests /home/REDACTED/work/bicep/bicep/src/Bicep.Core.IntegrationTests/bin/Debug/net10.0/Bicep.Core.IntegrationTests --internal-msbuild-node /tmp/01a62709f2644f929c91362debb83c02/.p --ignore-exit-code 8 (dns block)
    • Triggering command: /home/REDACTED/work/bicep/bicep/src/Bicep.Core.IntegrationTests/bin/Debug/net10.0/Bicep.Core.IntegrationTests /home/REDACTED/work/bicep/bicep/src/Bicep.Core.IntegrationTests/bin/Debug/net10.0/Bicep.Core.IntegrationTests --internal-msbuild-node /tmp/760f70c6845f416d9b15ed86e3ab0c34/.p --ignore-exit-code 8 (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@anthony-c-martin anthony-c-martin marked this pull request as ready for review April 29, 2026 01:15
Comment thread src/Bicep.Core/Analyzers/Linter/Rules/NoModuleNameRule.cs
Comment thread src/Bicep.Core.UnitTests/Diagnostics/LinterRuleTests/NoModuleNameRuleTests.cs Outdated
Comment thread src/Bicep.Core/CoreResources.resx Outdated
Copy link
Copy Markdown
Member

@anthony-c-martin anthony-c-martin left a comment

Choose a reason for hiding this comment

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

@copilot please address feedback

…date description text

Agent-Logs-Url: https://github.com/Azure/bicep/sessions/bb4d9091-57d8-45f5-824b-a4215fb6f3cc

Co-authored-by: anthony-c-martin <38542602+anthony-c-martin@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 29, 2026

@copilot please address feedback

Done in b2c8936. Three things addressed:

  1. Simplified null check in NoModuleNameRule.cs to use module.TryGetBodyProperty(...) extension method
  2. Simplified NoModuleNameRuleTests.cs following the UseParentPropertyRuleTests pattern — removed the duplicative 15-line AssertCodeFix body and CompileAndTest wrapper, replacing with one-liners; the code-fix test enables the rule via a bicepconfig.json supporting file since the rule is off by default
  3. Updated description text in CoreResources.resx, CoreResources.Designer.cs, and bicepconfig.schema.json

@anthony-c-martin
Copy link
Copy Markdown
Member

@copilot see latest feedback

…rty pattern

Agent-Logs-Url: https://github.com/Azure/bicep/sessions/13065bc1-d90f-42db-abef-ed54995bc8dc

Co-authored-by: anthony-c-martin <38542602+anthony-c-martin@users.noreply.github.com>
@anthony-c-martin anthony-c-martin merged commit 1d4fe17 into main Apr 30, 2026
41 checks passed
@anthony-c-martin anthony-c-martin deleted the copilot/add-optional-linter-rule branch April 30, 2026 17:54
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 an optional linter rule to enforce optional module names

3 participants