From 9358cc38ca1f88047255b63ac2b9f17ba41128b4 Mon Sep 17 00:00:00 2001 From: Remy Suen Date: Thu, 10 Jul 2025 14:20:08 -0400 Subject: [PATCH 1/2] Fix document links bounds error with some quoted image strings Signed-off-by: Remy Suen --- CHANGELOG.md | 2 ++ internal/compose/documentLink.go | 18 +++++++++--------- internal/compose/documentLink_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3964f9d..0e18b07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,8 @@ All notable changes to the Docker Language Server will be documented in this fil - Compose - textDocument/completion - prevent wildcard object attribute suggestions if the text cursor is not at the right indentation for attributes to be inserted ([#342](https://github.com/docker/docker-language-server/issues/342)) + - textDocument/documentLink + - fix bounds index error if a quoted string just has a registry and the colon character at the end ([#351](https://github.com/docker/docker-language-server/issues/351)) ## [0.13.0] - 2025-07-09 diff --git a/internal/compose/documentLink.go b/internal/compose/documentLink.go index eaed46c..676f38c 100644 --- a/internal/compose/documentLink.go +++ b/internal/compose/documentLink.go @@ -232,10 +232,10 @@ func DocumentLink(ctx context.Context, documentURI protocol.URI, doc document.Co func extractImageLink(nodeValue string) (string, string) { if strings.HasPrefix(nodeValue, "ghcr.io") { idx := strings.LastIndex(nodeValue, ":") + if len(nodeValue) <= 8 { + return "", "" + } if idx == -1 { - if len(nodeValue) <= 8 { - return "", "" - } return nodeValue, fmt.Sprintf("https://%v", nodeValue) } return nodeValue[0:idx], fmt.Sprintf("https://%v", nodeValue[0:idx]) @@ -243,10 +243,10 @@ func extractImageLink(nodeValue string) (string, string) { if strings.HasPrefix(nodeValue, "mcr.microsoft.com") { idx := strings.LastIndex(nodeValue, ":") + if len(nodeValue) <= 18 { + return "", "" + } if idx == -1 { - if len(nodeValue) <= 18 { - return "", "" - } return nodeValue, fmt.Sprintf("https://mcr.microsoft.com/artifact/mar/%v", nodeValue[18:]) } return nodeValue[0:idx], fmt.Sprintf("https://mcr.microsoft.com/artifact/mar/%v", nodeValue[18:idx]) @@ -254,10 +254,10 @@ func extractImageLink(nodeValue string) (string, string) { if strings.HasPrefix(nodeValue, "quay.io") { idx := strings.LastIndex(nodeValue, ":") + if len(nodeValue) <= 8 { + return "", "" + } if idx == -1 { - if len(nodeValue) <= 8 { - return "", "" - } return nodeValue, fmt.Sprintf("https://quay.io/repository/%v", nodeValue[8:]) } return nodeValue[0:idx], fmt.Sprintf("https://quay.io/repository/%v", nodeValue[8:idx]) diff --git a/internal/compose/documentLink_test.go b/internal/compose/documentLink_test.go index eeec5f6..ad62b96 100644 --- a/internal/compose/documentLink_test.go +++ b/internal/compose/documentLink_test.go @@ -501,6 +501,14 @@ services: image: ghcr.io:`, links: nil, }, + { + name: "image: \"ghcr.io:\"", + content: ` +services: + test: + image: "ghcr.io:"`, + links: []protocol.DocumentLink{}, + }, { name: "image: mcr.microsoft.com/powershell", content: ` @@ -593,6 +601,14 @@ services: image: mcr.microsoft.com:`, links: nil, }, + { + name: "image: \"mcr.microsoft.com:\"", + content: ` +services: + test: + image: "mcr.microsoft.com:"`, + links: []protocol.DocumentLink{}, + }, { name: "image: quay.io/prometheus/node-exporter", content: ` @@ -651,6 +667,14 @@ services: image: quay.io:`, links: nil, }, + { + name: "image: \"quay.io:\"", + content: ` +services: + test: + image: "quay.io:"`, + links: []protocol.DocumentLink{}, + }, { name: "anchors and aliases to nothing", content: ` From f181b478b8c157866df6938cc7fcd1cedb6bcc29 Mon Sep 17 00:00:00 2001 From: Remy Suen Date: Thu, 10 Jul 2025 14:23:39 -0400 Subject: [PATCH 2/2] Reorder the conditional statements slightly If the length is shorter than what we are looking we do not even need to bother doing any index calculations so the code has been moved around accordingly. Signed-off-by: Remy Suen --- internal/compose/documentLink.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/compose/documentLink.go b/internal/compose/documentLink.go index 676f38c..f1427fc 100644 --- a/internal/compose/documentLink.go +++ b/internal/compose/documentLink.go @@ -231,10 +231,10 @@ func DocumentLink(ctx context.Context, documentURI protocol.URI, doc document.Co func extractImageLink(nodeValue string) (string, string) { if strings.HasPrefix(nodeValue, "ghcr.io") { - idx := strings.LastIndex(nodeValue, ":") if len(nodeValue) <= 8 { return "", "" } + idx := strings.LastIndex(nodeValue, ":") if idx == -1 { return nodeValue, fmt.Sprintf("https://%v", nodeValue) } @@ -242,10 +242,10 @@ func extractImageLink(nodeValue string) (string, string) { } if strings.HasPrefix(nodeValue, "mcr.microsoft.com") { - idx := strings.LastIndex(nodeValue, ":") if len(nodeValue) <= 18 { return "", "" } + idx := strings.LastIndex(nodeValue, ":") if idx == -1 { return nodeValue, fmt.Sprintf("https://mcr.microsoft.com/artifact/mar/%v", nodeValue[18:]) } @@ -253,10 +253,10 @@ func extractImageLink(nodeValue string) (string, string) { } if strings.HasPrefix(nodeValue, "quay.io") { - idx := strings.LastIndex(nodeValue, ":") if len(nodeValue) <= 8 { return "", "" } + idx := strings.LastIndex(nodeValue, ":") if idx == -1 { return nodeValue, fmt.Sprintf("https://quay.io/repository/%v", nodeValue[8:]) }