Skip to content

Commit

Permalink
What If Short Circuiting Linter Rule (Default Off) (#14910)
Browse files Browse the repository at this point in the history
Add what if short circuiting warning when modules do not use deploy time
constants for resource Name, Scope, Condition, SubscriptionId, or
ResourceGroup
###### Microsoft Reviewers: [Open in
CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/Azure/bicep/pull/14910)

---------

Co-authored-by: Tate Smalligan <tasmalligan@microsoft.com>
  • Loading branch information
tsmallig33 and Tate Smalligan authored Sep 10, 2024
1 parent 1d71566 commit ead1da2
Show file tree
Hide file tree
Showing 28 changed files with 885 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/Bicep.Cli/Services/TemplateEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,21 +200,21 @@ private static TestEvaluation EvaluateTemplate(JToken? templateJtoken, JToken? p
var template = TemplateEngine.ParseTemplate(templateJtoken.ToString());
var parameters = ParseParametersFile(parametersJToken);

TemplateEngine.ValidateTemplate(template, TemplateWriter.NestedDeploymentResourceApiVersion, deploymentScope);
TemplateEngine.ValidateTemplate(template, EmitConstants.NestedDeploymentResourceApiVersion, deploymentScope);

TemplateEngine.ProcessTemplateLanguageExpressions(
managementGroupName: config.ManagementGroup,
subscriptionId: config.SubscriptionId,
resourceGroupName: config.ResourceGroup,
template: template,
apiVersion: TemplateWriter.NestedDeploymentResourceApiVersion,
apiVersion: EmitConstants.NestedDeploymentResourceApiVersion,
inputParameters: new(parameters),
metadata: metadata,
metricsRecorder: new TemplateMetricsRecorder());

ProcessTemplateLanguageExpressions(template, config, deploymentScope);

TemplateEngine.ValidateProcessedTemplate(template, TemplateWriter.NestedDeploymentResourceApiVersion, deploymentScope);
TemplateEngine.ValidateProcessedTemplate(template, EmitConstants.NestedDeploymentResourceApiVersion, deploymentScope);

var allAssertions = template.Asserts?.Select(p => new AssertionResult(p.Key, (bool)p.Value.Value)).ToImmutableArray() ?? [];
var failedAssertions = allAssertions.Where(a => !a.Result).Select(a => a).ToImmutableArray();
Expand Down
6 changes: 3 additions & 3 deletions src/Bicep.Core.IntegrationTests/TemplateEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,21 +230,21 @@ private static JToken EvaluateTemplate(JToken? templateJtoken, JToken? parameter
var template = TemplateEngine.ParseTemplate(templateJtoken.ToString());
var parameters = ParseParametersFile(parametersJToken);

TemplateEngine.ValidateTemplate(template, TemplateWriter.NestedDeploymentResourceApiVersion, deploymentScope);
TemplateEngine.ValidateTemplate(template, EmitConstants.NestedDeploymentResourceApiVersion, deploymentScope);

TemplateEngine.ProcessTemplateLanguageExpressions(
managementGroupName: config.ManagementGroup,
subscriptionId: config.SubscriptionId,
resourceGroupName: config.ResourceGroup,
template: template,
apiVersion: TemplateWriter.NestedDeploymentResourceApiVersion,
apiVersion: EmitConstants.NestedDeploymentResourceApiVersion,
inputParameters: new(parameters),
metadata: metadata,
metricsRecorder: new TemplateMetricsRecorder());

ProcessTemplateLanguageExpressions(template, config, deploymentScope);

TemplateEngine.ValidateProcessedTemplate(template, TemplateWriter.NestedDeploymentResourceApiVersion, deploymentScope);
TemplateEngine.ValidateProcessedTemplate(template, EmitConstants.NestedDeploymentResourceApiVersion, deploymentScope);

return template.ToJToken();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Bicep.Core.Samples/DataSetsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static string SaveFilesToTestDirectory(this DataSet dataSet, TestContext
await dataSet.PublishModulesToRegistryAsync(clientFactory);
var templateSpecRepositoryFactory = dataSet.CreateMockTemplateSpecRepositoryFactory(testContext);

var compiler = ServiceBuilder.Create(s => s.AddSingleton(templateSpecRepositoryFactory).AddSingleton(clientFactory).WithFeatureOverrides(features)).GetCompiler();
var compiler = ServiceBuilder.Create(s => s.AddSingleton(templateSpecRepositoryFactory).AddSingleton(clientFactory).WithFeatureOverrides(features).WithAnalyzersCodesToDisableConfiguration(BicepTestConstants.TestAnalyzersToSkip)).GetCompiler();

var fileUri = PathHelper.FilePathToFileUrl(Path.Combine(outputDirectory, DataSet.TestFileMain));
var compilation = await compiler.CreateCompilation(fileUri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,20 @@
"newText": "'./modulec.json'"
}
},
{
"label": "moduled.bicep",
"kind": "file",
"deprecated": false,
"preselect": false,
"sortText": "1_moduled.bicep",
"filterText": "'./moduled.bicep'",
"insertTextFormat": "plainText",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "'./moduled.bicep'"
}
},
{
"label": "moduled.json",
"kind": "file",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,20 @@
"newText": "'modulec.json'"
}
},
{
"label": "moduled.bicep",
"kind": "file",
"deprecated": false,
"preselect": false,
"sortText": "1_moduled.bicep",
"filterText": "'moduled.bicep'",
"insertTextFormat": "plainText",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "'moduled.bicep'"
}
},
{
"label": "moduled.json",
"kind": "file",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,20 @@
"newText": "'modulec.json'"
}
},
{
"label": "moduled.bicep",
"kind": "file",
"deprecated": false,
"preselect": false,
"sortText": "1_moduled.bicep",
"filterText": "'moduled.bicep'",
"insertTextFormat": "plainText",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "'moduled.bicep'"
}
},
{
"label": "moduled.json",
"kind": "file",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1769,6 +1769,34 @@
"newText": "modCycle"
}
},
{
"label": "modDOne",
"kind": "module",
"detail": "modDOne",
"deprecated": false,
"preselect": false,
"sortText": "2_modDOne",
"insertTextFormat": "plainText",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "modDOne"
}
},
{
"label": "modDTwo",
"kind": "module",
"detail": "modDTwo",
"deprecated": false,
"preselect": false,
"sortText": "2_modDTwo",
"insertTextFormat": "plainText",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "modDTwo"
}
},
{
"label": "modWithListKeysInCondition",
"kind": "module",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1769,6 +1769,34 @@
"newText": "modCycle"
}
},
{
"label": "modDOne",
"kind": "module",
"detail": "modDOne",
"deprecated": false,
"preselect": false,
"sortText": "2_modDOne",
"insertTextFormat": "plainText",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "modDOne"
}
},
{
"label": "modDTwo",
"kind": "module",
"detail": "modDTwo",
"deprecated": false,
"preselect": false,
"sortText": "2_modDTwo",
"insertTextFormat": "plainText",
"insertTextMode": "adjustIndentation",
"textEdit": {
"range": {},
"newText": "modDTwo"
}
},
{
"label": "modWithListKeysInCondition",
"kind": "module",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module nonExistentFileRef './nonExistent.bicep' = {
module nonExistentFileRef './nonExistent.bicep' = {

}

Expand All @@ -16,6 +16,21 @@ module moduleWithoutPath = {

}

module modDOne 'moduled.bicep' = {
name: 'modDOne'
params: {
input: 'NameOne'
}
}

module modDTwo 'moduled.bicep' = {
name: 'modDTwo'
params: {
input: modDOne.outputs.storageAccountName
}
}


// #completionTest(41) -> moduleBodyCompletions
module moduleWithPath './moduleb.bicep' =

Expand Down Expand Up @@ -566,4 +581,4 @@ module jsonModMissingParam 'moduled.json' = {
module assignToOutput 'empty.bicep' = {
name: 'assignToOutput'
outputs: {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ module moduleWithoutPath = {
}
//@[000:001) [BCP007 (Error)] This declaration type is not recognized. Specify a metadata, parameter, variable, resource, or output declaration. (bicep https://aka.ms/bicep/core-diagnostics#BCP007) |}|

module modDOne 'moduled.bicep' = {
name: 'modDOne'
params: {
input: 'NameOne'
}
}

module modDTwo 'moduled.bicep' = {
name: 'modDTwo'
params: {
input: modDOne.outputs.storageAccountName
//@[011:045) [what-if-short-circuiting (Warning)] Runtime value 'modDOne.outputs.storageAccountName' will reduce the precision of what-if analysis for module 'modDTwo' (bicep core linter https://aka.ms/bicep/linter/what-if-short-circuiting) |modDOne.outputs.storageAccountName|
}
}


// #completionTest(41) -> moduleBodyCompletions
module moduleWithPath './moduleb.bicep' =
//@[041:041) [BCP118 (Error)] Expected the "{" character, the "[" character, or the "if" keyword at this location. (bicep https://aka.ms/bicep/core-diagnostics#BCP118) ||
Expand Down Expand Up @@ -803,3 +819,4 @@ module assignToOutput 'empty.bicep' = {
outputs: {}
//@[002:009) [BCP073 (Error)] The property "outputs" is read-only. Expressions cannot be assigned to read-only properties. (bicep https://aka.ms/bicep/core-diagnostics#BCP073) |outputs|
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ module moduleWithoutPath = {

}

module modDOne 'moduled.bicep' = {
name: 'modDOne'
params: {
input: 'NameOne'
}
}

module modDTwo 'moduled.bicep' = {
name: 'modDTwo'
params: {
input: modDOne.outputs.storageAccountName
}
}

// #completionTest(41) -> moduleBodyCompletions
module moduleWithPath './moduleb.bicep' =

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ module moduleWithoutPath = {

}

module modDOne 'moduled.bicep' = {
//@[07:14) Module modDOne. Type: module. Declaration start char: 0, length: 91
name: 'modDOne'
params: {
input: 'NameOne'
}
}

module modDTwo 'moduled.bicep' = {
//@[07:14) Module modDTwo. Type: module. Declaration start char: 0, length: 116
name: 'modDTwo'
params: {
input: modDOne.outputs.storageAccountName
}
}


// #completionTest(41) -> moduleBodyCompletions
module moduleWithPath './moduleb.bicep' =
//@[07:21) Module moduleWithPath. Type: module. Declaration start char: 0, length: 41
Expand Down Expand Up @@ -745,3 +762,4 @@ module assignToOutput 'empty.bicep' = {
name: 'assignToOutput'
outputs: {}
}

Loading

0 comments on commit ead1da2

Please sign in to comment.