diff --git a/internal/langserver/handlers/did_open.go b/internal/langserver/handlers/did_open.go index f2d73b718..cac46ac6b 100644 --- a/internal/langserver/handlers/did_open.go +++ b/internal/langserver/handlers/did_open.go @@ -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) diff --git a/internal/terraform/module/module_loader.go b/internal/terraform/module/module_loader.go index b29009e55..192dca35d 100644 --- a/internal/terraform/module/module_loader.go +++ b/internal/terraform/module/module_loader.go @@ -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 { diff --git a/internal/terraform/module/module_manager.go b/internal/terraform/module/module_manager.go index 5da5d6d00..e88544868 100644 --- a/internal/terraform/module/module_manager.go +++ b/internal/terraform/module/module_manager.go @@ -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() diff --git a/internal/terraform/module/module_ops.go b/internal/terraform/module/module_ops.go index 146b277a0..0209e67f6 100644 --- a/internal/terraform/module/module_ops.go +++ b/internal/terraform/module/module_ops.go @@ -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{} } diff --git a/internal/terraform/module/types.go b/internal/terraform/module/types.go index 20e675382..e82c7a992 100644 --- a/internal/terraform/module/types.go +++ b/internal/terraform/module/types.go @@ -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() } diff --git a/internal/terraform/module/walker.go b/internal/terraform/module/walker.go index 3def6072d..c52a1572a 100644 --- a/internal/terraform/module/walker.go +++ b/internal/terraform/module/walker.go @@ -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 } diff --git a/internal/terraform/module/watcher.go b/internal/terraform/module/watcher.go index 401cb31b6..8c26cfef1 100644 --- a/internal/terraform/module/watcher.go +++ b/internal/terraform/module/watcher.go @@ -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 } } @@ -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 @@ -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 } }