Skip to content

Commit

Permalink
fix(chart): support missing Chart.yaml file
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Feb 3, 2024
1 parent d5f5982 commit 91a341a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 35 deletions.
30 changes: 12 additions & 18 deletions internal/charts/chart_for_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ func (s *ChartStore) GetChartForDoc(uri lsp.DocumentURI) (*Chart, error) {
return chart, nil
}

chart = s.getChartFromFilesystemForTemplates(uri.Filename())

if chart != nil {
s.Charts[chart.RootURI] = chart
return chart, nil
}

return nil, ErrChartNotFound{
URI: uri,
chart, err := s.getChartFromFilesystemForTemplates(uri.Filename())
s.Charts[chart.RootURI] = chart
if err != nil {
return chart, ErrChartNotFound{
URI: uri,
}
}
return chart, nil
}

func (s *ChartStore) getChartFromCache(uri lsp.DocumentURI) *Chart {
Expand All @@ -38,25 +36,21 @@ func (s *ChartStore) getChartFromCache(uri lsp.DocumentURI) *Chart {
return nil
}

func (s *ChartStore) getChartFromFilesystemForTemplates(path string) *Chart {
func (s *ChartStore) getChartFromFilesystemForTemplates(path string) (*Chart, error) {
directory := filepath.Dir(path)
if filepath.Base(directory) == "templates" {
templatesDir := directory
expectedChartDir := filepath.Dir(templatesDir)

// check if Chart.yaml exists
if isChartDirectory(expectedChartDir) {
return s.newChart(uri.File(expectedChartDir), s.valuesFilesConfig)
return s.newChart(uri.File(expectedChartDir), s.valuesFilesConfig), nil
}
}

rootDirectory := s.RootURI.Filename()
if directory == rootDirectory {
return nil
}

if directory == path {
return nil
if directory == rootDirectory || directory == path {
return s.newChart(uri.File(directory), s.valuesFilesConfig), ErrChartNotFound{}
}

return s.getChartFromFilesystemForTemplates(directory)
Expand All @@ -72,5 +66,5 @@ type ErrChartNotFound struct {
}

func (e ErrChartNotFound) Error() string {
return fmt.Sprintf("Chart not found for file: %s", e.URI)
return fmt.Sprintf("Chart not found for file: %s. Using fallback", e.URI)
}
6 changes: 3 additions & 3 deletions internal/charts/chart_for_document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
)

func TestGetChartForDocumentWorksForAlreadyAddedCharts(t *testing.T) {
chartStore := charts.NewChartStore("file:///tmp", func(_ uri.URI, _ util.ValuesFilesConfig) *charts.Chart {
return &charts.Chart{}
chartStore := charts.NewChartStore("file:///tmp", func(uri uri.URI, _ util.ValuesFilesConfig) *charts.Chart {
return &charts.Chart{RootURI: uri}
})

chart := &charts.Chart{}
Expand Down Expand Up @@ -42,7 +42,7 @@ func TestGetChartForDocumentWorksForAlreadyAddedCharts(t *testing.T) {

result5, error := chartStore.GetChartForDoc("file:///tmp/directory/deployment.yaml")
assert.Error(t, error)
assert.Nil(t, result5)
assert.Equal(t, &charts.Chart{RootURI: uri.File("/tmp")}, result5)
}

func TestGetChartForDocumentWorksForNewToAddChart(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (h *langHandler) handleTextDocumentDidOpen(ctx context.Context, reply jsonr
if err != nil {
logger.Error("Error getting chart info for file", doc.URI, err)
}
notification, err := lsplocal.NotifcationFromLint(ctx, h.connPool, chart, doc)
notification, err := lsplocal.NotificationFromLint(ctx, h.connPool, chart, doc)
return reply(ctx, notification, err)
}

Expand Down Expand Up @@ -138,6 +138,6 @@ func (h *langHandler) handleTextDocumentDidSave(ctx context.Context, reply jsonr
}

h.yamllsConnector.DocumentDidSave(doc, params)
notification, err := lsplocal.NotifcationFromLint(ctx, h.connPool, chart, doc)
notification, err := lsplocal.NotificationFromLint(ctx, h.connPool, chart, doc)
return reply(ctx, notification, err)
}
27 changes: 18 additions & 9 deletions internal/handler/initialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package handler
import (
"context"
"encoding/json"
"errors"
"os"

"github.com/mrjosh/helm-ls/internal/adapter/yamlls"
Expand All @@ -16,24 +15,34 @@ import (
)

func (h *langHandler) handleInitialize(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error {
var params lsp.InitializeParams
var (
params lsp.InitializeParams
workspaceURI uri.URI
err error
)
if err := json.Unmarshal(req.Params(), &params); err != nil {
return err
}

if len(params.WorkspaceFolders) == 0 {
return errors.New("length WorkspaceFolders is 0")
logger.Debug("handleInitialize with params ", req.Params())
if len(params.WorkspaceFolders) != 0 {
workspaceURI, err = uri.Parse(params.WorkspaceFolders[0].URI)
if err != nil {
logger.Error("Error parsing workspace URI", err)
return err
}
} else {
logger.Error("length WorkspaceFolders is 0, falling back to current working directory")
workspaceURI = uri.File(".")
}

workspaceURI, err := uri.Parse(params.WorkspaceFolders[0].URI)
if err != nil {
logger.Error("Error parsing workspace URI", err)
return err
}
logger.Debug("Initializing yamllsConnector")
h.yamllsConnector.CallInitialize(workspaceURI)

logger.Debug("Initializing chartStore")
h.chartStore = charts.NewChartStore(workspaceURI, h.NewChartWithWatchedFiles)

logger.Debug("Initializing done")
return reply(ctx, lsp.InitializeResult{
Capabilities: lsp.ServerCapabilities{
TextDocumentSync: lsp.TextDocumentSyncOptions{
Expand Down
7 changes: 5 additions & 2 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ type logger interface {
SetLevel(level logrus.Level)
}

var l logger
var once sync.Once
var (
l logger
once sync.Once
)

// start a new logger
func GetLogger() logger {
Expand All @@ -28,5 +30,6 @@ func GetLogger() logger {
func createLogger() logger {
logger := logrus.New()
logger.SetFormatter(&logrus.JSONFormatter{})
logger.SetLevel(logrus.DebugLevel)
return logger
}
2 changes: 1 addition & 1 deletion internal/lsp/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

var logger = log.GetLogger()

func NotifcationFromLint(ctx context.Context, conn jsonrpc2.Conn, chart *charts.Chart, doc *Document) (*jsonrpc2.Notification, error) {
func NotificationFromLint(ctx context.Context, conn jsonrpc2.Conn, chart *charts.Chart, doc *Document) (*jsonrpc2.Notification, error) {
vals := chart.ValuesFiles.MainValuesFile.Values
if chart.ValuesFiles.OverlayValuesFile != nil {
vals = chartutil.CoalesceTables(chart.ValuesFiles.OverlayValuesFile.Values, chart.ValuesFiles.MainValuesFile.Values)
Expand Down

0 comments on commit 91a341a

Please sign in to comment.