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

Symbolic names: Generate dependsOn for "existing" resources #11478

Merged
merged 2 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix for issue 10343
  • Loading branch information
anthony-c-martin committed Aug 11, 2023
commit 629779ea1d91252d143ec2c498dab6f0f1496ceb
26 changes: 26 additions & 0 deletions src/Bicep.Core.IntegrationTests/ScenarioTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4987,4 +4987,30 @@ public void Test_Issue502()

result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
}

// https://github.com/Azure/bicep/issues/10343
[TestMethod]
public void Test_Issue10343()
{
var result = CompilationHelper.Compile(Services.WithFeatureOverrides(new(SymbolicNameCodegenEnabled: true)), ("main.bicep", @"
resource foo1 'Microsoft.Storage/storageAccounts@2022-09-01' = {
name: 'foo'
}

resource foo2 'Microsoft.Authorization/roleAssignments@2022-04-01' existing = {
scope: foo1
name: 'blah'
}

resource foo3 'Microsoft.Storage/storageAccounts@2022-09-01' = {
name: 'foo3'
properties: {
accessTier: foo2.id
}
}
"));

var evaluated = TemplateEvaluator.Evaluate(result.Template);
evaluated.Should().HaveValueAtPath("resources.foo3.dependsOn", new JArray("foo2"));
}
}
2 changes: 1 addition & 1 deletion src/Bicep.Core/Emit/EmitterContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public EmitterContext(SemanticModel semanticModel)
SemanticModel = semanticModel;
DataFlowAnalyzer = new(semanticModel);
VariablesToInline = InlineDependencyVisitor.GetVariablesToInline(semanticModel);
ResourceDependencies = ResourceDependencyVisitor.GetResourceDependencies(semanticModel);
ResourceDependencies = ResourceDependencyVisitor.GetResourceDependencies(semanticModel, new() { IncludeExisting = Settings.EnableSymbolicNames });
FunctionVariables = FunctionVariableGeneratorVisitor.GetFunctionVariables(semanticModel);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Bicep.Core/Emit/ResourceDependencyVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ int GetIndexOfAncestor(ImmutableArray<ResourceAncestorGraph.ResourceAncestor> an
{
for (int i = ancestors.Length - 1; i >= 0; i--)
{
if (!ancestors[i].Resource.IsExistingResource || (options?.IncludeExisting ?? false))
if (!ancestors[i].Resource.IsExistingResource || options?.IncludeExisting == true)
{
// we found the non-existing resource - we're done
return i;
Expand Down Expand Up @@ -177,7 +177,7 @@ public override void VisitVariableAccessSyntax(VariableAccessSyntax syntax)
return;

case ResourceSymbol resourceSymbol:
if (resourceSymbol.DeclaringResource.IsExistingResource())
if (resourceSymbol.DeclaringResource.IsExistingResource() && options?.IncludeExisting != true)
{
var existingDependencies = GetResourceDependencies(resourceSymbol);

Expand Down
7 changes: 4 additions & 3 deletions src/Bicep.Core/Intermediate/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,7 @@ private static SyntaxBase GetModuleNameSyntax(ModuleSymbol moduleSymbol)
return null;
}

private static bool ShouldGenerateDependsOn(ResourceDependency dependency)
private bool ShouldGenerateDependsOn(ResourceDependency dependency)
{
if (dependency.Kind == ResourceDependencyKind.Transitive)
{
Expand All @@ -1066,8 +1066,9 @@ private static bool ShouldGenerateDependsOn(ResourceDependency dependency)
}

return dependency.Resource switch
{ // We only want to add a 'dependsOn' for resources being deployed in this file.
ResourceSymbol resource => !resource.DeclaringResource.IsExistingResource(),
{
// 'existing' resources are only represented in the JSON if using symbolic names.
ResourceSymbol resource => Context.Settings.EnableSymbolicNames || !resource.DeclaringResource.IsExistingResource(),
ModuleSymbol => true,
_ => throw new InvalidOperationException($"Found dependency '{dependency.Resource.Name}' of unexpected type {dependency.GetType()}"),
};
Expand Down