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

Introduce Defer to module loader #590

Merged
merged 1 commit into from
Jul 22, 2021
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
2 changes: 1 addition & 1 deletion internal/langserver/handlers/did_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (lh *logHandler) TextDocumentDidOpen(ctx context.Context, params lsp.DidOpe
modMgr.EnqueueModuleOpWait(mod.Path, op.OpTypeDecodeReferenceOrigins)

if mod.TerraformVersionState == op.OpStateUnknown {
modMgr.EnqueueModuleOp(mod.Path, op.OpTypeGetTerraformVersion)
modMgr.EnqueueModuleOp(mod.Path, op.OpTypeGetTerraformVersion, nil)
}

watcher, err := lsctx.Watcher(ctx)
Expand Down
65 changes: 32 additions & 33 deletions internal/terraform/module/module_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,59 +150,58 @@ func (ml *moduleLoader) executeModuleOp(ctx context.Context, modOp ModuleOperati
defer ml.logger.Printf("finished %q for %s", modOp.Type, modOp.ModulePath)
defer modOp.markAsDone()

var opErr error

switch modOp.Type {
case op.OpTypeGetTerraformVersion:
err := GetTerraformVersion(ctx, ml.modStore, modOp.ModulePath)
if err != nil {
ml.logger.Printf("failed to get terraform version: %s", err)
opErr = GetTerraformVersion(ctx, ml.modStore, modOp.ModulePath)
if opErr != nil {
ml.logger.Printf("failed to get terraform version: %s", opErr)
}
return
case op.OpTypeObtainSchema:
err := ObtainSchema(ctx, ml.modStore, ml.schemaStore, modOp.ModulePath)
if err != nil {
ml.logger.Printf("failed to obtain schema: %s", err)
opErr = ObtainSchema(ctx, ml.modStore, ml.schemaStore, modOp.ModulePath)
if opErr != nil {
ml.logger.Printf("failed to obtain schema: %s", opErr)
}
return
case op.OpTypeParseModuleConfiguration:
err := ParseModuleConfiguration(ml.fs, ml.modStore, modOp.ModulePath)
if err != nil {
ml.logger.Printf("failed to parse module configuration: %s", err)
opErr = ParseModuleConfiguration(ml.fs, ml.modStore, modOp.ModulePath)
if opErr != nil {
ml.logger.Printf("failed to parse module configuration: %s", opErr)
}
return
case op.OpTypeParseVariables:
err := ParseVariables(ml.fs, ml.modStore, modOp.ModulePath)
if err != nil {
ml.logger.Printf("failed to parse variables: %s", err)
opErr = ParseVariables(ml.fs, ml.modStore, modOp.ModulePath)
if opErr != nil {
ml.logger.Printf("failed to parse variables: %s", opErr)
}
return
case op.OpTypeParseModuleManifest:
err := ParseModuleManifest(ml.fs, ml.modStore, modOp.ModulePath)
if err != nil {
ml.logger.Printf("failed to parse module manifest: %s", err)
opErr = ParseModuleManifest(ml.fs, ml.modStore, modOp.ModulePath)
if opErr != nil {
ml.logger.Printf("failed to parse module manifest: %s", opErr)
}
return
case op.OpTypeLoadModuleMetadata:
err := LoadModuleMetadata(ml.modStore, modOp.ModulePath)
if err != nil {
ml.logger.Printf("failed to load module metadata: %s", err)
opErr = LoadModuleMetadata(ml.modStore, modOp.ModulePath)
if opErr != nil {
ml.logger.Printf("failed to load module metadata: %s", opErr)
}
return
case op.OpTypeDecodeReferenceTargets:
err := DecodeReferenceTargets(ml.modStore, ml.schemaStore, modOp.ModulePath)
if err != nil {
ml.logger.Printf("failed to decode reference targets: %s", err)
opErr = DecodeReferenceTargets(ml.modStore, ml.schemaStore, modOp.ModulePath)
if opErr != nil {
ml.logger.Printf("failed to decode reference targets: %s", opErr)
}
return
case op.OpTypeDecodeReferenceOrigins:
err := DecodeReferenceOrigins(ml.modStore, ml.schemaStore, modOp.ModulePath)
if err != nil {
ml.logger.Printf("failed to decode reference origins: %s", err)
opErr = DecodeReferenceOrigins(ml.modStore, ml.schemaStore, modOp.ModulePath)
if opErr != nil {
ml.logger.Printf("failed to decode reference origins: %s", opErr)
}
default:
ml.logger.Printf("%s: unknown operation (%#v) for module operation",
modOp.ModulePath, modOp.Type)
return
}

ml.logger.Printf("%s: unknown operation (%#v) for module operation",
modOp.ModulePath, modOp.Type)
if modOp.Defer != nil {
modOp.Defer(opErr)
}
}

func (ml *moduleLoader) EnqueueModuleOp(modOp ModuleOperation) error {
Expand Down
3 changes: 2 additions & 1 deletion internal/terraform/module/module_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ func (mm *moduleManager) EnqueueModuleOpWait(modPath string, opType op.OpType) e
return nil
}

func (mm *moduleManager) EnqueueModuleOp(modPath string, opType op.OpType) error {
func (mm *moduleManager) EnqueueModuleOp(modPath string, opType op.OpType, deferFunc DeferFunc) error {
modOp := NewModuleOperation(modPath, opType)
modOp.Defer = deferFunc
mm.loader.EnqueueModuleOp(modOp)
if mm.syncLoading {
<-modOp.Done()
Expand Down
3 changes: 3 additions & 0 deletions internal/terraform/module/module_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ import (
tfschema "github.com/hashicorp/terraform-schema/schema"
)

type DeferFunc func(opError error)

type ModuleOperation struct {
ModulePath string
Type op.OpType
Defer DeferFunc

doneCh chan struct{}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/terraform/module/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type ModuleManager interface {
SetLogger(logger *log.Logger)
AddModule(modPath string) (Module, error)
RemoveModule(modPath string) error
EnqueueModuleOp(modPath string, opType op.OpType) error
EnqueueModuleOp(modPath string, opType op.OpType, deferFunc DeferFunc) error
EnqueueModuleOpWait(modPath string, opType op.OpType) error
CancelLoading()
}
Expand Down
6 changes: 3 additions & 3 deletions internal/terraform/module/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,20 +239,20 @@ func (w *Walker) walk(ctx context.Context, rootPath string) error {
}
}

err = w.modMgr.EnqueueModuleOp(dir, op.OpTypeGetTerraformVersion)
err = w.modMgr.EnqueueModuleOp(dir, op.OpTypeGetTerraformVersion, nil)
if err != nil {
return err
}

dataDir := datadir.WalkDataDirOfModule(w.fs, dir)
if dataDir.ModuleManifestPath != "" {
err = w.modMgr.EnqueueModuleOp(dir, op.OpTypeParseModuleManifest)
err = w.modMgr.EnqueueModuleOp(dir, op.OpTypeParseModuleManifest, nil)
if err != nil {
return err
}
}
if dataDir.PluginLockFilePath != "" {
err = w.modMgr.EnqueueModuleOp(dir, op.OpTypeObtainSchema)
err = w.modMgr.EnqueueModuleOp(dir, op.OpTypeObtainSchema, nil)
if err != nil {
return err
}
Expand Down
18 changes: 9 additions & 9 deletions internal/terraform/module/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ func (w *watcher) processEvent(event fsnotify.Event) {
if event.Op&fsnotify.Write == fsnotify.Write {
for _, mod := range w.modules {
if containsPath(mod.Watchable.ModuleManifests, eventPath) {
w.modMgr.EnqueueModuleOp(mod.Path, op.OpTypeParseModuleManifest)
w.modMgr.EnqueueModuleOp(mod.Path, op.OpTypeParseModuleManifest, nil)
return
}
if containsPath(mod.Watchable.PluginLockFiles, eventPath) {
w.modMgr.EnqueueModuleOp(mod.Path, op.OpTypeObtainSchema)
w.modMgr.EnqueueModuleOp(mod.Path, op.OpTypeGetTerraformVersion)
w.modMgr.EnqueueModuleOp(mod.Path, op.OpTypeObtainSchema, nil)
w.modMgr.EnqueueModuleOp(mod.Path, op.OpTypeGetTerraformVersion, nil)
return
}
}
Expand All @@ -175,11 +175,11 @@ func (w *watcher) processEvent(event fsnotify.Event) {
return nil
}
if containsPath(mod.Watchable.ModuleManifests, path) {
return w.modMgr.EnqueueModuleOp(mod.Path, op.OpTypeParseModuleManifest)
return w.modMgr.EnqueueModuleOp(mod.Path, op.OpTypeParseModuleManifest, nil)
}
if containsPath(mod.Watchable.PluginLockFiles, path) {
w.modMgr.EnqueueModuleOp(mod.Path, op.OpTypeObtainSchema)
w.modMgr.EnqueueModuleOp(mod.Path, op.OpTypeGetTerraformVersion)
w.modMgr.EnqueueModuleOp(mod.Path, op.OpTypeObtainSchema, nil)
w.modMgr.EnqueueModuleOp(mod.Path, op.OpTypeGetTerraformVersion, nil)
return nil
}
return nil
Expand All @@ -189,13 +189,13 @@ func (w *watcher) processEvent(event fsnotify.Event) {
}

if containsPath(mod.Watchable.ModuleManifests, eventPath) {
w.modMgr.EnqueueModuleOp(mod.Path, op.OpTypeParseModuleManifest)
w.modMgr.EnqueueModuleOp(mod.Path, op.OpTypeParseModuleManifest, nil)
return
}

if containsPath(mod.Watchable.PluginLockFiles, eventPath) {
w.modMgr.EnqueueModuleOp(mod.Path, op.OpTypeObtainSchema)
w.modMgr.EnqueueModuleOp(mod.Path, op.OpTypeGetTerraformVersion)
w.modMgr.EnqueueModuleOp(mod.Path, op.OpTypeObtainSchema, nil)
w.modMgr.EnqueueModuleOp(mod.Path, op.OpTypeGetTerraformVersion, nil)
return
}
}
Expand Down