github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/configs/configload/loader_load.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package configload 5 6 import ( 7 "fmt" 8 9 version "github.com/hashicorp/go-version" 10 "github.com/hashicorp/hcl/v2" 11 12 "github.com/terramate-io/tf/configs" 13 ) 14 15 // LoadConfig reads the Terraform module in the given directory and uses it as the 16 // root module to build the static module tree that represents a configuration, 17 // assuming that all required descendent modules have already been installed. 18 // 19 // If error diagnostics are returned, the returned configuration may be either 20 // nil or incomplete. In the latter case, cautious static analysis is possible 21 // in spite of the errors. 22 // 23 // LoadConfig performs the basic syntax and uniqueness validations that are 24 // required to process the individual modules 25 func (l *Loader) LoadConfig(rootDir string) (*configs.Config, hcl.Diagnostics) { 26 return l.loadConfig(l.parser.LoadConfigDir(rootDir)) 27 } 28 29 // LoadConfigWithTests matches LoadConfig, except the configs.Config contains 30 // any relevant .tftest.hcl files. 31 func (l *Loader) LoadConfigWithTests(rootDir string, testDir string) (*configs.Config, hcl.Diagnostics) { 32 return l.loadConfig(l.parser.LoadConfigDirWithTests(rootDir, testDir)) 33 } 34 35 func (l *Loader) loadConfig(rootMod *configs.Module, diags hcl.Diagnostics) (*configs.Config, hcl.Diagnostics) { 36 if rootMod == nil || diags.HasErrors() { 37 // Ensure we return any parsed modules here so that required_version 38 // constraints can be verified even when encountering errors. 39 cfg := &configs.Config{ 40 Module: rootMod, 41 } 42 43 return cfg, diags 44 } 45 46 cfg, cDiags := configs.BuildConfig(rootMod, configs.ModuleWalkerFunc(l.moduleWalkerLoad)) 47 diags = append(diags, cDiags...) 48 49 return cfg, diags 50 } 51 52 // moduleWalkerLoad is a configs.ModuleWalkerFunc for loading modules that 53 // are presumed to have already been installed. 54 func (l *Loader) moduleWalkerLoad(req *configs.ModuleRequest) (*configs.Module, *version.Version, hcl.Diagnostics) { 55 // Since we're just loading here, we expect that all referenced modules 56 // will be already installed and described in our manifest. However, we 57 // do verify that the manifest and the configuration are in agreement 58 // so that we can prompt the user to run "terraform init" if not. 59 60 key := l.modules.manifest.ModuleKey(req.Path) 61 record, exists := l.modules.manifest[key] 62 63 if !exists { 64 return nil, nil, hcl.Diagnostics{ 65 { 66 Severity: hcl.DiagError, 67 Summary: "Module not installed", 68 Detail: "This module is not yet installed. Run \"terraform init\" to install all modules required by this configuration.", 69 Subject: &req.CallRange, 70 }, 71 } 72 } 73 74 var diags hcl.Diagnostics 75 76 // Check for inconsistencies between manifest and config. 77 78 // We ignore a nil SourceAddr here, which represents a failure during 79 // configuration parsing, and will be reported in a diagnostic elsewhere. 80 if req.SourceAddr != nil && req.SourceAddr.String() != record.SourceAddr { 81 diags = append(diags, &hcl.Diagnostic{ 82 Severity: hcl.DiagError, 83 Summary: "Module source has changed", 84 Detail: "The source address was changed since this module was installed. Run \"terraform init\" to install all modules required by this configuration.", 85 Subject: &req.SourceAddrRange, 86 }) 87 } 88 if len(req.VersionConstraint.Required) > 0 && record.Version == nil { 89 diags = append(diags, &hcl.Diagnostic{ 90 Severity: hcl.DiagError, 91 Summary: "Module version requirements have changed", 92 Detail: "The version requirements have changed since this module was installed and the installed version is no longer acceptable. Run \"terraform init\" to install all modules required by this configuration.", 93 Subject: &req.SourceAddrRange, 94 }) 95 } 96 if record.Version != nil && !req.VersionConstraint.Required.Check(record.Version) { 97 diags = append(diags, &hcl.Diagnostic{ 98 Severity: hcl.DiagError, 99 Summary: "Module version requirements have changed", 100 Detail: fmt.Sprintf( 101 "The version requirements have changed since this module was installed and the installed version (%s) is no longer acceptable. Run \"terraform init\" to install all modules required by this configuration.", 102 record.Version, 103 ), 104 Subject: &req.SourceAddrRange, 105 }) 106 } 107 108 mod, mDiags := l.parser.LoadConfigDir(record.Dir) 109 diags = append(diags, mDiags...) 110 if mod == nil { 111 // nil specifically indicates that the directory does not exist or 112 // cannot be read, so in this case we'll discard any generic diagnostics 113 // returned from LoadConfigDir and produce our own context-sensitive 114 // error message. 115 return nil, nil, hcl.Diagnostics{ 116 { 117 Severity: hcl.DiagError, 118 Summary: "Module not installed", 119 Detail: fmt.Sprintf("This module's local cache directory %s could not be read. Run \"terraform init\" to install all modules required by this configuration.", record.Dir), 120 Subject: &req.CallRange, 121 }, 122 } 123 } 124 125 return mod, record.Version, diags 126 }