Skip to content

Commit

Permalink
indexer: Parse declared modules on walk and change
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Jul 18, 2022
1 parent 2106833 commit cca874e
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
11 changes: 11 additions & 0 deletions internal/indexer/document_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package indexer
import (
"context"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-ls/internal/document"
"github.com/hashicorp/terraform-ls/internal/job"
"github.com/hashicorp/terraform-ls/internal/terraform/module"
Expand Down Expand Up @@ -68,6 +69,16 @@ func (idx *Indexer) decodeModule(ctx context.Context, modHandle document.DirHand
Type: op.OpTypeLoadModuleMetadata.String(),
Defer: func(ctx context.Context, jobErr error) (job.IDs, error) {
ids := make(job.IDs, 0)

var errs *multierror.Error

mcIds, err := idx.decodeDeclaredModuleCalls(modHandle)(ctx, jobErr)
if err != nil {
errs = multierror.Append(errs, err)
} else {
ids = append(ids, mcIds...)
}

id, err := idx.jobStore.EnqueueJob(job.Job{
Dir: modHandle,
Func: func(ctx context.Context) error {
Expand Down
103 changes: 103 additions & 0 deletions internal/indexer/module_calls.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package indexer
import (
"context"
"os"
"path/filepath"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-ls/internal/document"
"github.com/hashicorp/terraform-ls/internal/job"
"github.com/hashicorp/terraform-ls/internal/terraform/module"
op "github.com/hashicorp/terraform-ls/internal/terraform/module/operation"
tfmod "github.com/hashicorp/terraform-schema/module"
)

func (idx *Indexer) decodeInstalledModuleCalls(modHandle document.DirHandle) job.DeferFunc {
Expand Down Expand Up @@ -108,6 +110,107 @@ func (idx *Indexer) decodeInstalledModuleCalls(modHandle document.DirHandle) job
}
}

func (idx *Indexer) decodeDeclaredModuleCalls(modHandle document.DirHandle) job.DeferFunc {
return func(ctx context.Context, opErr error) (job.IDs, error) {
jobIds := make(job.IDs, 0)
if opErr != nil {
return jobIds, opErr
}

moduleCalls, err := idx.modStore.ModuleCalls(modHandle.Path())
if err != nil {
return jobIds, err
}

var errs *multierror.Error

for _, mc := range moduleCalls.Declared {
sourceAddr, ok := mc.SourceAddr.(tfmod.LocalSourceAddr)
if !ok {
continue
}
modPath := filepath.Join(modHandle.Path(), sourceAddr.String())

fi, err := os.Stat(modPath)
if err != nil || !fi.IsDir() {
multierror.Append(errs, err)
continue
}
err = idx.modStore.Add(modPath)
if err != nil {
multierror.Append(errs, err)
continue
}

mcHandle := document.DirHandleFromPath(modPath)

id, err := idx.jobStore.EnqueueJob(job.Job{
Dir: mcHandle,
Func: func(ctx context.Context) error {
return module.ParseModuleConfiguration(idx.fs, idx.modStore, mcHandle.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: mcHandle,
Type: op.OpTypeLoadModuleMetadata.String(),
Func: func(ctx context.Context) error {
return module.LoadModuleMetadata(idx.modStore, mcHandle.Path())
},
Defer: func(ctx context.Context, jobErr error) (job.IDs, error) {
return idx.collectReferences(ctx, mcHandle)
},
})
if err != nil {
return ids, err
} else {
ids = append(ids, id)
}

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

id, err = idx.jobStore.EnqueueJob(job.Job{
Dir: mcHandle,
Func: func(ctx context.Context) error {
return module.ParseVariables(idx.fs, idx.modStore, mcHandle.Path())
},
Type: op.OpTypeParseVariables.String(),
Defer: func(ctx context.Context, jobErr error) (job.IDs, error) {
ids := make(job.IDs, 0)
id, err = idx.jobStore.EnqueueJob(job.Job{
Dir: mcHandle,
Func: func(ctx context.Context) error {
return module.DecodeVarsReferences(ctx, idx.modStore, idx.schemaStore, mcHandle.Path())
},
Type: op.OpTypeDecodeVarsReferences.String(),
})
if err != nil {
return ids, err
}
ids = append(ids, id)
return ids, err
},
})
if err != nil {
multierror.Append(errs, err)
continue
}
jobIds = append(jobIds, id)
}

return jobIds, errs.ErrorOrNil()
}
}

func (idx *Indexer) collectReferences(ctx context.Context, modHandle document.DirHandle) (job.IDs, error) {
ids := make(job.IDs, 0)

Expand Down
1 change: 1 addition & 0 deletions internal/indexer/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func (idx *Indexer) WalkedModule(ctx context.Context, modHandle document.DirHand
Func: func(ctx context.Context) error {
return module.LoadModuleMetadata(idx.modStore, modHandle.Path())
},
Defer: idx.decodeDeclaredModuleCalls(modHandle),
})
if err != nil {
return ids, err
Expand Down

0 comments on commit cca874e

Please sign in to comment.