Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Jul 8, 2022
1 parent 2f1556a commit 0ff6d0c
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 50 deletions.
38 changes: 32 additions & 6 deletions internal/indexer/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,40 @@ func (idx *Indexer) WalkedModule(ctx context.Context, modHandle document.DirHand
ids := make(job.IDs, 0)
var errs *multierror.Error

// blockingJobIds tracks job IDs which need to finish
// prior to collecting references
blockingJobIds := make(job.IDs, 0)

id, err := idx.jobStore.EnqueueJob(job.Job{
Dir: modHandle,
Func: func(ctx context.Context) error {
return module.ParseModuleConfiguration(idx.fs, idx.modStore, modHandle.Path())
},
Type: op.OpTypeParseModuleConfiguration.String(),
Defer: func(ctx context.Context, jobErr error) (job.IDs, error) {
ids := make(job.IDs, 0)

id, err := idx.jobStore.EnqueueJob(job.Job{
Dir: modHandle,
Type: op.OpTypeLoadModuleMetadata.String(),
Func: func(ctx context.Context) error {
return module.LoadModuleMetadata(idx.modStore, modHandle.Path())
},
})
if err != nil {
return ids, err
} else {
ids = append(ids, id)
}

return ids, nil
},
})
if err != nil {
errs = multierror.Append(errs, err)
} else {
ids = append(ids, id)
blockingJobIds = append(blockingJobIds, id)
}

id, err = idx.jobStore.EnqueueJob(job.Job{
Expand Down Expand Up @@ -70,6 +93,7 @@ func (idx *Indexer) WalkedModule(ctx context.Context, modHandle document.DirHand
errs = multierror.Append(errs, err)
} else {
ids = append(ids, id)
blockingJobIds = append(blockingJobIds, id)
}

dataDir := datadir.WalkDataDirOfModule(idx.fs, modHandle.Path())
Expand All @@ -88,6 +112,7 @@ func (idx *Indexer) WalkedModule(ctx context.Context, modHandle document.DirHand
errs = multierror.Append(errs, err)
} else {
ids = append(ids, id)
blockingJobIds = append(blockingJobIds, id)
}
}

Expand All @@ -106,15 +131,16 @@ func (idx *Indexer) WalkedModule(ctx context.Context, modHandle document.DirHand
errs = multierror.Append(errs, err)
} else {
ids = append(ids, id)
blockingJobIds = append(blockingJobIds, id)
}

// Here we wait for all module calls to be processed to
// reflect any metadata required to collect reference origins.
// This assumes scheduler is running to consume the jobs
// by the time we reach this point.
idx.jobStore.WaitForJobs(ctx, id)
}

// Here we wait for all dependent jobs to be processed to
// reflect any data required to collect reference origins.
// This assumes scheduler is running to consume the jobs
// by the time we reach this point.
idx.jobStore.WaitForJobs(ctx, id)

rIds, err := idx.collectReferences(ctx, modHandle)
if err != nil {
errs = multierror.Append(errs, err)
Expand Down
2 changes: 1 addition & 1 deletion internal/langserver/handlers/complete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestModuleCompletion_withoutInitialization(t *testing.T) {
}`, TempDir(t).URI)}, session.SessionNotInitialized.Err())
}

func TestModuleCompletion_withValidData(t *testing.T) {
func TestModuleCompletion_withValidData_basic(t *testing.T) {
tmpDir := TempDir(t)
InitPluginCache(t, tmpDir.Path())

Expand Down
16 changes: 8 additions & 8 deletions internal/langserver/handlers/did_change_watched_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,22 +735,22 @@ func TestLangServer_DidChangeWatchedFiles_pluginChange(t *testing.T) {
},
{
Method: "GetExecPath",
Repeatability: 2,
Repeatability: 1,
ReturnArguments: []interface{}{
"",
},
},
{
Method: "ProviderSchemas",
Repeatability: 2,
Repeatability: 1,
Arguments: []interface{}{
mock.AnythingOfType(""),
},
ReturnArguments: []interface{}{
&tfjson.ProviderSchemas{
FormatVersion: "0.1",
Schemas: map[string]*tfjson.ProviderSchema{
"test": {
"foo": {
ConfigSchema: &tfjson.Schema{},
},
},
Expand Down Expand Up @@ -780,12 +780,12 @@ func TestLangServer_DidChangeWatchedFiles_pluginChange(t *testing.T) {
ReqParams: "{}",
})

addr := tfaddr.MustParseProviderSource("-/test")
addr := tfaddr.MustParseProviderSource("-/foo")
vc := version.MustConstraints(version.NewConstraint(">= 1.0"))

_, err = ss.ProviderSchemas.ProviderSchema(testHandle.Path(), addr, vc)
if err == nil {
t.Fatal("expected -/test schema to be missing")
t.Fatal("expected -/foo schema to be missing")
}

// Install Terraform
Expand Down Expand Up @@ -876,7 +876,7 @@ func TestLangServer_DidChangeWatchedFiles_moduleInstalled(t *testing.T) {
ReqParams: "{}",
})

submodulePath := filepath.Join(testDir, "application")
submodulePath := filepath.Join(testDir, ".terraform", "modules", "azure-hcp-consul")
_, err = ss.Modules.ModuleByPath(submodulePath)
if err == nil || !state.IsModuleNotFound(err) {
t.Fatalf("expected submodule not to be found: %s", err)
Expand Down Expand Up @@ -922,7 +922,7 @@ func TestLangServer_DidChangeWatchedFiles_moduleInstalled(t *testing.T) {
t.Fatal(err)
}

if len(mod.Meta.Variables) != 3 {
t.Fatalf("expected exactly 3 variables, %d given", len(mod.Meta.Variables))
if len(mod.Meta.Variables) != 8 {
t.Fatalf("expected exactly 8 variables, %d given", len(mod.Meta.Variables))
}
}
10 changes: 10 additions & 0 deletions internal/langserver/handlers/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,14 @@ func InitPluginCache(t *testing.T, dir string) {
if err != nil {
t.Fatal(err)
}

// create an empty file such that it's recognized as an indexable workspace
f, err = os.Create(filepath.Join(dir, "empty.tf"))
if err != nil {
t.Fatal(err)
}
err = f.Close()
if err != nil {
t.Fatal(err)
}
}
1 change: 1 addition & 0 deletions internal/langserver/handlers/references_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ output "foo" {
}

func TestReferences_variableToModuleInput(t *testing.T) {
// TODO: fix - fails w/ -count=2+
rootModPath, err := filepath.Abs(filepath.Join("testdata", "single-submodule"))
if err != nil {
t.Fatal(err)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
module "gorilla-app" {
source = "./application"
environment_name = "prod"
app_prefix = "protect-gorillas"
instances = 5
module "azure-hcp-consul" {
source = "github.com/hashicorp/terraform-azurerm-hcp-consul?ref=v0.2.4"
}
4 changes: 2 additions & 2 deletions internal/terraform/ast/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (mf ModFilename) IsJSON() bool {
}

func (mf ModFilename) IsIgnored() bool {
return isIgnoredFile(string(mf))
return IsIgnoredFile(string(mf))
}

func IsModuleFilename(name string) bool {
Expand All @@ -28,7 +28,7 @@ func IsModuleFilename(name string) bool {
// isIgnoredFile returns true if the given filename (which must not have a
// directory path ahead of it) should be ignored as e.g. an editor swap file.
// See https://github.com/hashicorp/terraform/blob/d35bc05/internal/configs/parser_config_dir.go#L107
func isIgnoredFile(name string) bool {
func IsIgnoredFile(name string) bool {
return strings.HasPrefix(name, ".") || // Unix-like hidden files
strings.HasSuffix(name, "~") || // vim
strings.HasPrefix(name, "#") && strings.HasSuffix(name, "#") // emacs
Expand Down
21 changes: 17 additions & 4 deletions internal/walker/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/hashicorp/terraform-ls/internal/document"
"github.com/hashicorp/terraform-ls/internal/job"
"github.com/hashicorp/terraform-ls/internal/terraform/datadir"
"github.com/hashicorp/terraform-ls/internal/terraform/ast"
)

var (
Expand Down Expand Up @@ -159,6 +159,8 @@ func (w *Walker) isSkippableDir(dirName string) bool {
}

func (w *Walker) walk(ctx context.Context, dir document.DirHandle) error {
dirsWalked := make(map[string]struct{}, 0)

err := fs.WalkDir(w.fs, dir.Path(), func(path string, info fs.DirEntry, err error) error {
select {
case <-ctx.Done():
Expand Down Expand Up @@ -186,7 +188,18 @@ func (w *Walker) walk(ctx context.Context, dir document.DirHandle) error {
return filepath.SkipDir
}

if info.Name() == datadir.DataDirName {
// TODO: replace local map lookup with w.modStore.HasChangedSince(modTime)
// once available
// See https://github.com/hashicorp/terraform-ls/issues/989
_, walked := dirsWalked[dir]

w.logger.Printf("walker checking file %q; !walked: %t && isModule: %t && !isIgnored: %t",
info.Name(),
walked, ast.IsModuleFilename(info.Name()), ast.IsIgnoredFile(info.Name()))

if !walked && ast.IsModuleFilename(info.Name()) && !ast.IsIgnoredFile(info.Name()) {
dirsWalked[dir] = struct{}{}

w.logger.Printf("found module %s", dir)

exists, err := w.modStore.Exists(dir)
Expand All @@ -210,8 +223,8 @@ func (w *Walker) walk(ctx context.Context, dir document.DirHandle) error {
return nil
}

if !info.IsDir() {
// All files are skipped, we only care about dirs
if info.IsDir() {
// All other files are skipped
return nil
}

Expand Down
Loading

0 comments on commit 0ff6d0c

Please sign in to comment.