Skip to content

Commit

Permalink
Set assigning an expression to a read-only property diagnostic to alw…
Browse files Browse the repository at this point in the history
…ays be a warning
  • Loading branch information
miqm committed Dec 23, 2021
1 parent 90374c2 commit 2cefbd8
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 11 deletions.
37 changes: 37 additions & 0 deletions src/Bicep.Core.IntegrationTests/ScenarioTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2835,5 +2835,42 @@ public void Test_Issue5099()
result.Template!.Should().HaveValueAtPath("$.resources[?(@.name == '[format(\\'foo{0}\\', parameters(\\'rgNames\\')[copyIndex()])]')].metadata.description", "module loop");
result.Template!.Should().HaveValueAtPath("$.outputs.productGroupsResourceIds.metadata.description", "The Resources Ids of the API management service product groups");
}

/// <summary>
/// https://github.com/Azure/bicep/issues/5456
/// </summary>
[TestMethod]
public void Test_Issue5456()
{
var typeReference = ResourceTypeReference.Parse("My.Rp/myResource@2020-01-01");
var typeLoader = TestTypeHelper.CreateAzResourceTypeLoaderWithTypes(new[] {
new ResourceTypeComponents(typeReference, ResourceScope.ResourceGroup, new ObjectType(typeReference.FormatName(), TypeSymbolValidationFlags.Default, new [] {
new TypeProperty("name", LanguageConstants.String, TypePropertyFlags.DeployTimeConstant, "name property"),
new TypeProperty("tags", LanguageConstants.Array, TypePropertyFlags.ReadOnly, "tags property"),
new TypeProperty("properties", new ObjectType("properties",TypeSymbolValidationFlags.Default, new []
{
new TypeProperty("prop1", LanguageConstants.String, TypePropertyFlags.ReadOnly, "prop1")
},null), TypePropertyFlags.ReadOnly, "properties property"),
}, null))
});

// explicitly pass a valid scope
var result = CompilationHelper.Compile(typeLoader, ("main.bicep", @"
resource resourceA 'My.Rp/myResource@2020-01-01' = {
name: 'resourceA'
tags: [
'tag1'
]
properties: {
prop1: 'value'
}
}
"));
result.Should().GenerateATemplate().And.HaveDiagnostics(new []
{
("BCP073", DiagnosticLevel.Warning, "The property \"tags\" is read-only. Expressions cannot be assigned to read-only properties. If this is an inaccuracy in the documentation, please report it to the Bicep Team."),
("BCP073", DiagnosticLevel.Warning, "The property \"properties\" is read-only. Expressions cannot be assigned to read-only properties. If this is an inaccuracy in the documentation, please report it to the Bicep Team.")
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,11 @@ resource baz 'Microsoft.Foo/foos@2020-02-02-alpha' = {
name: 'test'
//@[8:14) [BCP121 (Error)] Resources: "baz", "badDepends", "badDepends2", "badDepends3", "badDepends4", "badDepends5", "badInterp" are defined with this same name in a file. Rename them or split into different modules. (CodeDescription: none) |'test'|
id: 2
//@[2:4) [BCP073 (Error)] The property "id" is read-only. Expressions cannot be assigned to read-only properties. (CodeDescription: none) |id|
//@[2:4) [BCP073 (Warning)] The property "id" is read-only. Expressions cannot be assigned to read-only properties. If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |id|
type: 'hello'
//@[2:6) [BCP073 (Error)] The property "type" is read-only. Expressions cannot be assigned to read-only properties. (CodeDescription: none) |type|
//@[2:6) [BCP073 (Warning)] The property "type" is read-only. Expressions cannot be assigned to read-only properties. If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |type|
apiVersion: true
//@[2:12) [BCP073 (Error)] The property "apiVersion" is read-only. Expressions cannot be assigned to read-only properties. (CodeDescription: none) |apiVersion|
//@[2:12) [BCP073 (Warning)] The property "apiVersion" is read-only. Expressions cannot be assigned to read-only properties. If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |apiVersion|
}

resource badDepends 'Microsoft.Foo/foos@2020-02-02-alpha' = {
Expand Down
6 changes: 3 additions & 3 deletions src/Bicep.Core/Diagnostics/DiagnosticBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -496,11 +496,11 @@ public ErrorDiagnostic ArgumentCountMismatch(int argumentCount, int minimumArgum
"BCP072",
"This symbol cannot be referenced here. Only other parameters can be referenced in parameter default values.");

public Diagnostic CannotAssignToReadOnlyProperty(bool warnInsteadOfError, string property) => new(
public Diagnostic CannotAssignToReadOnlyProperty(string property) => new(
TextSpan,
warnInsteadOfError ? DiagnosticLevel.Warning : DiagnosticLevel.Error,
DiagnosticLevel.Warning,
"BCP073",
$"The property \"{property}\" is read-only. Expressions cannot be assigned to read-only properties.");
$"The property \"{property}\" is read-only. Expressions cannot be assigned to read-only properties.{TypeInaccuracyClause}", TypeInaccuracyLink);

public ErrorDiagnostic ArraysRequireIntegerIndex(TypeSymbol wrongType) => new(
TextSpan,
Expand Down
2 changes: 1 addition & 1 deletion src/Bicep.Core/TypeSystem/TypeValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ static TypeSymbol RemoveImplicitNull(TypeSymbol type, bool typeWasPreserved)
}
else
{
diagnosticWriter.Write(config.OriginSyntax ?? declaredPropertySyntax.Key, x => x.CannotAssignToReadOnlyProperty(ShouldWarn(targetType), declaredProperty.Name));
diagnosticWriter.Write(config.OriginSyntax ?? declaredPropertySyntax.Key, x => x.CannotAssignToReadOnlyProperty(declaredProperty.Name));
}

narrowedProperties.Add(new TypeProperty(declaredProperty.Name, declaredProperty.TypeReference.Type, declaredProperty.Flags));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ resource storageAccounts_ll21w7onmmpv65q24_name_resource 'Microsoft.Storage/stor
sku: {
name: 'Standard_LRS'
tier: 'Standard'
//@[4:8) [BCP073 (Warning)] The property "tier" is read-only. Expressions cannot be assigned to read-only properties. (CodeDescription: none) |tier|
//@[4:8) [BCP073 (Warning)] The property "tier" is read-only. Expressions cannot be assigned to read-only properties. If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |tier|
}
kind: 'StorageV2'
properties: {
Expand Down Expand Up @@ -41,7 +41,7 @@ resource storageAccounts_ll22w7onmmpv65q24_name_resource 'Microsoft.Storage/stor
sku: {
name: 'Standard_LRS'
tier: 'Standard'
//@[4:8) [BCP073 (Warning)] The property "tier" is read-only. Expressions cannot be assigned to read-only properties. (CodeDescription: none) |tier|
//@[4:8) [BCP073 (Warning)] The property "tier" is read-only. Expressions cannot be assigned to read-only properties. If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |tier|
}
kind: 'StorageV2'
properties: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ resource dataFactoryName_Backup_Trigger 'Microsoft.DataFactory/factories/trigger
properties: {
annotations: []
runtimeState: 'Started'
//@[4:16) [BCP073 (Warning)] The property "runtimeState" is read-only. Expressions cannot be assigned to read-only properties. (CodeDescription: none) |runtimeState|
//@[4:16) [BCP073 (Warning)] The property "runtimeState" is read-only. Expressions cannot be assigned to read-only properties. If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |runtimeState|
pipelines: [
//@[4:13) [BCP037 (Warning)] The property "pipelines" is not allowed on objects of type "ScheduleTrigger". Permissible properties include "description". If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |pipelines|
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ resource roleDefinitionName 'Microsoft.Authorization/roleAssignments@2020-04-01-
roleDefinitionId: roleDefinitionId
principalId: identityName.properties.principalId
scope: resourceGroup().id
//@[4:9) [BCP073 (Warning)] The property "scope" is read-only. Expressions cannot be assigned to read-only properties. (CodeDescription: none) |scope|
//@[4:9) [BCP073 (Warning)] The property "scope" is read-only. Expressions cannot be assigned to read-only properties. If this is an inaccuracy in the documentation, please report it to the Bicep Team. (CodeDescription: bicep(https://aka.ms/bicep-type-issues)) |scope|
principalType: 'ServicePrincipal'
}
}
Expand Down

0 comments on commit 2cefbd8

Please sign in to comment.