-
Notifications
You must be signed in to change notification settings - Fork 779
Mark conditional resources as nullable for the purposes of property access #16987
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
base: main
Are you sure you want to change the base?
Conversation
…en it is a resource with a condition
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR addresses issue #3750 by updating tests to correctly mark conditional resources as nullable when accessing their properties, ensuring that property access on conditionally available resources does not lead to runtime failures. Key changes include:
- Removing explicit resourceId(...) calls in secure output test assertions in favor of string symbolic names.
- Updating resource reference functions in resource list tests to use resourceId(...) instead of literal strings.
- Adding non-null assertions to resource property accesses in various test scenarios.
Reviewed Changes
Copilot reviewed 24 out of 38 changed files in this pull request and generated no comments.
Show a summary per file
File | Description |
---|---|
src/Bicep.Core.IntegrationTests/SecureOutputsTests.cs | Replaced resourceId(...) with symbolic string names in secure output value assertions. |
src/Bicep.Core.IntegrationTests/Scenarios/ResourceListFunctionTests.cs | Updated listKeys(...) calls to use resourceId(...) consistently for resource references. |
src/Bicep.Core.IntegrationTests/Scenarios/InliningResourcesAndModulesTests.cs | Added non-null assertions to resource property accesses. |
src/Bicep.Core.IntegrationTests/ScenarioTests.cs | Modified outputs to ensure correct resolution when conditional resources are used. |
src/Bicep.Core.IntegrationTests/DereferenceTests.cs | Inserted non-null operators to guard against potentially null resource properties. |
src/Bicep.Core.IntegrationTests/DeployTimeConstantTests.cs | Adjusted resource name lookups with non-null assertions for improved safety. |
Files not reviewed (14)
- src/Bicep.Core.IntegrationTests/Files/SymbolicNameTests/ResourceInfo/sql-database-with-management/modules/sql-database.bicep: Language not supported
- src/Bicep.Core.IntegrationTests/Files/SymbolicNameTests/ResourceInfo/sql-database-with-management/modules/sql-logical-server.bicep: Language not supported
- src/Bicep.Core.Samples/Files/baselines/InvalidModules_LF/main.diagnostics.bicep: Language not supported
- src/Bicep.Core.Samples/Files/baselines/InvalidResources_CRLF/main.diagnostics.bicep: Language not supported
- src/Bicep.Core.Samples/Files/baselines/Loops_LF/main.diagnostics.bicep: Language not supported
- src/Bicep.Core.Samples/Files/baselines/Modules_CRLF/main.json: Language not supported
- src/Bicep.Core.Samples/Files/baselines/Modules_CRLF/main.sourcemap.bicep: Language not supported
- src/Bicep.Core.Samples/Files/baselines/Outputs_CRLF/main.json: Language not supported
- src/Bicep.Core.Samples/Files/baselines/Resources_CRLF/main.diagnostics.bicep: Language not supported
- src/Bicep.Core.Samples/Files/user_submitted/101/azure-bastion/main.bicep: Language not supported
- src/Bicep.Core.Samples/Files/user_submitted/201/avd-backplane-with-network-and-storage-and-monitoring/avd-image-builder-module.bicep: Language not supported
- src/Bicep.Core.Samples/Files/user_submitted/201/vm-new-or-existing-conditions/main.bicep: Language not supported
- src/Bicep.Core.Samples/Files/user_submitted/201/vnet-with-subnet-and-user-defined-route/main.bicep: Language not supported
- src/Bicep.Core.Samples/Files/user_submitted/301/sql-database-with-management/modules/sql-database.bicep: Language not supported
Comments suppressed due to low confidence (2)
src/Bicep.Core.IntegrationTests/SecureOutputsTests.cs:218
- Removing the resourceId(...) call in favor of a symbolic name here may lead to a mismatch in resource reference resolution when the deployment engine expects a fully-qualified resource ID. Please confirm that the secure output resolution logic is compatible with this change.
result.Template.Should().HaveValueAtPath("$.resources['key'].properties.value", "[listOutputsWithSecureValues('secureOuputs', '2022-09-01').secureOutput]");
src/Bicep.Core.IntegrationTests/Scenarios/ResourceListFunctionTests.cs:41
- Changing the literal string to a call to resourceId(...) modifies the input for the listKeys function; please verify that this adjustment aligns with the intended reference resolution behavior and that it properly covers the deployment semantics for resource keys.
result.Template.Should().HaveValueAtPath("$.outputs['pkMethod'].value", "[listKeys(resourceId('Microsoft.Storage/storageAccounts', 'testacc'), '2019-06-01').keys[0].value]");
Test this change out locally with the following install scripts (Action run 14629761279) VSCode
Azure CLI
|
Dotnet Test Results 78 files - 39 78 suites - 39 34m 29s ⏱️ - 19m 3s Results for commit 6b034ef. ± Comparison against base commit 813df1e. This pull request removes 1795 and adds 618 tests. Note that renamed tests count towards both.
|
Resolves #3750
This PR updates
.?
is compiled when used on top-level properties of resources and modules. The compiler will use the condition of the resource or module as the guard clause in anif
expression, substitutingnull()
if the condition is not met. For example, in the following template:The vaule of the
prop
output will compile to an expression like[tryGet(if(parameters('condition'), reference('r'), null()), 'bar')]
.Type analysis to make sure a warning is emitted when a
.?
should be used but is not and to make sure chained accesses are properly typed was complected by the fact that references are only situationally nullable. For example, given the statement:then the expression
accounts[1].properties
(which compiles to[reference('accounts[1]').properties]
) will cause a deployment failure, whereasmap(accounts, a => a.properties)
(which compiles to[map(references('accounts'), lambda('a', lambdaVariables('a').properties))]
) will not because the output ofreferences()
only includes resources with atrue
or absent condition. ThedependsOn
property in ARM is also specifically allowed to refer to resources with afalse
condition, but it may be a problem if theparent
orscope
property in Bicep refers to a resource with afalse
condition.I opted to make property access on a base that is a resource nullable. E.g.,
r
in the first example above is not nullable, but it becomes nullable when used in the expressionr.properties
. Similarly for collections,accounts
in the second example is an array of nonnullable value, butaccounts[<index>]
becomes nullable when used in an expression likeaccounts[<index>].properties
. This seems error prone, however, so it's probably best to tag this for a team discussion.Microsoft Reviewers: Open in CodeFlow