Skip to content

Commit 9cadd16

Browse files
authored
Merge pull request #312 from docker/bake-definition-nil-fix
Fix Bake attribute definitions that referenced nil pointers
2 parents f7b5cf2 + 322c2e3 commit 9cadd16

File tree

3 files changed

+98
-2
lines changed

3 files changed

+98
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ All notable changes to the Docker Language Server will be documented in this fil
2424
- stop volume named references from causing volume attributes to not be suggested ([#309](https://github.com/docker/docker-language-server/issues/309))
2525
- textDocument/documentLink
2626
- ensure the image attribute is valid before trying to process it for document links ([#306](https://github.com/docker/docker-language-server/issues/306))
27+
- Bake
28+
- textDocument/definition
29+
- fix nil pointers when navigating around a top level attribute that is not in any block ([#311](https://github.com/docker/docker-language-server/issues/311))
2730

2831
## [0.10.2] - 2025-06-06
2932

internal/bake/hcl/definition.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func ResolveAttributeValue(ctx context.Context, definitionLinkSupport bool, mana
7070
if tupleConsExpr, ok := attribute.Expr.(*hclsyntax.TupleConsExpr); ok {
7171
for _, e := range tupleConsExpr.Exprs {
7272
if isInsideRange(e.Range(), position) {
73-
if templateExpr, ok := e.(*hclsyntax.TemplateExpr); ok {
73+
if templateExpr, ok := e.(*hclsyntax.TemplateExpr); ok && sourceBlock != nil {
7474
if templateExpr.IsStringLiteral() {
7575
// look up a target reference if it's inside a
7676
// target block's inherits attribute, or a
@@ -169,7 +169,7 @@ func ResolveExpression(ctx context.Context, definitionLinkSupport bool, manager
169169

170170
if objectConsExpression, ok := expression.(*hclsyntax.ObjectConsExpr); ok {
171171
for _, item := range objectConsExpression.Items {
172-
if isInsideRange(item.KeyExpr.Range(), position) {
172+
if isInsideRange(item.KeyExpr.Range(), position) && sourceBlock != nil {
173173
if attributeName == "args" && sourceBlock.Type == "target" {
174174
dockerfilePath, err := doc.DockerfileForTarget(sourceBlock)
175175
if dockerfilePath == "" || err != nil {

internal/bake/hcl/definition_test.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,99 @@ func TestDefinition(t *testing.T) {
982982
},
983983
},
984984
},
985+
{
986+
name: "attribute whitespace should resolve to nothing",
987+
content: "a1 = \"value\"",
988+
line: 0,
989+
character: 3,
990+
endCharacter: -1,
991+
locations: nil,
992+
links: nil,
993+
},
994+
{
995+
name: "attribute array string value",
996+
content: "a1 = [\"value\"]",
997+
line: 0,
998+
character: 10,
999+
endCharacter: -1,
1000+
locations: nil,
1001+
links: nil,
1002+
},
1003+
{
1004+
name: "attribute array string has a templated variable",
1005+
content: "a1 = \"\"\na2 = [\"${a1}\"]",
1006+
line: 1,
1007+
character: 10,
1008+
endCharacter: -1,
1009+
locations: []protocol.Location{
1010+
{
1011+
URI: bakeFileURI,
1012+
Range: protocol.Range{
1013+
Start: protocol.Position{Line: 0, Character: 0},
1014+
End: protocol.Position{Line: 0, Character: 2},
1015+
},
1016+
},
1017+
},
1018+
links: []protocol.LocationLink{
1019+
{
1020+
OriginSelectionRange: &protocol.Range{
1021+
Start: protocol.Position{Line: 1, Character: 9},
1022+
End: protocol.Position{Line: 1, Character: 11},
1023+
},
1024+
TargetURI: bakeFileURI,
1025+
TargetRange: protocol.Range{
1026+
Start: protocol.Position{Line: 0, Character: 0},
1027+
End: protocol.Position{Line: 0, Character: 2},
1028+
},
1029+
TargetSelectionRange: protocol.Range{
1030+
Start: protocol.Position{Line: 0, Character: 0},
1031+
End: protocol.Position{Line: 0, Character: 2},
1032+
},
1033+
},
1034+
},
1035+
},
1036+
{
1037+
name: "attribute object value's attribute name",
1038+
content: "args = { var = value }",
1039+
line: 0,
1040+
character: 11,
1041+
endCharacter: -1,
1042+
locations: nil,
1043+
links: nil,
1044+
},
1045+
{
1046+
name: "attribute object value's attribute name",
1047+
content: "a1 = \"\"\nargs = { var = a1 }",
1048+
line: 1,
1049+
character: 16,
1050+
endCharacter: -1,
1051+
locations: []protocol.Location{
1052+
{
1053+
URI: bakeFileURI,
1054+
Range: protocol.Range{
1055+
Start: protocol.Position{Line: 0, Character: 0},
1056+
End: protocol.Position{Line: 0, Character: 2},
1057+
},
1058+
},
1059+
},
1060+
links: []protocol.LocationLink{
1061+
{
1062+
OriginSelectionRange: &protocol.Range{
1063+
Start: protocol.Position{Line: 1, Character: 15},
1064+
End: protocol.Position{Line: 1, Character: 17},
1065+
},
1066+
TargetURI: bakeFileURI,
1067+
TargetRange: protocol.Range{
1068+
Start: protocol.Position{Line: 0, Character: 0},
1069+
End: protocol.Position{Line: 0, Character: 2},
1070+
},
1071+
TargetSelectionRange: protocol.Range{
1072+
Start: protocol.Position{Line: 0, Character: 0},
1073+
End: protocol.Position{Line: 0, Character: 2},
1074+
},
1075+
},
1076+
},
1077+
},
9851078
{
9861079
name: "variable referenced in for loop conditional",
9871080
content: "variable num { default = 3 }\nvariable varList { default = [\"tag\"] }\ntarget default {\n tags = [for var in varList : upper(var) if num > 2]\n}",

0 commit comments

Comments
 (0)