mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-15 19:22:46 -06:00
86c02d5c35
There are a few constructs from 0.11 and prior that cause 0.12 parsing to fail altogether, which previously created a chicken/egg problem because we need to install the providers in order to run "terraform 0.12upgrade" and thus fix the problem. This changes "terraform init" to use the new "early configuration" loader for module and provider installation. This is built on the more permissive parser in the terraform-config-inspect package, and so it allows us to read out the top-level blocks from the configuration while accepting legacy HCL syntax. In the long run this will let us do version compatibility detection before attempting a "real" config load, giving us better error messages for any future syntax additions, but in the short term the key thing is that it allows us to install the dependencies even if the configuration isn't fully valid. Because backend init still requires full configuration, this introduces a new mode of terraform init where it detects heuristically if it seems like we need to do a configuration upgrade and does a partial init if so, before finally directing the user to run "terraform 0.12upgrade" before running any other commands. The heuristic here is based on two assumptions: - If the "early" loader finds no errors but the normal loader does, the configuration is likely to be valid for Terraform 0.11 but not 0.12. - If there's already a version constraint in the configuration that excludes Terraform versions prior to v0.12 then the configuration is probably _already_ upgraded and so it's just a normal syntax error, even if the early loader didn't detect it. Once the upgrade process is removed in 0.13.0 (users will be required to go stepwise 0.11 -> 0.12 -> 0.13 to upgrade after that), some of this can be simplified to remove that special mode, but the idea of doing the dependency version checks against the liberal parser will remain valuable to increase our chances of reporting version-based incompatibilities rather than syntax errors as we add new features in future.
98 lines
3.6 KiB
Go
98 lines
3.6 KiB
Go
package configload
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
version "github.com/hashicorp/go-version"
|
|
"github.com/hashicorp/hcl2/hcl"
|
|
"github.com/hashicorp/terraform/configs"
|
|
)
|
|
|
|
// LoadConfig reads the Terraform module in the given directory and uses it as the
|
|
// root module to build the static module tree that represents a configuration,
|
|
// assuming that all required descendent modules have already been installed.
|
|
//
|
|
// If error diagnostics are returned, the returned configuration may be either
|
|
// nil or incomplete. In the latter case, cautious static analysis is possible
|
|
// in spite of the errors.
|
|
//
|
|
// LoadConfig performs the basic syntax and uniqueness validations that are
|
|
// required to process the individual modules, and also detects
|
|
func (l *Loader) LoadConfig(rootDir string) (*configs.Config, hcl.Diagnostics) {
|
|
rootMod, diags := l.parser.LoadConfigDir(rootDir)
|
|
if rootMod == nil {
|
|
return nil, diags
|
|
}
|
|
|
|
cfg, cDiags := configs.BuildConfig(rootMod, configs.ModuleWalkerFunc(l.moduleWalkerLoad))
|
|
diags = append(diags, cDiags...)
|
|
|
|
return cfg, diags
|
|
}
|
|
|
|
// moduleWalkerLoad is a configs.ModuleWalkerFunc for loading modules that
|
|
// are presumed to have already been installed. A different function
|
|
// (moduleWalkerInstall) is used for installation.
|
|
func (l *Loader) moduleWalkerLoad(req *configs.ModuleRequest) (*configs.Module, *version.Version, hcl.Diagnostics) {
|
|
// Since we're just loading here, we expect that all referenced modules
|
|
// will be already installed and described in our manifest. However, we
|
|
// do verify that the manifest and the configuration are in agreement
|
|
// so that we can prompt the user to run "terraform init" if not.
|
|
|
|
key := l.modules.manifest.ModuleKey(req.Path)
|
|
record, exists := l.modules.manifest[key]
|
|
|
|
if !exists {
|
|
return nil, nil, hcl.Diagnostics{
|
|
{
|
|
Severity: hcl.DiagError,
|
|
Summary: "Module not installed",
|
|
Detail: "This module is not yet installed. Run \"terraform init\" to install all modules required by this configuration.",
|
|
Subject: &req.CallRange,
|
|
},
|
|
}
|
|
}
|
|
|
|
var diags hcl.Diagnostics
|
|
|
|
// Check for inconsistencies between manifest and config
|
|
if req.SourceAddr != record.SourceAddr {
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
Severity: hcl.DiagError,
|
|
Summary: "Module source has changed",
|
|
Detail: "The source address was changed since this module was installed. Run \"terraform init\" to install all modules required by this configuration.",
|
|
Subject: &req.SourceAddrRange,
|
|
})
|
|
}
|
|
if !req.VersionConstraint.Required.Check(record.Version) {
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
Severity: hcl.DiagError,
|
|
Summary: "Module version requirements have changed",
|
|
Detail: fmt.Sprintf(
|
|
"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.",
|
|
record.Version,
|
|
),
|
|
Subject: &req.SourceAddrRange,
|
|
})
|
|
}
|
|
|
|
mod, mDiags := l.parser.LoadConfigDir(record.Dir)
|
|
diags = append(diags, mDiags...)
|
|
if mod == nil {
|
|
// nil specifically indicates that the directory does not exist or
|
|
// cannot be read, so in this case we'll discard any generic diagnostics
|
|
// returned from LoadConfigDir and produce our own context-sensitive
|
|
// error message.
|
|
return nil, nil, hcl.Diagnostics{
|
|
{
|
|
Severity: hcl.DiagError,
|
|
Summary: "Module not installed",
|
|
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),
|
|
Subject: &req.CallRange,
|
|
},
|
|
}
|
|
}
|
|
|
|
return mod, record.Version, diags
|
|
}
|