Skip to content

Commit 35fdba4

Browse files
authored
Enable speculative reference evaluation in snapshot helper (#17512)
## Description Reference evaluation in nested deployment expansion requires an explicit opt-in. This isn't currently being supplied in the snapshot helper, which causes snapshots to have more unevaluated expressions than you would see from the what-if API. ## Example Usage No change to usage. ## Checklist - [X] I have read and adhere to the [contribution guide](https://github.com/Azure/bicep/blob/main/CONTRIBUTING.md).
1 parent 126d295 commit 35fdba4

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/Bicep.Cli.IntegrationTests/SnapshotCommandTests.cs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,74 @@ param condition bool
226226
result.Stderr.Should().NotContain("Unhandled exception");
227227
}
228228

229+
[TestMethod]
230+
public async Task Snapshot_speculatively_evaluates_references()
231+
{
232+
var outputPath = FileHelper.GetUniqueTestOutputPath(TestContext);
233+
var bicepparamPath = FileHelper.SaveResultFile(
234+
TestContext,
235+
"main.bicepparam",
236+
"""
237+
using './main.bicep'
238+
""",
239+
testOutputPath: outputPath);
240+
241+
FileHelper.SaveResultFile(
242+
TestContext,
243+
"main.bicep",
244+
"""
245+
module mod 'mod.bicep' = {}
246+
247+
module mod2 'mod2.bicep' = {
248+
params: {
249+
vnetName: mod.outputs.static
250+
}
251+
}
252+
""",
253+
testOutputPath: outputPath);
254+
255+
FileHelper.SaveResultFile(
256+
TestContext,
257+
"mod.bicep",
258+
"""output static string = 'foo'""",
259+
testOutputPath: outputPath);
260+
261+
FileHelper.SaveResultFile(
262+
TestContext,
263+
"mod2.bicep",
264+
"""
265+
param vnetName string
266+
267+
resource vnet 'Microsoft.Network/virtualNetworks@2024-07-01' = {
268+
name: vnetName
269+
}
270+
""",
271+
testOutputPath: outputPath);
272+
273+
var outputFilePath = FileHelper.GetResultFilePath(TestContext, "main.snapshot.json", testOutputPath: outputPath);
274+
275+
var subscriptionId = new Guid().ToString();
276+
277+
var result = await Bicep("snapshot", bicepparamPath, "--mode", "overwrite", "--subscription-id", subscriptionId, "--resource-group", "myRg", "--deployment-name", "deployment");
278+
279+
result.Should().Succeed();
280+
281+
var snapshotContents = File.ReadAllText(outputFilePath).FromJson<JToken>();
282+
snapshotContents.Should().DeepEqual(JObject.Parse("""
283+
{
284+
"predictedResources": [
285+
{
286+
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myRg/providers/Microsoft.Network/virtualNetworks/foo",
287+
"type": "Microsoft.Network/virtualNetworks",
288+
"name": "foo",
289+
"apiVersion": "2024-07-01"
290+
}
291+
],
292+
"diagnostics": []
293+
}
294+
"""));
295+
}
296+
229297
private static string ReplaceColorCodes(string input)
230298
{
231299
var namesByColor = new Dictionary<Color, string>

src/Bicep.Cli/Helpers/Snapshot/SnapshotHelper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public static async Task<Snapshot> GetSnapshot(
7272
template,
7373
parameters: ResolveParameters(parameters, externalInputs),
7474
rootDeploymentMetadata: GetDeploymentMetadata(tenantId, subscriptionId, resourceGroup, deploymentName, location, scope, template),
75+
referenceFunctionPreflightEnabled: true,
7576
cancellationToken: cancellationToken);
7677

7778
return new(

0 commit comments

Comments
 (0)