Skip to content

Conversation

@ArcturusZhang
Copy link
Member

Fixes #53862

In general, we are having this issue because when assigning values to ordinary properties and collection items, we are using different approach:
When assigning to ordinary properties, we always copies the value into the current BicepValue.
For instance, when we have a string property, we have this:

public partial class StorageAccount : ProvisionableResource
{
	private BicepValue<string>? _skuName;
	public BicepValue<string> SkuName
	{
		get { Initialize(); return _skuName!; }
		set { Initialize(); _skuName!.Assign(value); }
	}
}

in the setter, we call the Assign method which copies whatever we have in value (which is another BicepValue) into _skuName. In this process, the BicepValue here _skuName has its own Self to track its reference, and the value also has its own Self.
In collection case, it is different. When we do this:

var value = storageAccount.Name;
storageAccount.List.Add(value);

We only do this internally:

    public void Add(BicepValue<T> item)
    {
		// omit checks
        _values.Add(item);
        // update the _self pointing the new item
        SetSelfForItem(item, _values.Count - 1);
    }

this will add that BicepValue instance into the list, which might hold a Self of a property, and then we modified its self.
This causes a share of instances between this collection and a property in another instance.
This violates our best practice that we should never share instances. And therefore it is causing the issue.

To fix this, when modifying collections, we must first construct a new BicepValue<T> instance, and then assign the item value into it, and add our own instance into the list to keep the reference correct, and no longer pollute other properties' self reference.

@ArcturusZhang ArcturusZhang marked this pull request as ready for review November 14, 2025 08:49
Copilot AI review requested due to automatic review settings November 14, 2025 08:49
Copilot finished reviewing on behalf of ArcturusZhang November 14, 2025 08:51
Copy link
Contributor

Copilot AI left a 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 fixes a regression where assigning a BicepValue<T> from one resource's property to another resource's collection would incorrectly share the BicepValue instance, causing incorrect reference tracking in generated Bicep expressions.

Key Changes:

  • Modified collection operations (Add, Insert, indexer set) to create new BicepValue<T> instances and use Assign() to copy values instead of sharing instances
  • Changed BicepListValueReference.Index from read-only to mutable to support efficient index updates
  • Reordered precedence in BicepValue.Compile() to check _self before _source
  • Added comprehensive test coverage (13 new test methods) to validate the fix
  • Updated project references in 27 test projects to ensure proper dependencies

Reviewed Changes

Copilot reviewed 36 out of 36 changed files in this pull request and generated no comments.

Show a summary per file
File Description
BicepValue.cs Reordered Compile() to prioritize self-reference over source reference
BicepValueReference.cs Made Index property settable for efficient list index updates
BicepListOfT.cs Modified Add, Insert, indexer, and RemoveAt to create new instances and copy values
BicepDictionaryOfT.cs Modified Add and indexer to create new instances and copy values
BicepValueToExpressionTests.cs Added 13 comprehensive test methods covering property and collection reference scenarios
BasicContainerRegistryTests.cs Removed Ignore attribute and added Name assignment that previously failed
CHANGELOG.md Added bug fix entry with issue reference
Multiple .csproj files Added Azure.Provisioning project references to 27 test projects

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.

[Provisioning/Bug] Using BicepValue<T> as dictionary value causes weird exception

1 participant