Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ All notable changes to the Docker Language Server will be documented in this fil
- handle WSL URIs with a dollar sign properly to fix build ARG reference lookups on those hosts ([#393](https://github.com/docker/docker-language-server/issues/393))
- textDocument/documentLink
- convert links properly if a WSL URI with a dollar sign is used ([#378](https://github.com/docker/docker-language-server/issues/378))
- textDocument/inlayHint
- refactor the URI handling code so it will process a WSL URI with a dollar sign correctly ([#395](https://github.com/docker/docker-language-server/issues/395))
- textDocument/inlineCompletion
- convert links properly if a WSL URI with a dollar sign is used ([#384](https://github.com/docker/docker-language-server/issues/384))
- textDocument/publishDiagnostics
Expand Down
4 changes: 2 additions & 2 deletions internal/bake/hcl/inlayHint.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ func InlayHint(docs *document.Manager, doc document.BakeHCLDocument, rng protoco
if block.Type == "target" && len(block.Labels) > 0 {
if attribute, ok := block.Body.Attributes["args"]; ok {
if expr, ok := attribute.Expr.(*hclsyntax.ObjectConsExpr); ok && len(expr.Items) > 0 {
dockerfilePath, err := doc.DockerfileForTarget(block)
dockerfileURI, dockerfilePath, err := doc.DockerfileDocumentPathForTarget(block)
if dockerfilePath != "" && err == nil {
_, nodes := document.OpenDockerfile(context.Background(), docs, "", dockerfilePath)
_, nodes := document.OpenDockerfile(context.Background(), docs, dockerfileURI, dockerfilePath)
args := map[string]string{}
for _, child := range nodes {
if strings.EqualFold(child.Value, "ARG") {
Expand Down
125 changes: 78 additions & 47 deletions internal/bake/hcl/inlayHint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,87 @@ import (
"go.lsp.dev/uri"
)

func TestInlayHint(t *testing.T) {
testCases := []struct {
name string
content string
dockerfileContent string
rng protocol.Range
items []protocol.InlayHint
}{
{
name: "args lookup",
content: "target t1 {\n args = {\n undefined = \"test\"\n empty = \"test\"\n defined = \"test\"\n}\n}",
dockerfileContent: "FROM scratch\nARG undefined\nARG empty=\nARG defined=value\n",
rng: protocol.Range{
Start: protocol.Position{Line: 0, Character: 0},
End: protocol.Position{Line: 5, Character: 0},
},
items: []protocol.InlayHint{
{
Label: "(default value: value)",
PaddingLeft: types.CreateBoolPointer(true),
Position: protocol.Position{Line: 4, Character: 20},
},
},
var testCases = []struct {
name string
content string
dockerfileContent string
rng protocol.Range
items []protocol.InlayHint
}{
{
name: "args lookup",
content: "target t1 {\n args = {\n undefined = \"test\"\n empty = \"test\"\n defined = \"test\"\n}\n}",
dockerfileContent: "FROM scratch\nARG undefined\nARG empty=\nARG defined=value\n",
rng: protocol.Range{
Start: protocol.Position{Line: 0, Character: 0},
End: protocol.Position{Line: 5, Character: 0},
},
{
name: "args lookup outside the range",
content: "target t1 {\n args = {\n undefined = \"test\"\n empty = \"test\"\n defined = \"test\"\n}\n}\n\n\n\n",
dockerfileContent: "FROM scratch\nARG undefined\nARG empty=\nARG defined=value\n",
rng: protocol.Range{
Start: protocol.Position{Line: 8, Character: 0},
End: protocol.Position{Line: 8, Character: 0},
items: []protocol.InlayHint{
{
Label: "(default value: value)",
PaddingLeft: types.CreateBoolPointer(true),
Position: protocol.Position{Line: 4, Character: 20},
},
items: []protocol.InlayHint{},
},
},
{
name: "args lookup outside the range",
content: "target t1 {\n args = {\n undefined = \"test\"\n empty = \"test\"\n defined = \"test\"\n}\n}\n\n\n\n",
dockerfileContent: "FROM scratch\nARG undefined\nARG empty=\nARG defined=value\n",
rng: protocol.Range{
Start: protocol.Position{Line: 8, Character: 0},
End: protocol.Position{Line: 8, Character: 0},
},
items: []protocol.InlayHint{},
},
}

func TestInlayHint(t *testing.T) {
tempDir := os.TempDir()
dockerfileURI := uri.URI(fmt.Sprintf("file:///%v", strings.TrimPrefix(filepath.ToSlash(filepath.Join(tempDir, "Dockerfile")), "/")))
bakeFileURI := uri.URI(fmt.Sprintf("file:///%v", strings.TrimPrefix(filepath.ToSlash(filepath.Join(tempDir, "docker-bake.hcl")), "/")))

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
manager := document.NewDocumentManager()
changed, err := manager.Write(context.Background(), dockerfileURI, protocol.DockerfileLanguage, 1, []byte(tc.dockerfileContent))
require.NoError(t, err)
require.True(t, changed)

doc := document.NewBakeHCLDocument(bakeFileURI, 1, []byte(tc.content))
items, err := InlayHint(manager, doc, tc.rng)
require.NoError(t, err)
require.Equal(t, tc.items, items)
})
}
}

func TestInlayHint_WSL(t *testing.T) {
dockerfileURI := uri.URI("file://wsl%24/docker-desktop/tmp/Dockerfile")
bakeFileURI := uri.URI("file://wsl%24/docker-desktop/tmp/docker-bake.hcl")

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
manager := document.NewDocumentManager()
changed, err := manager.Write(context.Background(), dockerfileURI, protocol.DockerfileLanguage, 1, []byte(tc.dockerfileContent))
require.NoError(t, err)
require.True(t, changed)

doc := document.NewBakeHCLDocument(bakeFileURI, 1, []byte(tc.content))
items, err := InlayHint(manager, doc, tc.rng)
require.NoError(t, err)
require.Equal(t, tc.items, items)
})
}
}

func TestInlayHint_TestFiles(t *testing.T) {
testCases := []struct {
name string
content string
rng protocol.Range
items []protocol.InlayHint
}{
{
name: "args lookup to a different context folder",
content: "target \"backend\" {\n context = \"./backend\"\n args = {\n BACKEND_VAR=\"changed\"\n }\n}",
Expand All @@ -70,28 +117,12 @@ func TestInlayHint(t *testing.T) {
require.NoError(t, err)
projectRoot := filepath.Dir(filepath.Dir(filepath.Dir(wd)))
inlayHintTestFolderPath := filepath.Join(projectRoot, "testdata", "inlayHint")
dockerfilePath := filepath.Join(inlayHintTestFolderPath, "Dockerfile")
bakeFilePath := filepath.Join(inlayHintTestFolderPath, "docker-bake.hcl")
bakeFileURI := uri.URI(fmt.Sprintf("file:///%v", strings.TrimPrefix(filepath.ToSlash(bakeFilePath), "/")))
dockerfileURI := uri.URI(fmt.Sprintf("file:///%v", strings.TrimPrefix(filepath.ToSlash(dockerfilePath), "/")))

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
manager := document.NewDocumentManager()
if len(tc.content) > 0 {
changed, err := manager.Write(context.Background(), dockerfileURI, protocol.DockerfileLanguage, 1, []byte(tc.dockerfileContent))
defer manager.Remove(dockerfileURI)
require.NoError(t, err)
require.True(t, changed)
}
bytes := []byte(tc.content)
err := os.WriteFile(bakeFilePath, bytes, 0644)
require.NoError(t, err)
t.Cleanup(func() {
err := os.Remove(bakeFilePath)
require.NoError(t, err)
})

doc := document.NewBakeHCLDocument(bakeFileURI, 1, []byte(tc.content))
items, err := InlayHint(manager, doc, tc.rng)
require.NoError(t, err)
Expand Down
Loading