Skip to content
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

fix: prevent panic on empty or unsupported template context #83

Merged
merged 1 commit into from
May 19, 2024
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 internal/handler/completion_main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func TestCompletionMainSingleLines(t *testing.T) {
{`Test completion on {{ range .Values.ingress.hosts }} {{ .^ }} {{ end }}`, []string{"host", "paths"}, []string{}, nil},
{`Test completion on {{ range .Values.ingress.hosts }} {{ .ho^ }} {{ end }}`, []string{"host", "paths"}, []string{}, nil},
{`Test completion on {{ range .Values.ingress.hosts }} {{ range .paths }} {{ .^ }} {{ end }} {{ end }}`, []string{"pathType", "path"}, []string{}, nil},
{`Test completion on {{ root := . }} {{ $root.test.^ }}`, []string{}, []string{}, errors.New("[$root test ] is no valid template context for helm")},
}

for _, tt := range testCases {
Expand Down
10 changes: 10 additions & 0 deletions internal/handler/hover_main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handler

import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -113,6 +114,15 @@ func TestHoverMain(t *testing.T) {
expected: fmt.Sprintf("### %s\n%s\n\n", filepath.Join("..", "..", "testdata", "example", "values.yaml"), "1"),
expectedError: nil,
},
{
desc: "Test hover on template context with variables",
position: lsp.Position{
Line: 74,
Character: 50,
},
expected: "",
expectedError: errors.New("no template context found"),
},
}
for _, tt := range testCases {
t.Run(tt.desc, func(t *testing.T) {
Expand Down
10 changes: 4 additions & 6 deletions internal/language_features/built_in_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@

allowedBuiltIns := []string{"Chart", "Values", "Files", "Template", "Release"}

templateContext, _ := f.getTemplateContext()
if len(templateContext) != 1 {
templateContext, err := f.getTemplateContext()
if err != nil || len(templateContext) != 1 {
return false
}
for _, allowedBuiltIn := range allowedBuiltIns {
Expand All @@ -41,10 +41,7 @@
}

func (f *BuiltInObjectsFeature) References() (result []lsp.Location, err error) {
templateContext, err := f.getTemplateContext()
if err != nil {
return []lsp.Location{}, err
}
templateContext, _ := f.getTemplateContext()

locations := f.getReferencesFromSymbolTable(templateContext)
return append(locations, f.getDefinitionLocations(templateContext)...), err
Expand All @@ -54,12 +51,12 @@
locations := []lsp.Location{}

switch templateContext[0] {
case "Values":

Check failure on line 54 in internal/language_features/built_in_objects.go

View workflow job for this annotation

GitHub Actions / lint (1.21.5, ubuntu-latest)

string `Values` has 4 occurrences, make it a constant (goconst)
for _, valueFile := range f.Chart.ValuesFiles.AllValuesFiles() {
locations = append(locations, lsp.Location{URI: valueFile.URI})
}
return locations
case "Chart":

Check failure on line 59 in internal/language_features/built_in_objects.go

View workflow job for this annotation

GitHub Actions / lint (1.21.5, ubuntu-latest)

string `Chart` has 3 occurrences, make it a constant (goconst)
return []lsp.Location{{URI: f.Chart.ChartMetadata.URI}}
}

Expand All @@ -68,6 +65,7 @@

func (f *BuiltInObjectsFeature) Hover() (string, error) {
templateContext, _ := f.getTemplateContext()

docs, err := f.builtInOjectDocsLookup(templateContext[0], helmdocs.BuiltInObjects)
return docs.Doc, err
}
Expand Down
7 changes: 5 additions & 2 deletions internal/language_features/template_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (f *TemplateContextFeature) AppropriateForNode() bool {

func (f *TemplateContextFeature) References() (result []lsp.Location, err error) {
templateContext, err := f.getTemplateContext()
if err != nil {
if err != nil || len(templateContext) == 0 {
return []lsp.Location{}, err
}

Expand All @@ -45,7 +45,7 @@ func (f *TemplateContextFeature) References() (result []lsp.Location, err error)

func (f *TemplateContextFeature) Definition() (result []lsp.Location, err error) {
templateContext, err := f.getTemplateContext()
if err != nil {
if err != nil || len(templateContext) == 0 {
return []lsp.Location{}, err
}
return f.getDefinitionLocations(templateContext), nil
Expand Down Expand Up @@ -81,6 +81,9 @@ func (f *TemplateContextFeature) getDefinitionLocations(templateContext lsplocal

func (f *TemplateContextFeature) Hover() (string, error) {
templateContext, err := f.getTemplateContext()
if err != nil || len(templateContext) == 0 {
return "", err
}

switch templateContext[0] {
case "Values":
Expand Down
11 changes: 11 additions & 0 deletions testdata/example/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,14 @@ spec:
{{- end }}
{{- end }}
hosts: {{ .Values.ingress.hosts }}

{{- $root := . -}}
{{- range $type, $config := $root.Values.deployments }}
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app-{{ $type }}
spec:
replicas: {{ $config.hpa.minReplicas }}
---
{{- end }}
Loading