From bd6ce82434db22313c9f68708609900aa2285180 Mon Sep 17 00:00:00 2001 From: Remy Suen Date: Wed, 27 Aug 2025 15:04:34 -0400 Subject: [PATCH] Flag annotations without an equals sign as an error Signed-off-by: Remy Suen --- CHANGELOG.md | 3 +++ internal/bake/hcl/diagnosticsCollector.go | 20 +++++++++++++++++++ .../bake/hcl/diagnosticsCollector_test.go | 18 +++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 421e646..e3acb36 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ All notable changes to the Docker Language Server will be documented in this fil - Dockerfile - textDocument/codeAction - `InvalidBaseImagePlatform` warnings can now be ignored with a code action ([#464](https://github.com/docker/docker-language-server/issues/464)) +- Bake + - textDocument/publishDiagnostics + - flag `annotations` in a `target` block without an equals sign as an error ([#99](https://github.com/docker/docker-language-server/issues/99)) ### Fixed diff --git a/internal/bake/hcl/diagnosticsCollector.go b/internal/bake/hcl/diagnosticsCollector.go index 81bbf23..90aefc3 100644 --- a/internal/bake/hcl/diagnosticsCollector.go +++ b/internal/bake/hcl/diagnosticsCollector.go @@ -228,6 +228,26 @@ func (c *BakeHCLDiagnosticsCollector) CollectDiagnostics(source, workspaceFolder } } + if attribute, ok := block.Body.Attributes["annotations"]; ok { + if expr, ok := attribute.Expr.(*hclsyntax.TupleConsExpr); ok { + for _, e := range expr.Exprs { + if templateExpr, ok := e.(*hclsyntax.TemplateExpr); ok { + if templateExpr.IsStringLiteral() { + value, _ := templateExpr.Value(&hcl.EvalContext{}) + if len(strings.Split(value.AsString(), "=")) < 2 { + diagnostics = append(diagnostics, protocol.Diagnostic{ + Message: fmt.Sprintf(`invalid annotation "%v", expected key=value`, value.AsString()), + Source: types.CreateStringPointer(source), + Severity: types.CreateDiagnosticSeverityPointer(protocol.DiagnosticSeverityError), + Range: createProtocolRange(templateExpr.SrcRange, false), + }) + } + } + } + } + } + } + _, dockerfilePath, err := bakeDoc.DockerfileForTarget(block) if dockerfilePath == "" || err != nil { continue diff --git a/internal/bake/hcl/diagnosticsCollector_test.go b/internal/bake/hcl/diagnosticsCollector_test.go index 54fc08b..5ff926d 100644 --- a/internal/bake/hcl/diagnosticsCollector_test.go +++ b/internal/bake/hcl/diagnosticsCollector_test.go @@ -358,6 +358,24 @@ target "build" { }, }, }, + { + name: "annotations attribute has no equals sign", + content: ` +target "t1" { + annotations = [ "abc" ] +}`, + diagnostics: []protocol.Diagnostic{ + { + Message: `invalid annotation "abc", expected key=value`, + Source: types.CreateStringPointer("docker-language-server"), + Severity: types.CreateDiagnosticSeverityPointer(protocol.DiagnosticSeverityError), + Range: protocol.Range{ + Start: protocol.Position{Line: 2, Character: 18}, + End: protocol.Position{Line: 2, Character: 23}, + }, + }, + }, + }, } wd, err := os.Getwd()