LoadModule now always returns the module
We don't need to load the configuration twice, since configload can return the module for us.
This commit is contained in:
parent
a53faf43f6
commit
c2e0d265cf
|
@ -182,24 +182,6 @@ func (c *InitCommand) Run(args []string) int {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Using loadSingleModule will allow us to get the sniffed required_version
|
|
||||||
// before trying to build the complete config.
|
|
||||||
rootMod, _ := c.loadSingleModule(path)
|
|
||||||
// We can ignore the error, since we are going to reload the full config
|
|
||||||
// again below once we know the root module constraints are valid.
|
|
||||||
if rootMod != nil {
|
|
||||||
rootCfg := &configs.Config{
|
|
||||||
Module: rootMod,
|
|
||||||
}
|
|
||||||
// If our module version constraints are not valid, then there is no
|
|
||||||
// need to continue processing.
|
|
||||||
versionDiags := terraform.CheckCoreVersionRequirements(rootCfg)
|
|
||||||
if versionDiags.HasErrors() {
|
|
||||||
c.showDiagnostics(versionDiags)
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// With all of the modules (hopefully) installed, we can now try to load the
|
// With all of the modules (hopefully) installed, we can now try to load the
|
||||||
// whole configuration tree.
|
// whole configuration tree.
|
||||||
config, confDiags := c.loadConfig(path)
|
config, confDiags := c.loadConfig(path)
|
||||||
|
@ -207,9 +189,9 @@ func (c *InitCommand) Run(args []string) int {
|
||||||
// incorrect version of terraform may be producing errors for configuration
|
// incorrect version of terraform may be producing errors for configuration
|
||||||
// constructs added in later versions.
|
// constructs added in later versions.
|
||||||
|
|
||||||
// Check again to make sure none of the modules in the configuration
|
// Before we go further, we'll check to make sure none of the modules in
|
||||||
// declare that they don't support this Terraform version, so we can
|
// the configuration declare that they don't support this Terraform
|
||||||
// produce a version-related error message rather than
|
// version, so we can produce a version-related error message rather than
|
||||||
// potentially-confusing downstream errors.
|
// potentially-confusing downstream errors.
|
||||||
versionDiags := terraform.CheckCoreVersionRequirements(config)
|
versionDiags := terraform.CheckCoreVersionRequirements(config)
|
||||||
if versionDiags.HasErrors() {
|
if versionDiags.HasErrors() {
|
||||||
|
|
|
@ -1611,6 +1611,7 @@ func TestInit_checkRequiredVersion(t *testing.T) {
|
||||||
// Verify that init will error out with an invalid version constraint, even if
|
// Verify that init will error out with an invalid version constraint, even if
|
||||||
// there are other invalid configuration constructs.
|
// there are other invalid configuration constructs.
|
||||||
func TestInit_checkRequiredVersionFirst(t *testing.T) {
|
func TestInit_checkRequiredVersionFirst(t *testing.T) {
|
||||||
|
t.Run("root_module", func(t *testing.T) {
|
||||||
td := t.TempDir()
|
td := t.TempDir()
|
||||||
testCopyDir(t, testFixturePath("init-check-required-version-first"), td)
|
testCopyDir(t, testFixturePath("init-check-required-version-first"), td)
|
||||||
defer testChdir(t, td)()
|
defer testChdir(t, td)()
|
||||||
|
@ -1633,6 +1634,31 @@ func TestInit_checkRequiredVersionFirst(t *testing.T) {
|
||||||
if !strings.Contains(errStr, `Unsupported Terraform Core version`) {
|
if !strings.Contains(errStr, `Unsupported Terraform Core version`) {
|
||||||
t.Fatalf("output should point to unmet version constraint, but is:\n\n%s", errStr)
|
t.Fatalf("output should point to unmet version constraint, but is:\n\n%s", errStr)
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
t.Run("sub_module", func(t *testing.T) {
|
||||||
|
td := t.TempDir()
|
||||||
|
testCopyDir(t, testFixturePath("init-check-required-version-first-module"), td)
|
||||||
|
defer testChdir(t, td)()
|
||||||
|
|
||||||
|
ui := cli.NewMockUi()
|
||||||
|
view, _ := testView(t)
|
||||||
|
c := &InitCommand{
|
||||||
|
Meta: Meta{
|
||||||
|
testingOverrides: metaOverridesForProvider(testProvider()),
|
||||||
|
Ui: ui,
|
||||||
|
View: view,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
args := []string{}
|
||||||
|
if code := c.Run(args); code != 1 {
|
||||||
|
t.Fatalf("got exit status %d; want 1\nstderr:\n%s\n\nstdout:\n%s", code, ui.ErrorWriter.String(), ui.OutputWriter.String())
|
||||||
|
}
|
||||||
|
errStr := ui.ErrorWriter.String()
|
||||||
|
if !strings.Contains(errStr, `Unsupported Terraform Core version`) {
|
||||||
|
t.Fatalf("output should point to unmet version constraint, but is:\n\n%s", errStr)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestInit_providerLockFile(t *testing.T) {
|
func TestInit_providerLockFile(t *testing.T) {
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
module "mod" {
|
||||||
|
source = "./mod"
|
||||||
|
}
|
17
internal/command/testdata/init-check-required-version-first-module/mod/main.tf
vendored
Normal file
17
internal/command/testdata/init-check-required-version-first-module/mod/main.tf
vendored
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
terraform {
|
||||||
|
required_version = ">200.0.0"
|
||||||
|
|
||||||
|
bad {
|
||||||
|
block = "false"
|
||||||
|
}
|
||||||
|
|
||||||
|
required_providers {
|
||||||
|
bang = {
|
||||||
|
oops = "boom"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
nope {
|
||||||
|
boom {}
|
||||||
|
}
|
Loading…
Reference in New Issue