Skip to content

Commit

Permalink
Use languageVersion 2.0 if types or functions are imported via wildca…
Browse files Browse the repository at this point in the history
…rd (Azure#12898)

Resolves Azure#12897 

Bicep should be targeting languageVersion 2.0 if any types or functions
are imported, but the current check will miss cases where those imports
are performed using a wildcard (`import * as foo...`) instead of by name
(`import {foo, bar} from ...`). The check looks at a model's symbol
table, and wildcard imports create a different kind of symbol.
###### Microsoft Reviewers: [Open in
CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/Azure/bicep/pull/12898)
  • Loading branch information
jeskew committed Jan 3, 2024
1 parent b51a3b1 commit 63fcc71
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Bicep.Core.IntegrationTests/CompileTimeImportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1934,4 +1934,22 @@ func getSubnetNumber(plan_name string) string => isFirstPlan(getPlanNumber(plan_
var evaluated = TemplateEvaluator.Evaluate(result.Template);
evaluated.Should().HaveValueAtPath("outputs.out.value", "ASP999");
}

// https://github.com/Azure/bicep/issues/12897
[TestMethod]
public void LanguageVersion_2_should_be_used_if_types_imported_via_wildcard()
{
var result = CompilationHelper.Compile(ServicesWithCompileTimeTypeImportsAndUserDefinedFunctions,
("main.bicep", """
import * as types from 'types.bicep'
"""),
("types.bicep", """
@export()
type str = string
"""));

result.Diagnostics.Should().BeEmpty();
result.Template.Should().NotBeNull();
result.Template.Should().HaveValueAtPath("languageVersion", "2.0");
}
}
4 changes: 4 additions & 0 deletions src/Bicep.Core/Emit/EmitterSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.
using System.Linq;
using Bicep.Core.Semantics;
using Bicep.Core.Semantics.Metadata;
using Bicep.Core.Syntax;
using Bicep.Core.Syntax.Visitors;
using Bicep.Core.Workspaces;
Expand All @@ -27,6 +28,9 @@ public EmitterSettings(SemanticModel model)
model.Root.ImportedTypes.Any() ||
// there are any functions imported (it's impossible to tell here if the functions use user-defined types for their parameter or output declarations)
model.Root.ImportedFunctions.Any() ||
// there are any wildcard imports that include user-defined types or functions
model.Root.WildcardImports.Any(w => w.SourceModel.Exports.Values.Any(
e => e.Kind == ExportMetadataKind.Type || e.Kind == ExportMetadataKind.Function)) ||
// any user-defined type declaration syntax is used (e.g., in a `param` or `output` statement)
SyntaxAggregator.Aggregate(model.SourceFile.ProgramSyntax,
seed: false,
Expand Down

0 comments on commit 63fcc71

Please sign in to comment.