From ecf8c060ceba18972a584a7826876fa49411836c Mon Sep 17 00:00:00 2001 From: Remy Suen Date: Wed, 11 Jun 2025 06:13:29 -0400 Subject: [PATCH] Ignore inline completion request if parameters are out of bounds Signed-off-by: Remy Suen --- CHANGELOG.md | 3 +++ e2e-tests/inlineCompletion_test.go | 14 +++++++++++++- internal/bake/hcl/inlineCompletion.go | 3 +++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62e5656..6f85472 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,9 @@ All notable changes to the Docker Language Server will be documented in this fil - Compose - textDocument/completion - fix error case triggered by using code completion before the first node ([#314](https://github.com/docker/docker-language-server/issues/314)) +- Bake + - textDocument/inlineCompletion + - check that the request is within the document's bounds when processing the request ([#318](https://github.com/docker/docker-language-server/issues/318)) ## [0.11.0] - 2025-06-10 diff --git a/e2e-tests/inlineCompletion_test.go b/e2e-tests/inlineCompletion_test.go index d6a0504..71cafd0 100644 --- a/e2e-tests/inlineCompletion_test.go +++ b/e2e-tests/inlineCompletion_test.go @@ -31,12 +31,16 @@ func TestInlineCompletion(t *testing.T) { testCases := []struct { name string content string + line protocol.UInteger + character protocol.UInteger dockerfileContent string items []protocol.InlineCompletionItem }{ { name: "one build stage", content: "", + line: 0, + character: 0, dockerfileContent: "FROM scratch AS simple", items: []protocol.InlineCompletionItem{ { @@ -48,6 +52,14 @@ func TestInlineCompletion(t *testing.T) { }, }, }, + { + name: "outside document bounds", + content: "", + line: 1, + character: 0, + dockerfileContent: "FROM scratch AS simple", + items: nil, + }, } temporaryDockerfile := fmt.Sprintf("file:///%v", strings.TrimPrefix(filepath.ToSlash(filepath.Join(os.TempDir(), "Dockerfile")), "/")) @@ -81,7 +93,7 @@ func TestInlineCompletion(t *testing.T) { err = conn.Call(context.Background(), protocol.MethodTextDocumentInlineCompletion, protocol.InlineCompletionParams{ TextDocumentPositionParams: protocol.TextDocumentPositionParams{ TextDocument: protocol.TextDocumentIdentifier{URI: didOpenBakeFile.TextDocument.URI}, - Position: protocol.Position{Line: 0, Character: 0}, + Position: protocol.Position{Line: tc.line, Character: tc.character}, }, }, &result) require.NoError(t, err) diff --git a/internal/bake/hcl/inlineCompletion.go b/internal/bake/hcl/inlineCompletion.go index d82aaf6..79f9dfe 100644 --- a/internal/bake/hcl/inlineCompletion.go +++ b/internal/bake/hcl/inlineCompletion.go @@ -40,6 +40,9 @@ func shouldSuggest(content []byte, body *hclsyntax.Body, position protocol.Posit } lines := strings.Split(string(content), "\n") + if len(lines) <= int(position.Line) { + return false + } return strings.TrimSpace(lines[position.Line]) != "}" }