@ -167,9 +167,9 @@ func (c *InitCommand) runPssInit(initArgs *arguments.Init, view views.Init) int
// With all of the modules (hopefully) installed, we can now try to load the
// whole configuration tree.
config , confDiags := c . loadConfigWithTests ( path , initArgs . TestsDirectory )
// configDiags will be handled after the version constraint check, since an
// incorrect version of terraform may be producing errors for configuration
// constructs added in later versions.
// configDiags will be handled after :
// - the version constraint check has happened
// - and, the backend/state_store is initialised
// Before we go further, we'll check to make sure none of the modules in
// the configuration declare that they don't support this Terraform
@ -181,6 +181,14 @@ func (c *InitCommand) runPssInit(initArgs *arguments.Init, view views.Init) int
return 1
}
// We've passed the core version check, now we can show errors from the early configuration.
// This prevents trying to initialise the backend with faulty configuration.
if earlyConfDiags . HasErrors ( ) {
diags = diags . Append ( errors . New ( view . PrepareMessage ( views . InitConfigError ) ) , earlyConfDiags )
view . Diagnostics ( diags )
return 1
}
// Now the full configuration is loaded, we can download the providers specified in the configuration.
// This is step one of a two-step provider download process
// Providers may be downloaded by this code, but the dependency lock file is only updated later in `init`
@ -226,6 +234,24 @@ func (c *InitCommand) runPssInit(initArgs *arguments.Init, view views.Init) int
view . Output ( views . EmptyMessage )
}
// Show any errors from initializing the backend.
// No preamble using `InitConfigError` is present, as we expect
// any errors to from configuring the backend itself.
diags = diags . Append ( backDiags )
if backDiags . HasErrors ( ) {
view . Diagnostics ( diags )
return 1
}
// If everything is ok with the core version check and backend/state_store initialization,
// show other errors from loading the full configuration tree.
diags = diags . Append ( confDiags )
if confDiags . HasErrors ( ) {
diags = diags . Append ( errors . New ( view . PrepareMessage ( views . InitConfigError ) ) )
view . Diagnostics ( diags )
return 1
}
var state * states . State
// If we have a functional backend (either just initialized or initialized
@ -288,33 +314,6 @@ func (c *InitCommand) runPssInit(initArgs *arguments.Init, view views.Init) int
view . Output ( views . EmptyMessage )
}
// As Terraform version-related diagnostics are handled above, we can now
// check the diagnostics from the early configuration and the backend.
diags = diags . Append ( earlyConfDiags )
diags = diags . Append ( backDiags )
if earlyConfDiags . HasErrors ( ) {
diags = diags . Append ( errors . New ( view . PrepareMessage ( views . InitConfigError ) ) )
view . Diagnostics ( diags )
return 1
}
// Now, we can show any errors from initializing the backend, but we won't
// show the InitConfigError preamble as we didn't detect problems with
// the early configuration.
if backDiags . HasErrors ( ) {
view . Diagnostics ( diags )
return 1
}
// If everything is ok with the core version check and backend initialization,
// show other errors from loading the full configuration tree.
diags = diags . Append ( confDiags )
if confDiags . HasErrors ( ) {
diags = diags . Append ( errors . New ( view . PrepareMessage ( views . InitConfigError ) ) )
view . Diagnostics ( diags )
return 1
}
if cb , ok := back . ( * cloud . Cloud ) ; ok {
if c . RunningInAutomation {
if err := cb . AssertImportCompatible ( config ) ; err != nil {
@ -522,7 +521,6 @@ However, if you intended to override a defined backend, please verify that
the backend configuration is present and valid .
` ,
) )
}
opts = & BackendOpts {