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

terraform: Support 0.13 version #149

Merged
merged 1 commit into from
Jun 9, 2020
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
9 changes: 9 additions & 0 deletions internal/terraform/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ func (utv *UnsupportedTerraformVersion) Error() string {
return msg
}

func (utv *UnsupportedTerraformVersion) Is(err error) bool {
te, ok := err.(*UnsupportedTerraformVersion)
if !ok {
return false
}

return te.Version == utv.Version
}

type NotInitializedErr struct {
Dir string
}
Expand Down
11 changes: 10 additions & 1 deletion internal/terraform/lang/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,19 @@ type parser struct {
}

func ParserSupportsTerraform(v string) error {
tfVersion, err := version.NewVersion(v)
rawVer, err := version.NewVersion(v)
if err != nil {
return err
}

// Assume that alpha/beta/rc prereleases have the same compatibility
segments := rawVer.Segments64()
segmentsOnly := fmt.Sprintf("%d.%d.%d", segments[0], segments[1], segments[2])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it would be useful to upstream these two lines into go-version as Version.RawVersion() or something like that. Not something I'd want to block this PR on, but thinking out loud.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think we should just start implementing this in terraform-exec and see what feels right there. I'd rather not have to have a lot of this stuff in different packages we need to think/worry about and terraform-exec will handle schema dumping, we could also add lock file watching to it, etc.

tfVersion, err := version.NewVersion(segmentsOnly)
if err != nil {
return fmt.Errorf("failed to parse stripped version: %w", err)
}

c, err := version.NewConstraint(parserVersionConstraint)
if err != nil {
return err
Expand Down
14 changes: 11 additions & 3 deletions internal/terraform/schema/schema_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,17 @@ func SchemaSupportsTerraform(v string) error {
return fmt.Errorf("failed to parse constraint: %w", err)
}

ver, err := version.NewVersion(v)
rawVer, err := version.NewVersion(v)
if err != nil {
return fmt.Errorf("failed to parse verison: %w", err)
return fmt.Errorf("failed to parse version: %w", err)
}

// Assume that alpha/beta/rc prereleases have the same compatibility
segments := rawVer.Segments64()
segmentsOnly := fmt.Sprintf("%d.%d.%d", segments[0], segments[1], segments[2])
ver, err := version.NewVersion(segmentsOnly)
if err != nil {
return fmt.Errorf("failed to parse stripped version: %w", err)
}

supported := c.Check(ver)
Expand All @@ -89,7 +97,7 @@ func SchemaSupportsTerraform(v string) error {
}
}

return watcherSupportsTerraform(ver)
return nil
}

func (s *Storage) SetLogger(logger *log.Logger) {
Expand Down
57 changes: 57 additions & 0 deletions internal/terraform/schema/schema_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,68 @@ package schema

import (
"errors"
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
tferr "github.com/hashicorp/terraform-ls/internal/terraform/errors"
)

func TestSchemaSupportsTerraform(t *testing.T) {
testCases := []struct {
version string
expectedErr error
}{
{
"0.11.0",
&tferr.UnsupportedTerraformVersion{Version: "0.11.0"},
},
{
"0.12.0-rc1",
nil,
},
{
"0.12.0",
nil,
},
{
"0.13.0-beta1",
nil,
},
{
"0.14.0-beta1",
nil,
},
{
"0.14.0",
nil,
},
{
"1.0.0",
nil,
},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
err := SchemaSupportsTerraform(tc.version)
if err != nil {
if tc.expectedErr == nil {
t.Fatalf("Expected no error for %q: %#v",
tc.version, err)
}
if !errors.Is(err, tc.expectedErr) {
diff := cmp.Diff(tc.expectedErr, err)
t.Fatalf("%q: error doesn't match: %s",
tc.version, diff)
}
} else if tc.expectedErr != nil {
t.Fatalf("Expected error for %q: %#v",
tc.version, tc.expectedErr)
}
})
}
}

func TestProviderConfigSchema_noSchema(t *testing.T) {
s := NewStorage()
expectedErr := &NoSchemaAvailableErr{}
Expand Down
95 changes: 54 additions & 41 deletions internal/terraform/schema/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"runtime"

"github.com/fsnotify/fsnotify"
"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-ls/internal/terraform/errors"
)

Expand All @@ -23,32 +22,20 @@ type watcher interface {
SetLogger(*log.Logger)
}

func watcherSupportsTerraform(ver *version.Version) error {
c, err := version.NewConstraint(
"< 0.13.0", // Version 0.13 will likely have a different lock file path
)
if err != nil {
return fmt.Errorf("failed to parse constraint: %w", err)
}

supported := c.Check(ver)
if !supported {
return &errors.UnsupportedTerraformVersion{
Component: "plugin watcher",
Version: ver.String(),
Constraints: c,
}
func lockFilePaths(dir string) []string {
return []string{
// Terraform >= v0.13
filepath.Join(dir,
".terraform",
"plugins",
"selections.json"),
// Terraform <= v0.12
filepath.Join(dir,
".terraform",
"plugins",
runtime.GOOS+"_"+runtime.GOARCH,
"lock.json"),
}

return nil
}

func lockFilePath(dir string) string {
return filepath.Join(dir,
".terraform",
"plugins",
runtime.GOOS+"_"+runtime.GOARCH,
"lock.json")
}

// Watcher is a wrapper around native fsnotify.Watcher
Expand All @@ -57,9 +44,9 @@ func lockFilePath(dir string) string {
// (rather than just events that may not be changing any bytes)
// and hold knowledge about workspace structure
type Watcher struct {
w *fsnotify.Watcher
files map[string]*watchedWorkspace
logger *log.Logger
w *fsnotify.Watcher
lockFiles map[string]*watchedWorkspace
logger *log.Logger
}

type watchedWorkspace struct {
Expand All @@ -73,9 +60,9 @@ func NewWatcher() (*Watcher, error) {
return nil, err
}
return &Watcher{
w: w,
files: make(map[string]*watchedWorkspace, 0),
logger: defaultLogger,
w: w,
lockFiles: make(map[string]*watchedWorkspace, 0),
logger: defaultLogger,
}, nil
}

Expand All @@ -84,10 +71,10 @@ func (w *Watcher) SetLogger(logger *log.Logger) {
}

func (w *Watcher) AddWorkspace(dir string) error {
lockPath := lockFilePath(dir)
w.logger.Printf("Adding %q for watching...", lockPath)
lockPaths := lockFilePaths(dir)
w.logger.Printf("Adding %q for watching...", lockPaths)

hash, err := fileHashSum(lockPath)
lockFile, err := findLockFile(lockPaths)
if err != nil {
if os.IsNotExist(err) {
return &errors.NotInitializedErr{
Expand All @@ -97,12 +84,38 @@ func (w *Watcher) AddWorkspace(dir string) error {
return fmt.Errorf("unable to calculate hash: %w", err)
}

w.files[lockPath] = &watchedWorkspace{
pluginsLockFileHash: string(hash),
w.lockFiles[lockFile.path] = &watchedWorkspace{
pluginsLockFileHash: lockFile.hash,
dir: dir,
}

return w.w.Add(lockPath)
return w.w.Add(lockFile.path)
}

type lockFile struct {
path string
hash string
}

func findLockFile(paths []string) (*lockFile, error) {
var b []byte
var err error

for _, path := range paths {
b, err = fileHashSum(path)
if err == nil {
return &lockFile{
path: path,
hash: string(b),
}, nil
}

if !os.IsNotExist(err) {
return nil, err
}
}

return nil, err
}

func fileHashSum(path string) ([]byte, error) {
Expand Down Expand Up @@ -147,12 +160,12 @@ func (w *Watcher) OnPluginChange(f func(*watchedWorkspace) error) {
w.logger.Println("unable to calculate hash:", err)
}
newHash := string(hash)
existingHash := w.files[event.Name].pluginsLockFileHash
existingHash := w.lockFiles[event.Name].pluginsLockFileHash

if newHash != existingHash {
w.files[event.Name].pluginsLockFileHash = newHash
w.lockFiles[event.Name].pluginsLockFileHash = newHash

err = f(w.files[event.Name])
err = f(w.lockFiles[event.Name])
if err != nil {
w.logger.Println("error when executing on change:", err)
}
Expand Down