Skip to content

Commit

Permalink
indexer: refactor & improve/cleanup error handling (#988)
Browse files Browse the repository at this point in the history
* avoid extra function arguments

* indexer: extract module call-related jobs

* indexer: improve/cleanup error handling

 - capture and log any errors from Deferred jobs
 - capture and log any errors from other places which were previously skipped on error (via multierror)

* walker: fix error collection

* scheduler/state: avoid passing JobStore via ctx

This (hack) is no longer necessary since all indexing logic is now self-contained within one package.
  • Loading branch information
radeksimko authored Jul 6, 2022
1 parent 14e71fd commit d5602d9
Show file tree
Hide file tree
Showing 11 changed files with 211 additions and 214 deletions.
28 changes: 13 additions & 15 deletions internal/indexer/document_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@ func (idx *Indexer) DocumentChanged(modHandle document.DirHandle) (job.IDs, erro
return module.ParseModuleConfiguration(idx.fs, idx.modStore, modHandle.Path())
},
Type: op.OpTypeParseModuleConfiguration.String(),
Defer: func(ctx context.Context, jobErr error) job.IDs {
ids, err := idx.decodeModule(ctx, modHandle)
if err != nil {
idx.logger.Printf("error: %s", err)
}
return ids
Defer: func(ctx context.Context, jobErr error) (job.IDs, error) {
return idx.decodeModule(ctx, modHandle)
},
})
if err != nil {
Expand All @@ -37,7 +33,8 @@ func (idx *Indexer) DocumentChanged(modHandle document.DirHandle) (job.IDs, erro
return module.ParseVariables(idx.fs, idx.modStore, modHandle.Path())
},
Type: op.OpTypeParseVariables.String(),
Defer: func(ctx context.Context, jobErr error) (ids job.IDs) {
Defer: func(ctx context.Context, jobErr error) (job.IDs, error) {
ids := make(job.IDs, 0)
id, err := idx.jobStore.EnqueueJob(job.Job{
Dir: modHandle,
Func: func(ctx context.Context) error {
Expand All @@ -46,10 +43,10 @@ func (idx *Indexer) DocumentChanged(modHandle document.DirHandle) (job.IDs, erro
Type: op.OpTypeDecodeVarsReferences.String(),
})
if err != nil {
return
return ids, err
}
ids = append(ids, id)
return
return ids, nil
},
})
if err != nil {
Expand All @@ -69,7 +66,8 @@ func (idx *Indexer) decodeModule(ctx context.Context, modHandle document.DirHand
return module.LoadModuleMetadata(idx.modStore, modHandle.Path())
},
Type: op.OpTypeLoadModuleMetadata.String(),
Defer: func(ctx context.Context, jobErr error) (ids job.IDs) {
Defer: func(ctx context.Context, jobErr error) (job.IDs, error) {
ids := make(job.IDs, 0)
id, err := idx.jobStore.EnqueueJob(job.Job{
Dir: modHandle,
Func: func(ctx context.Context) error {
Expand All @@ -78,7 +76,7 @@ func (idx *Indexer) decodeModule(ctx context.Context, modHandle document.DirHand
Type: op.OpTypeDecodeReferenceTargets.String(),
})
if err != nil {
return
return ids, err
}
ids = append(ids, id)

Expand All @@ -90,7 +88,7 @@ func (idx *Indexer) decodeModule(ctx context.Context, modHandle document.DirHand
Type: op.OpTypeDecodeReferenceOrigins.String(),
})
if err != nil {
return
return ids, err
}
ids = append(ids, id)

Expand All @@ -104,11 +102,11 @@ func (idx *Indexer) decodeModule(ctx context.Context, modHandle document.DirHand
Type: op.OpTypeGetModuleDataFromRegistry.String(),
})
if err != nil {
return
return ids, err
}
ids = append(ids, id)

return
ids = append(ids, id)
return ids, nil
},
})
if err != nil {
Expand Down
126 changes: 0 additions & 126 deletions internal/indexer/indexer.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
package indexer

import (
"context"
"io/ioutil"
"log"
"os"

"github.com/hashicorp/terraform-ls/internal/document"
"github.com/hashicorp/terraform-ls/internal/job"
"github.com/hashicorp/terraform-ls/internal/registry"
"github.com/hashicorp/terraform-ls/internal/state"
"github.com/hashicorp/terraform-ls/internal/terraform/exec"
"github.com/hashicorp/terraform-ls/internal/terraform/module"
op "github.com/hashicorp/terraform-ls/internal/terraform/module/operation"
)

type Indexer struct {
Expand Down Expand Up @@ -51,124 +46,3 @@ func (idx *Indexer) SetLogger(logger *log.Logger) {
type Collector interface {
CollectJobId(jobId job.ID)
}

func decodeInstalledModuleCalls(fs ReadOnlyFS, modStore *state.ModuleStore, schemaReader state.SchemaReader, modPath string) job.DeferFunc {
return func(ctx context.Context, opErr error) (jobIds job.IDs) {
if opErr != nil {
return
}

moduleCalls, err := modStore.ModuleCalls(modPath)
if err != nil {
return
}

jobStore, err := job.JobStoreFromContext(ctx)
if err != nil {
return
}

for _, mc := range moduleCalls.Installed {
fi, err := os.Stat(mc.Path)
if err != nil || !fi.IsDir() {
continue
}
modStore.Add(mc.Path)

mcHandle := document.DirHandleFromPath(mc.Path)
// copy path for queued jobs below
mcPath := mc.Path

id, err := jobStore.EnqueueJob(job.Job{
Dir: mcHandle,
Func: func(ctx context.Context) error {
return module.ParseModuleConfiguration(fs, modStore, mcPath)
},
Type: op.OpTypeParseModuleConfiguration.String(),
Defer: func(ctx context.Context, jobErr error) (ids job.IDs) {
id, err := jobStore.EnqueueJob(job.Job{
Dir: mcHandle,
Type: op.OpTypeLoadModuleMetadata.String(),
Func: func(ctx context.Context) error {
return module.LoadModuleMetadata(modStore, mcPath)
},
})
if err != nil {
return
}
ids = append(ids, id)

rIds := collectReferences(ctx, mcHandle, modStore, schemaReader)
ids = append(ids, rIds...)

return
},
})
if err != nil {
return
}
jobIds = append(jobIds, id)

id, err = jobStore.EnqueueJob(job.Job{
Dir: mcHandle,
Func: func(ctx context.Context) error {
return module.ParseVariables(fs, modStore, mcPath)
},
Type: op.OpTypeParseVariables.String(),
Defer: func(ctx context.Context, jobErr error) (ids job.IDs) {
id, err = jobStore.EnqueueJob(job.Job{
Dir: mcHandle,
Func: func(ctx context.Context) error {
return module.DecodeVarsReferences(ctx, modStore, schemaReader, mcPath)
},
Type: op.OpTypeDecodeVarsReferences.String(),
})
if err != nil {
return
}
ids = append(ids, id)
return
},
})
if err != nil {
return
}
jobIds = append(jobIds, id)
}

return
}
}

func collectReferences(ctx context.Context, dirHandle document.DirHandle, modStore *state.ModuleStore, schemaReader state.SchemaReader) (ids job.IDs) {
jobStore, err := job.JobStoreFromContext(ctx)
if err != nil {
return
}

id, err := jobStore.EnqueueJob(job.Job{
Dir: dirHandle,
Func: func(ctx context.Context) error {
return module.DecodeReferenceTargets(ctx, modStore, schemaReader, dirHandle.Path())
},
Type: op.OpTypeDecodeReferenceTargets.String(),
})
if err != nil {
return
}
ids = append(ids, id)

id, err = jobStore.EnqueueJob(job.Job{
Dir: dirHandle,
Func: func(ctx context.Context) error {
return module.DecodeReferenceOrigins(ctx, modStore, schemaReader, dirHandle.Path())
},
Type: op.OpTypeDecodeReferenceOrigins.String(),
})
if err != nil {
return
}
ids = append(ids, id)

return
}
148 changes: 148 additions & 0 deletions internal/indexer/module_calls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package indexer

import (
"context"
"os"

"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"
)

func (idx *Indexer) decodeInstalledModuleCalls(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.Installed {
fi, err := os.Stat(mc.Path)
if err != nil || !fi.IsDir() {
multierror.Append(errs, err)
continue
}
err = idx.modStore.Add(mc.Path)
if err != nil {
multierror.Append(errs, err)
continue
}

mcHandle := document.DirHandleFromPath(mc.Path)
// copy path for queued jobs below
mcPath := mc.Path

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

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

rIds, err := idx.collectReferences(ctx, mcHandle)
if err != nil {
errs = multierror.Append(errs, err)
} else {
ids = append(ids, rIds...)
}

return ids, errs.ErrorOrNil()
},
})
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, mcPath)
},
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, mcPath)
},
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)

var errs *multierror.Error

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

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

return ids, errs.ErrorOrNil()
}
Loading

0 comments on commit d5602d9

Please sign in to comment.