From 24b91f79d0c20375651af6a9106188cae7073882 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Wed, 18 Nov 2020 16:00:46 +0000 Subject: [PATCH] Avoid panic by initing universal schema early (#307) --- internal/terraform/rootmodule/root_module.go | 29 +++++-------------- .../rootmodule/root_module_manager.go | 9 ------ internal/terraform/rootmodule/types.go | 1 - 3 files changed, 8 insertions(+), 31 deletions(-) diff --git a/internal/terraform/rootmodule/root_module.go b/internal/terraform/rootmodule/root_module.go index c66f3144f..f43a1b063 100644 --- a/internal/terraform/rootmodule/root_module.go +++ b/internal/terraform/rootmodule/root_module.go @@ -64,9 +64,8 @@ type rootModule struct { tfVersionErr error // core schema - coreSchemaLoaded bool - coreSchema *schema.BodySchema - coreSchemaMu *sync.RWMutex + coreSchema *schema.BodySchema + coreSchemaMu *sync.RWMutex // decoder isParsed bool @@ -88,6 +87,7 @@ func newRootModule(fs filesystem.Filesystem, dir string) *rootModule { pluginMu: &sync.RWMutex{}, providerSchemaMu: &sync.RWMutex{}, tfLoadingMu: &sync.RWMutex{}, + coreSchema: tfschema.UniversalCoreModuleSchema(), coreSchemaMu: &sync.RWMutex{}, isParsedMu: &sync.RWMutex{}, pFilesMap: make(map[string]*hcl.File, 0), @@ -327,8 +327,9 @@ func (rm *rootModule) findAndSetCoreSchema() error { return err } + rm.coreSchemaMu.Lock() rm.coreSchema = coreSchema - rm.setCoreSchemaLoaded(true) + rm.coreSchemaMu.Unlock() return nil } @@ -398,18 +399,6 @@ func (rm *rootModule) Decoder() (*decoder.Decoder, error) { return d, nil } -func (rm *rootModule) IsCoreSchemaLoaded() bool { - rm.coreSchemaMu.RLock() - defer rm.coreSchemaMu.RUnlock() - return rm.coreSchemaLoaded -} - -func (rm *rootModule) setCoreSchemaLoaded(isLoaded bool) { - rm.coreSchemaMu.Lock() - defer rm.coreSchemaMu.Unlock() - rm.coreSchemaLoaded = isLoaded -} - func (rm *rootModule) IsProviderSchemaLoaded() bool { rm.providerSchemaMu.RLock() defer rm.providerSchemaMu.RUnlock() @@ -489,11 +478,9 @@ func (rm *rootModule) parsedFiles() map[string]*hcl.File { } func (rm *rootModule) MergedSchema() (*schema.BodySchema, error) { - var mergedSchema *schema.BodySchema - - if rm.IsCoreSchemaLoaded() { - mergedSchema = rm.coreSchema - } + rm.coreSchemaMu.RLock() + defer rm.coreSchemaMu.RUnlock() + mergedSchema := rm.coreSchema if rm.IsProviderSchemaLoaded() { if !rm.IsParsed() { diff --git a/internal/terraform/rootmodule/root_module_manager.go b/internal/terraform/rootmodule/root_module_manager.go index 7f6ea68f2..63e64615c 100644 --- a/internal/terraform/rootmodule/root_module_manager.go +++ b/internal/terraform/rootmodule/root_module_manager.go @@ -219,15 +219,6 @@ func (rmm *rootModuleManager) RootModuleByPath(path string) (RootModule, error) return nil, &RootModuleNotFoundErr{path} } -func (rmm *rootModuleManager) IsCoreSchemaLoaded(path string) (bool, error) { - rm, err := rmm.RootModuleByPath(path) - if err != nil { - return false, err - } - - return rm.IsCoreSchemaLoaded(), nil -} - func (rmm *rootModuleManager) IsProviderSchemaLoaded(path string) (bool, error) { rm, err := rmm.RootModuleByPath(path) if err != nil { diff --git a/internal/terraform/rootmodule/types.go b/internal/terraform/rootmodule/types.go index fb7bc4b33..14b70f19a 100644 --- a/internal/terraform/rootmodule/types.go +++ b/internal/terraform/rootmodule/types.go @@ -77,7 +77,6 @@ type RootModule interface { IsParsed() bool ParseFiles() error ParsedDiagnostics() map[string]hcl.Diagnostics - IsCoreSchemaLoaded() bool TerraformFormatter() (exec.Formatter, error) HasTerraformDiscoveryFinished() bool IsTerraformAvailable() bool