Skip to content

Fix build target completion crash from line number differences #211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ All notable changes to the Docker Language Server will be documented in this fil
- Compose
- textDocument/completion
- fix panic in code completion in an empty file ([#196](https://github.com/docker/docker-language-server/issues/196))
- fix line number assumption issues when using code completion for build targets ([#210](https://github.com/docker/docker-language-server/issues/210))
- textDocument/hover
- ensure results are returned even if the file has CRLFs ([#205](https://github.com/docker/docker-language-server/issues/205))
- Bake
Expand Down
6 changes: 4 additions & 2 deletions internal/compose/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,10 @@ func buildTargetCompletionItems(params *protocol.CompletionParams, manager *docu
if _, ok := path[3].Value.(*ast.NullNode); ok {
return createBuildStageItems(params, manager, dockerfilePath, "", prefixLength), true
} else if prefix, ok := path[3].Value.(*ast.StringNode); ok {
offset := int(params.Position.Character) - path[3].Value.GetToken().Position.Column + 1
return createBuildStageItems(params, manager, dockerfilePath, prefix.Value[0:offset], prefixLength), true
if int(params.Position.Line) == path[3].Value.GetToken().Position.Line-1 {
offset := int(params.Position.Character) - path[3].Value.GetToken().Position.Column + 1
return createBuildStageItems(params, manager, dockerfilePath, prefix.Value[0:offset], prefixLength), true
}
}
}
}
Expand Down
38 changes: 38 additions & 0 deletions internal/compose/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3410,6 +3410,44 @@ services:
}
},
},
{
name: "completion on line different from the target attribute",
dockerfileContent: "FROM alpine as astage",
content: `
services:
postgres:
build:
target:
`,
line: 5,
character: 8,
list: func() *protocol.CompletionList {
return &protocol.CompletionList{
Items: []protocol.CompletionItem{
{
Label: "astage",
Documentation: "alpine",
TextEdit: textEdit("astage", 5, 8, 0),
},
},
}
},
},
{
name: "completion on a different line from target that already has content",
dockerfileContent: "FROM scratch AS stage",
content: `
services:
postgres:
build:
target: ab
`,
line: 5,
character: 8,
list: func() *protocol.CompletionList {
return nil
},
},
{
name: "no build stages suggested if dockerfile_inline used",
dockerfileContent: "FROM scratch AS base",
Expand Down
Loading