Skip to content

Commit 78a65ae

Browse files
authored
Fixed dependency generation with existing resources (#7433)
* workaround for File.Exists deadlock on win11 * fix
1 parent 04263c4 commit 78a65ae

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-3
lines changed

src/Bicep.Core.IntegrationTests/ScenarioTests.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3684,5 +3684,69 @@ public void Test_Issue2017()
36843684

36853685
result.Diagnostics.Should().OnlyContain(x => x.Styling == DiagnosticStyling.ShowCodeDeprecated);
36863686
}
3687+
3688+
/// <summary>
3689+
/// https://github.com/Azure/bicep/issues/6477
3690+
/// </summary>
3691+
[TestMethod]
3692+
public void Test_Issue6477()
3693+
{
3694+
var result = CompilationHelper.Compile(@"
3695+
param storageAccountName string
3696+
3697+
@allowed([
3698+
'Standard_GRS'
3699+
'Standard_ZRS'
3700+
])
3701+
@description('Storage account SKU. Standard_ZRS should be used if region supports, else Standard_GRS.')
3702+
param storageAccountSku string
3703+
3704+
resource storageAccountName_resource 'Microsoft.Storage/storageAccounts@2021-04-01' = {
3705+
name: storageAccountName
3706+
#disable-next-line no-loc-expr-outside-params
3707+
location: resourceGroup().location
3708+
sku: {
3709+
name: storageAccountSku
3710+
#disable-next-line BCP073
3711+
tier: 'Standard'
3712+
}
3713+
kind: 'StorageV2'
3714+
properties: {
3715+
allowBlobPublicAccess: true // Ibiza requires anonymous access to the container
3716+
minimumTlsVersion: 'TLS1_2'
3717+
supportsHttpsTrafficOnly: true
3718+
encryption: {
3719+
services: {
3720+
file: {
3721+
enabled: true
3722+
}
3723+
blob: {
3724+
enabled: true
3725+
}
3726+
}
3727+
keySource: 'Microsoft.Storage'
3728+
}
3729+
accessTier: 'Hot'
3730+
}
3731+
3732+
resource blobs 'blobServices' existing = {
3733+
name: 'default'
3734+
3735+
resource containers 'containers' = {
3736+
name: 'extension'
3737+
properties: {
3738+
publicAccess: 'Container'
3739+
}
3740+
}
3741+
}
3742+
}
3743+
");
3744+
result.ExcludingLinterDiagnostics().Should().NotHaveAnyDiagnostics();
3745+
result.Template.Should().HaveValueAtPath("$.resources[0].type", "Microsoft.Storage/storageAccounts/blobServices/containers");
3746+
result.Template.Should().HaveValueAtPath("$.resources[0].dependsOn", new JArray("[resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName'))]"));
3747+
3748+
result.Template.Should().HaveValueAtPath("$.resources[1].type", "Microsoft.Storage/storageAccounts");
3749+
result.Template.Should().NotHaveValueAtPath("$.resources[1].dependsOn");
3750+
}
36873751
}
36883752
}

src/Bicep.Core/Emit/ResourceDependencyVisitor.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,21 @@ private ResourceDependencyVisitor(SemanticModel model, Options? options)
6565

6666
public override void VisitResourceDeclarationSyntax(ResourceDeclarationSyntax syntax)
6767
{
68+
static int GetIndexOfLastNonExistingAncestor(ImmutableArray<ResourceAncestorGraph.ResourceAncestor> ancestors)
69+
{
70+
for (int i = ancestors.Length - 1; i >= 0; i--)
71+
{
72+
if (!ancestors[i].Resource.IsExistingResource)
73+
{
74+
// we found the non-existing resource - we're done
75+
return i;
76+
}
77+
}
78+
79+
// no non-existing resources are found in the ancestors list
80+
return -1;
81+
}
82+
6883
if (model.ResourceMetadata.TryLookup(syntax) is not DeclaredResourceMetadata resource)
6984
{
7085
// When invoked by BicepDeploymentGraphHandler, it's possible that the declaration is unbound.
@@ -73,13 +88,13 @@ public override void VisitResourceDeclarationSyntax(ResourceDeclarationSyntax sy
7388

7489
// Resource ancestors are always dependencies.
7590
var ancestors = this.model.ResourceAncestors.GetAncestors(resource);
76-
var lastAncestorIndex = ancestors.Length - 1;
91+
var lastNonExistingAncestorIndex = GetIndexOfLastNonExistingAncestor(ancestors);
7792

7893
// save previous declaration as we may call this recursively
7994
var prevDeclaration = this.currentDeclaration;
8095

8196
this.currentDeclaration = resource.Symbol;
82-
this.resourceDependencies[resource.Symbol] = new HashSet<ResourceDependency>(ancestors.Select((a, i) => new ResourceDependency(a.Resource.Symbol, a.IndexExpression, i == lastAncestorIndex ? ResourceDependencyKind.Primary : ResourceDependencyKind.Transitive)));
97+
this.resourceDependencies[resource.Symbol] = new HashSet<ResourceDependency>(ancestors.Select((a, i) => new ResourceDependency(a.Resource.Symbol, a.IndexExpression, i == lastNonExistingAncestorIndex ? ResourceDependencyKind.Primary : ResourceDependencyKind.Transitive)));
8398
base.VisitResourceDeclarationSyntax(syntax);
8499

85100
// restore previous declaration

src/Bicep.LangServer.IntegrationTests/Helpers/ServerRequestHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public ServerRequestHelper(TestContext testContext, MultiFileLanguageServerHelpe
116116

117117
public async Task<FileRequestHelper> OpenFile(string text)
118118
=> await OpenFile(
119-
new Uri($"file://{Guid.NewGuid():D}/{testContext.TestName}/main.bicep"),
119+
new Uri($"file:///{Guid.NewGuid():D}/{testContext.TestName}/main.bicep"),
120120
text);
121121

122122
public async Task<FileRequestHelper> OpenFile(Uri fileUri, string text)

0 commit comments

Comments
 (0)