Skip to content

Commit

Permalink
Merge pull request #84 from hashicorp/f-not-initialized
Browse files Browse the repository at this point in the history
terraform: Report uninitialized workspace
  • Loading branch information
radeksimko authored May 6, 2020
2 parents 13b6214 + 0e3e0a3 commit 80c5aaa
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
21 changes: 21 additions & 0 deletions internal/terraform/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,24 @@ func (utv *UnsupportedTerraformVersion) Error() string {

return msg
}

type NotInitializedErr struct {
Dir string
}

func (e *NotInitializedErr) Is(err error) bool {
_, ok := err.(*NotInitializedErr)
if !ok {
return false
}

return true
}

func (e *NotInitializedErr) Error() string {
if e.Dir != "" {
return fmt.Sprintf("workspace %s not initialized", e.Dir)
}

return fmt.Sprintf("workspace not initialized")
}
5 changes: 5 additions & 0 deletions internal/terraform/schema/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ func (w *Watcher) AddWorkspace(dir string) error {

hash, err := fileHashSum(lockPath)
if err != nil {
if os.IsNotExist(err) {
return &errors.NotInitializedErr{
Dir: dir,
}
}
return fmt.Errorf("unable to calculate hash: %w", err)
}

Expand Down
9 changes: 7 additions & 2 deletions langserver/handlers/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package handlers

import (
"context"
"errors"
"fmt"
"os"

lsctx "github.com/hashicorp/terraform-ls/internal/context"
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
"github.com/hashicorp/terraform-ls/internal/terraform/errors"
tferr "github.com/hashicorp/terraform-ls/internal/terraform/errors"
lsp "github.com/sourcegraph/go-lsp"
)

Expand Down Expand Up @@ -65,7 +66,7 @@ func (lh *logHandler) Initialize(ctx context.Context, params lsp.InitializeParam

err = supportsTerraform(tfVersion)
if err != nil {
if uvErr, ok := err.(*errors.UnsupportedTerraformVersion); ok {
if uvErr, ok := err.(*tferr.UnsupportedTerraformVersion); ok {
lh.logger.Printf("Unsupported terraform version: %s", uvErr)
// Which component exactly imposed the constrain may not be relevant
// to the user unless they are very familiar with internals of the LS
Expand Down Expand Up @@ -101,6 +102,10 @@ func (lh *logHandler) Initialize(ctx context.Context, params lsp.InitializeParam

err = ss.AddWorkspaceForWatching(rootURI)
if err != nil {
if errors.Is(err, &tferr.NotInitializedErr{}) {
return serverCaps, fmt.Errorf("Workspace not initialized. "+
"Please run `terraform init` in %s", rootURI)
}
return serverCaps, err
}

Expand Down

0 comments on commit 80c5aaa

Please sign in to comment.