Skip to content

Commit

Permalink
Restore behavior to avoid inferring dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-c-martin committed Sep 20, 2024
1 parent fbe41a7 commit cc02f3d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public void NameofFunction_OnResourceProperty_ReturnsResourcePropertyName(string
}

[TestMethod]
public void UsingNameofFunction_ShouldGenerateDependsOnEntries()
public void UsingNameofFunction_ShouldNotGenerateDependsOnEntries()
{
var result = CompilationHelper.Compile("""
resource myStorage 'Microsoft.Storage/storageAccounts@2019-06-01' = {
Expand All @@ -257,7 +257,7 @@ public void UsingNameofFunction_ShouldGenerateDependsOnEntries()
{
result.Should().NotHaveAnyCompilationBlockingDiagnostics();
result.Template.Should().HaveValueAtPath("$.resources[?(@.type == 'Microsoft.Sql/servers')].name", "myStorage", "Nameof function should emit static string during compilation");
result.Template.Should().HaveValueAtPath("$.resources[?(@.type == 'Microsoft.Sql/servers')].dependsOn[0]", "[resourceId('Microsoft.Storage/storageAccounts', 'storage123')]");
result.Template.Should().NotHaveValueAtPath("$.resources[?(@.type == 'Microsoft.Sql/servers')].dependsOn", "Depends on should not be generated for nameof function usage");
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/Bicep.Core/Emit/ResourceDependencyVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Bicep.Core.Semantics;
using Bicep.Core.Semantics.Metadata;
using Bicep.Core.Syntax;
using Bicep.Core.TypeSystem;

namespace Bicep.Core.Emit
{
Expand Down Expand Up @@ -282,6 +283,15 @@ public override void VisitObjectPropertySyntax(ObjectPropertySyntax propertySynt
base.VisitObjectPropertySyntax(propertySyntax);
}

public override void VisitFunctionCallSyntax(FunctionCallSyntax syntax)
{
var functionSymbol = model.GetSymbolInfo(syntax) as FunctionSymbol;
if (ShouldVisitFunctionArguments(functionSymbol))
{
base.VisitFunctionCallSyntax(syntax);
}
}

private bool IsTopLevelPropertyOfCurrentDeclaration(ObjectPropertySyntax propertySyntax)
{
SyntaxBase? declaringSyntax = this.currentDeclaration switch
Expand All @@ -294,5 +304,8 @@ private bool IsTopLevelPropertyOfCurrentDeclaration(ObjectPropertySyntax propert

return currentDeclarationProperties?.Contains(propertySyntax) ?? false;
}

private static bool ShouldVisitFunctionArguments(FunctionSymbol? functionSymbol)
=> functionSymbol is null || !functionSymbol.FunctionFlags.HasFlag(FunctionFlags.IsArgumentValueIndependent);
}
}

0 comments on commit cc02f3d

Please sign in to comment.