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

Added optional module names as an experimental feature #12600

Merged
merged 1 commit into from
Dec 2, 2023

Conversation

majastrz
Copy link
Member

@majastrz majastrz commented Dec 2, 2023

Overview

Currently, module names are a required property on module declarations. Modules are compiled to nested deployments in the JSON and the name expression is transpiled as-is. With deeply-nested module hierarchies it is very easy to create module names that conflict in grandchild deployments, which causes difficult to diagnose failures at deploy time. This partially fixes #3897.

I'm introducing an experimental feature that once enabled, makes the name property in module declarations optional. There is no change in behavior if the feature is disabled.

With the feature enabled and name omitted in a module, we generate the following name expression in the JSON:

  • In a module loop -> [format('<symbolic name prefix>-{0}-{1}', copyIndex(), uniqueString('<symbolic name>', deployment().name))]
  • In a single module -> [format('<symbolic name prefix>-{0}', uniqueString('<symbolic name>', deployment().name))]

above represents the full symbolic name of the enclosing module declaration. is a substring of that starts at index 0 and takes as many characters as possible to fit within a deployment name.

Considerations

  • The uniqueString() function always returns 13 characters
  • We assume that the length of copyIndex() serialized to a string is always 3 characters since the max value is 800. While this approach leaves up to 2 characters unused in module loops with 1 or 2-digit iteration counts, it makes the symbolic name prefix consistent, improving readability in the portal.
  • The full symbolic name and current deployment name are hashed into the module name to reduce the possibility of clashes. Chaining of deployment name hashes also avoids clashes with grandchild deployments using duplicated symbolic names.

Bicep authors should not take a dependency on the algorithm used to calculate the name. We reserve the right to change it at any time in the future without notice.

We will add a linter for module names in the future, but it is out of scope of this PR.

Portal experience

Module loop

main.bicep

param count int

module veryLongSymbolicNameThatExceedsTheMaximumPrefixLengthLimitSoICanTestMyAlgorithm 'mod.bicep' = [for i in range(0, count): {
  params: {
    templateSpecName: 'ts-${i}'
  }
}]

mod.bicep

param templateSpecName string

resource ts 'Microsoft.Resources/templateSpecs@2022-02-01' = {
  name: templateSpecName
  location: resourceGroup().location
}

Azure Portal (count = 500)
image

Module nesting

main.bicep

module veryLongSymbolicNameThatExceedsTheMaximumPrefixLengthLimitSoICanTestMyAlgorithm 'mod.bicep' = {
  params: {
    name: 'one'
  }
}

module veryLongSymbolicNameThatExceedsTheMaximumPrefixLengthLimitSoICanTestMyAlgorithm2 'mod.bicep' = {
  params: {
    name: 'two'
  }
}

mod.bicep

param name string

module veryLongSymbolicNameThatExceedsTheMaximumPrefixLengthLimitSoICanTestMyAlgorithm 'mod2.bicep' = {
  params: {
    name: name
  }
}

mod2.bicep

param name string

resource ts 'Microsoft.Resources/templateSpecs@2022-02-01' = {
  name: name
  location: resourceGroup().location
}

Azure Portal (count = 500)
image

Microsoft Reviewers: Open in CodeFlow

Copy link
Contributor

github-actions bot commented Dec 2, 2023

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

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

[DataRow("hello", "hello")]
[DataRow("this______has_________fifty_____________characters", "this______has_________fifty_____________characters")]
[DataRow("this______has_________fifty_one__________characters", "this______has_________fifty_one__________character")]
[DataRow("module_symbolic_name_with_a_super_long_name_that_has_seventy_seven_characters", "module_symbolic_name_with_a_super_long_name_that_h")]
Copy link
Member Author

Choose a reason for hiding this comment

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

module_symbolic_name_with_a_super_long_name_that_has_seventy_seven_characters

Guess how long this string is :D

Copy link
Contributor

Choose a reason for hiding this comment

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

42

Copy link
Member Author

Choose a reason for hiding this comment

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

:D

@majastrz majastrz marked this pull request as ready for review December 2, 2023 01:18
Copy link
Contributor

github-actions bot commented Dec 2, 2023

Test Results

     143 files  +     11       143 suites  +11   5h 49m 2s ⏱️ + 1h 15m 4s
10 870 tests +     10  10 868 ✔️ +       8  0 💤 ±0  2 +2 
54 533 runs  +2 170  54 531 ✔️ +2 168  0 💤 ±0  2 +2 

For more details on these failures, see this check.

Results for commit 776b94c. ± Comparison against base commit edf1324.

♻️ This comment has been updated with latest results.

@majastrz majastrz merged commit 1667ddc into main Dec 2, 2023
46 of 47 checks passed
@majastrz majastrz deleted the majastrz/module-name branch December 2, 2023 09:55
majastrz added a commit that referenced this pull request Jan 30, 2024
Added documentation for the "optional module names" experimental feature
that was added by #12600 and is related to #3897.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove the name property from module references
2 participants