Skip to content

Commit 57a44c0

Browse files
Fix deploy pane when deploying .bicep files directly (#14587)
As part of the local deployment implementation, I apparently completely removed the ability for the deployment pane to deploy a non-local `.bicep` file 🤦. This PR fixes that. The changes in this PR: * Fixes deployment through the deploy pane if the user is referencing a `.bicep` file directly. * As part of testing this, I noticed we're actually missing validation for a `using` statement referencing a broken file. I've updated this to bring it in line with behavior for a `module` statement referencing a broken file. * Fixes the issue where a cryptic error appears in local-deploy mode if the `.bicep` file is invalid. * Fixes an issue where we're not logging the artifact reference properly in error messages (missing a `.ToString()` implementation). ###### Microsoft Reviewers: [Open in CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/Azure/bicep/pull/14587)
1 parent f1c7212 commit 57a44c0

File tree

10 files changed

+97
-44
lines changed

10 files changed

+97
-44
lines changed

src/Bicep.Core.IntegrationTests/CompileTimeImportTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,6 +2007,7 @@ INVALID FILE
20072007
{
20082008
("BCP104", DiagnosticLevel.Error, "The referenced module has errors."),
20092009
("BCP104", DiagnosticLevel.Error, "The referenced module has errors."),
2010+
("BCP104", DiagnosticLevel.Error, "The referenced module has errors."),
20102011
});
20112012
}
20122013

src/Bicep.Core.IntegrationTests/EvaluationTests.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -918,9 +918,14 @@ param bar string
918918
}
919919
";
920920

921-
var (parameters, diag, comp) = CompilationHelper.CompileParams(("parameters.bicepparam", bicepparamText), ("main.bicep", bicepTemplateText));
922-
923-
var result = CompilationHelper.Compile(("main.bicep", bicepTemplateText), ("module.bicep", bicepModuleText));
921+
var (parameters, diag, comp) = CompilationHelper.CompileParams(
922+
("parameters.bicepparam", bicepparamText),
923+
("main.bicep", bicepTemplateText),
924+
("module.bicep", bicepModuleText));
925+
926+
var result = CompilationHelper.Compile(
927+
("main.bicep", bicepTemplateText),
928+
("module.bicep", bicepModuleText));
924929

925930
var evaluated = TemplateEvaluator.Evaluate(result.Template, parameters, config => config with
926931
{

src/Bicep.Core.IntegrationTests/ParameterFileTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,20 @@ param optionalBecauseNullable string?
222222

223223
result.Should().NotHaveAnyDiagnostics();
224224
}
225+
226+
[TestMethod]
227+
public void Error_is_displayed_for_file_reference_with_errors()
228+
{
229+
var result = CompilationHelper.CompileParams(
230+
("parameters.bicepparam", """
231+
using 'main.bicep'
232+
"""), ("main.bicep", """
233+
invalid file
234+
"""));
235+
236+
result.Should().HaveDiagnostics(new[]
237+
{
238+
("BCP104", DiagnosticLevel.Error, "The referenced module has errors."),
239+
});
240+
}
225241
}

src/Bicep.Core.Samples/Files/baselines_bicepparam/Invalid_Parameters/parameters.diagnostics.bicepparam

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using './main.bicep'
22
//@[06:20) [BCP258 (Error)] The following parameters are declared in the Bicep file but are missing an assignment in the params file: "additionalMetadata", "decoratedString", "description", "description2", "emptyMetadata", "myBool", "myInt", "myString", "password", "secretObject", "someArray", "someParameter", "storageName", "storageSku", "stringLiteral". (CodeDescription: none) |'./main.bicep'|
3+
//@[06:20) [BCP104 (Error)] The referenced module has errors. (CodeDescription: none) |'./main.bicep'|
34

45
param para1 = 'value
56
//@[00:20) [BCP259 (Error)] The parameter "para1" is assigned in the params file without being declared in the Bicep file. (CodeDescription: none) |param para1 = 'value|

src/Bicep.Core/Registry/ArtifactReference.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,7 @@ protected ArtifactReference(string scheme, Uri parentModuleUri)
3535
/// Gets a value indicating whether this reference points to an external artifact.
3636
/// </summary>
3737
public abstract bool IsExternal { get; }
38+
39+
public override string ToString() => FullyQualifiedReference;
3840
}
3941
}

src/Bicep.Core/Semantics/SemanticModel.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,28 @@ private IEnumerable<IDiagnostic> GetAdditionalParamsSemanticDiagnostics()
503503
// get diagnostics relating to missing parameter assignments or declarations
504504
GatherParameterMismatchDiagnostics(semanticModel)
505505
// get diagnostics relating to type mismatch of params between Bicep and params files
506-
.Concat(GatherTypeMismatchDiagnostics());
506+
.Concat(GatherTypeMismatchDiagnostics())
507+
// get diagnostics on whether the module referenced in the using statement is valid
508+
.Concat(GatherUsingModelInvalidDiagnostics(semanticModel));
509+
}
510+
511+
private IEnumerable<IDiagnostic> GatherUsingModelInvalidDiagnostics(ISemanticModel usingModel)
512+
{
513+
// emit diagnostic only if there is a using statement
514+
var usingSyntax = this.Root.UsingDeclarationSyntax;
515+
516+
if (usingSyntax is null ||
517+
usingSyntax.Path is NoneLiteralSyntax)
518+
{
519+
yield break;
520+
}
521+
522+
if (usingModel.HasErrors())
523+
{
524+
yield return usingModel is ArmTemplateSemanticModel
525+
? DiagnosticBuilder.ForPosition(usingSyntax.Path).ReferencedArmTemplateHasErrors()
526+
: DiagnosticBuilder.ForPosition(usingSyntax.Path).ReferencedModuleHasErrors();
527+
}
507528
}
508529

509530
private IEnumerable<IDiagnostic> GatherParameterMismatchDiagnostics(ISemanticModel usingModel)

src/Bicep.LangServer/Handlers/GetDeploymentDataHandler.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,26 @@ public async Task<GetDeploymentDataResponse> Handle(GetDeploymentDataRequest req
4949
if (result.Parameters is null ||
5050
result.Template?.Template is null)
5151
{
52-
return new(ErrorMessage: $"Bicep compilation failed. The Bicep parameters file contains errors.", LocalDeployEnabled: localDeployEnabled);
52+
return new(ErrorMessage: $"Compilation failed. The Bicep parameters file contains errors.", LocalDeployEnabled: localDeployEnabled);
5353
}
5454

5555
paramsFile = result.Parameters;
5656
templateFile = result.Template.Template;
5757

5858
if (!semanticModel.Root.TryGetBicepFileSemanticModelViaUsing().IsSuccess(out var usingModel))
5959
{
60-
return new(ErrorMessage: $"Bicep compilation failed. The Bicep parameters file contains errors.", LocalDeployEnabled: localDeployEnabled);
60+
return new(ErrorMessage: $"Compilation failed. Failed to find a file referenced via 'using'.", LocalDeployEnabled: localDeployEnabled);
6161
}
6262
}
63-
else
63+
else if (semanticModel.Root.FileKind == BicepSourceFileKind.BicepFile)
6464
{
65-
return new(ErrorMessage: $"Bicep compilation failed. The Bicep file contains errors.", LocalDeployEnabled: localDeployEnabled);
65+
var result = context.Compilation.Emitter.Template();
66+
templateFile = result.Template;
67+
68+
if (result.Template is null)
69+
{
70+
return new(ErrorMessage: $"Compilation failed. The Bicep file contains errors.", LocalDeployEnabled: localDeployEnabled);
71+
}
6672
}
6773

6874
return new(TemplateJson: templateFile, ParametersJson: paramsFile, LocalDeployEnabled: localDeployEnabled);

src/Bicep.MSBuild.E2eTests/src/bicepparam.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@ describe("msbuild", () => {
5353

5454
asserts.expectLinesInLog(result.stdout, [
5555
"1 Warning(s)",
56-
"3 Error(s)",
56+
"4 Error(s)",
5757
'main.bicepparam(1,7): error BCP258: The following parameters are declared in the Bicep file but are missing an assignment in the params file: "extraneous"',
58+
"main.bicepparam(1,7): error BCP104: The referenced module has errors.",
5859
'main.bicepparam(3,1): error BCP259: The parameter "missing" is assigned in the params file without being declared in the Bicep file.',
5960
'main.bicep(1,7): warning no-unused-params: Parameter "extraneous" is declared but never used. [https://aka.ms/bicep/linter/no-unused-params]',
6061
'main.bicep(3,27): error BCP033: Expected a value of type "object" but the provided value is of type "\'\'"',

src/vscode-bicep/src/panes/deploy/app/components/App.tsx

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,9 @@ export const App: FC = () => {
6464
);
6565
}
6666

67-
if (messages.messageState.localDeployEnabled && !messages.paramsMetadata.sourceFilePath?.endsWith('.bicepparam')) {
68-
return (
69-
<div className="alert-error">
70-
Local Deployment is only currently supported for .bicepparam files. Relaunch this pane for a .bicepparam file.
71-
</div>
72-
);
73-
}
67+
const showLocalDeployControls = messages.messageState.localDeployEnabled &&
68+
// if there's an error, this'll cause sourceFilePath to be empty - we still want to show the controls to display the error
69+
(errorMessage || messages.paramsMetadata.sourceFilePath?.endsWith('.bicepparam'));
7470

7571
return (
7672
<main id="webview-body">
@@ -123,30 +119,36 @@ export const App: FC = () => {
123119
Local Deployment is an experimental feature.
124120
</div>
125121
</FormSection>
126-
127-
<ParametersInputView
128-
parameters={messages.paramsMetadata}
129-
template={messages.templateMetadata}
130-
disabled={localDeployRunning}
131-
onValueChange={setParamValue}
132-
onEnableEditing={handleEnableParamEditing}
133-
onPickParametersFile={messages.pickParamsFile} />
134-
135-
<FormSection title="Actions">
136-
{errorMessage && <div className="alert-error">
137-
<span className="codicon codicon-error" />
138-
{errorMessage}
139-
</div>}
140-
<div className="controls">
141-
<VSCodeButton onClick={handleLocalDeployClick} disabled={localDeployRunning}>Deploy</VSCodeButton>
122+
{showLocalDeployControls && <>
123+
<ParametersInputView
124+
parameters={messages.paramsMetadata}
125+
template={messages.templateMetadata}
126+
disabled={localDeployRunning}
127+
onValueChange={setParamValue}
128+
onEnableEditing={handleEnableParamEditing}
129+
onPickParametersFile={messages.pickParamsFile} />
130+
131+
<FormSection title="Actions">
132+
{errorMessage && <div className="alert-error">
133+
<span className="codicon codicon-error" />
134+
{errorMessage}
135+
</div>}
136+
<div className="controls">
137+
<VSCodeButton onClick={handleLocalDeployClick} disabled={localDeployRunning}>Deploy</VSCodeButton>
138+
</div>
139+
{localDeployRunning && <VSCodeProgressRing></VSCodeProgressRing>}
140+
</FormSection>
141+
142+
{!localDeployRunning && messages.localDeployResult && <>
143+
<LocalDeployResult result={messages.localDeployResult} />
144+
<LocalDeployOperations result={messages.localDeployResult} />
145+
<LocalDeployOutputs result={messages.localDeployResult} />
146+
</>}
147+
</>}
148+
{!showLocalDeployControls && <>
149+
<div className="alert-error">
150+
Local Deployment is only currently supported for .bicepparam files. Relaunch this pane for a .bicepparam file.
142151
</div>
143-
{localDeployRunning && <VSCodeProgressRing></VSCodeProgressRing>}
144-
</FormSection>
145-
146-
{!localDeployRunning && messages.localDeployResult && <>
147-
<LocalDeployResult result={messages.localDeployResult} />
148-
<LocalDeployOperations result={messages.localDeployResult} />
149-
<LocalDeployOutputs result={messages.localDeployResult} />
150152
</>}
151153
</>}
152154
</main>

src/vscode-bicep/src/panes/deploy/app/components/hooks/useMessageHandler.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,10 @@ export function useMessageHandler(props: UseMessageHandlerProps) {
6464
initialized: true,
6565
localDeployEnabled: message.localDeployEnabled,
6666
});
67-
if (!message.templateJson) {
67+
68+
if (message.errorMessage || !message.templateJson) {
6869
setTemplateMetadata(undefined);
69-
setErrorMessage(
70-
message.errorMessage ??
71-
"An error occurred building the deployment object.",
72-
);
70+
setErrorMessage(message.errorMessage ?? "An error occurred compiling the Bicep file.");
7371
return;
7472
}
7573

0 commit comments

Comments
 (0)