Skip to content

Commit

Permalink
Avoid panic by initing universal schema early (hashicorp#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko authored Nov 18, 2020
1 parent 48dd17f commit 24b91f7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 31 deletions.
29 changes: 8 additions & 21 deletions internal/terraform/rootmodule/root_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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() {
Expand Down
9 changes: 0 additions & 9 deletions internal/terraform/rootmodule/root_module_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion internal/terraform/rootmodule/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 24b91f7

Please sign in to comment.