Skip to content

Commit 351b0d9

Browse files
committed
Ensure include node is an array before processing it
The include node may not be an array if the user has made a mistake or is not finished editing their Compose file so we should not try to process it if it is not an array. Signed-off-by: Remy Suen <remy.suen@docker.com>
1 parent 755373e commit 351b0d9

File tree

4 files changed

+35
-13
lines changed

4 files changed

+35
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ All notable changes to the Docker Language Server will be documented in this fil
1818
- Compose
1919
- textDocument/completion
2020
- fix error case triggered by using code completion before the first node ([#314](https://github.com/docker/docker-language-server/issues/314))
21+
- textDocument/hover
22+
- protect the processing of included files if the node is not a proper array ([#322](https://github.com/docker/docker-language-server/issues/322))
2123
- Bake
2224
- textDocument/inlineCompletion
2325
- check that the request is within the document's bounds when processing the request ([#318](https://github.com/docker/docker-language-server/issues/318))

internal/compose/hover_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,18 @@ volumes:
11271127
character: 13,
11281128
result: nil,
11291129
},
1130+
{
1131+
name: "reference missing and include node is invalid",
1132+
content: `
1133+
include:
1134+
services:
1135+
backend:
1136+
volumes:
1137+
- volA:/mount`,
1138+
line: 5,
1139+
character: 10,
1140+
result: nil,
1141+
},
11301142
}
11311143

11321144
composeFile := fmt.Sprintf("file:///%v", strings.TrimPrefix(filepath.ToSlash(filepath.Join(os.TempDir(), "compose.yaml")), "/"))

internal/pkg/document/dockerComposeDocument.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,22 +130,24 @@ func (d *composeDocument) includedPaths() []string {
130130
if node, ok := doc.Body.(*ast.MappingNode); ok {
131131
for _, topNode := range node.Values {
132132
if topNode.Key.GetToken().Value == "include" {
133-
for _, sequenceValue := range topNode.Value.(*ast.SequenceNode).Values {
134-
if stringArrayItem, ok := sequenceValue.(*ast.StringNode); ok {
135-
paths = append(paths, stringArrayItem.Value)
136-
} else if includeObject, ok := sequenceValue.(*ast.MappingNode); ok {
137-
for _, includeValues := range includeObject.Values {
138-
if includeValues.Key.GetToken().Value == "path" {
139-
if pathArrayItem, ok := includeValues.Value.(*ast.StringNode); ok {
140-
paths = append(paths, pathArrayItem.Value)
141-
} else if pathSequence, ok := includeValues.Value.(*ast.SequenceNode); ok {
142-
for _, sequenceValue := range pathSequence.Values {
143-
if s, ok := sequenceValue.(*ast.StringNode); ok {
144-
paths = append(paths, s.Value)
133+
if sequenceNode, ok := topNode.Value.(*ast.SequenceNode); ok {
134+
for _, sequenceValue := range sequenceNode.Values {
135+
if stringArrayItem, ok := sequenceValue.(*ast.StringNode); ok {
136+
paths = append(paths, stringArrayItem.Value)
137+
} else if includeObject, ok := sequenceValue.(*ast.MappingNode); ok {
138+
for _, includeValues := range includeObject.Values {
139+
if includeValues.Key.GetToken().Value == "path" {
140+
if pathArrayItem, ok := includeValues.Value.(*ast.StringNode); ok {
141+
paths = append(paths, pathArrayItem.Value)
142+
} else if pathSequence, ok := includeValues.Value.(*ast.SequenceNode); ok {
143+
for _, sequenceValue := range pathSequence.Values {
144+
if s, ok := sequenceValue.(*ast.StringNode); ok {
145+
paths = append(paths, s.Value)
146+
}
145147
}
146148
}
149+
break
147150
}
148-
break
149151
}
150152
}
151153
}

internal/pkg/document/dockerComposeDocument_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ include:
112112
- compose.yaml`,
113113
},
114114
},
115+
{
116+
name: "include node is invalid",
117+
content: "include:",
118+
resolved: true,
119+
externalContent: map[string]string{},
120+
},
115121
}
116122

117123
folder := os.TempDir()

0 commit comments

Comments
 (0)