Skip to content

Commit 80c5aaa

Browse files
authored
Merge pull request hashicorp#84 from hashicorp/f-not-initialized
terraform: Report uninitialized workspace
2 parents 13b6214 + 0e3e0a3 commit 80c5aaa

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

internal/terraform/errors/errors.go

+21
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,24 @@ func (utv *UnsupportedTerraformVersion) Error() string {
2828

2929
return msg
3030
}
31+
32+
type NotInitializedErr struct {
33+
Dir string
34+
}
35+
36+
func (e *NotInitializedErr) Is(err error) bool {
37+
_, ok := err.(*NotInitializedErr)
38+
if !ok {
39+
return false
40+
}
41+
42+
return true
43+
}
44+
45+
func (e *NotInitializedErr) Error() string {
46+
if e.Dir != "" {
47+
return fmt.Sprintf("workspace %s not initialized", e.Dir)
48+
}
49+
50+
return fmt.Sprintf("workspace not initialized")
51+
}

internal/terraform/schema/watcher.go

+5
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ func (w *Watcher) AddWorkspace(dir string) error {
8989

9090
hash, err := fileHashSum(lockPath)
9191
if err != nil {
92+
if os.IsNotExist(err) {
93+
return &errors.NotInitializedErr{
94+
Dir: dir,
95+
}
96+
}
9297
return fmt.Errorf("unable to calculate hash: %w", err)
9398
}
9499

langserver/handlers/initialize.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ package handlers
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"os"
78

89
lsctx "github.com/hashicorp/terraform-ls/internal/context"
910
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
10-
"github.com/hashicorp/terraform-ls/internal/terraform/errors"
11+
tferr "github.com/hashicorp/terraform-ls/internal/terraform/errors"
1112
lsp "github.com/sourcegraph/go-lsp"
1213
)
1314

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

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

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

0 commit comments

Comments
 (0)