diff --git a/internal/command/init.go b/internal/command/init.go index 02f555ee0..ec6fa5723 100644 --- a/internal/command/init.go +++ b/internal/command/init.go @@ -227,7 +227,7 @@ func (c *InitCommand) Run(args []string) int { var back backend.Backend if flagBackend { - be, backendOutput, backendDiags := c.initBackend(config.Root.Module, flagConfigExtra) + be, backendOutput, backendDiags := c.initBackend(config.Module, flagConfigExtra) diags = diags.Append(backendDiags) if backendDiags.HasErrors() { c.showDiagnostics(diags) diff --git a/internal/configs/configload/loader_load.go b/internal/configs/configload/loader_load.go index 323001de1..9ae440274 100644 --- a/internal/configs/configload/loader_load.go +++ b/internal/configs/configload/loader_load.go @@ -21,7 +21,13 @@ import ( func (l *Loader) LoadConfig(rootDir string) (*configs.Config, hcl.Diagnostics) { rootMod, diags := l.parser.LoadConfigDir(rootDir) if rootMod == nil || diags.HasErrors() { - return nil, diags + // Ensure we return any parsed modules here so that required_version + // constraints can be verified even when encountering errors. + cfg := &configs.Config{ + Module: rootMod, + } + + return cfg, diags } cfg, cDiags := configs.BuildConfig(rootMod, configs.ModuleWalkerFunc(l.moduleWalkerLoad)) diff --git a/internal/configs/configload/loader_load_test.go b/internal/configs/configload/loader_load_test.go index 82d8db0cd..ab8dd5dee 100644 --- a/internal/configs/configload/loader_load_test.go +++ b/internal/configs/configload/loader_load_test.go @@ -91,8 +91,16 @@ func TestLoaderLoadConfig_loadDiags(t *testing.T) { t.Fatalf("unexpected error from NewLoader: %s", err) } - _, diags := loader.LoadConfig(fixtureDir) + cfg, diags := loader.LoadConfig(fixtureDir) if !diags.HasErrors() { - t.Fatalf("success; want error") + t.Fatal("success; want error") + } + + if cfg == nil { + t.Fatal("partial config not returned with diagnostics") + } + + if cfg.Module == nil { + t.Fatal("expected config module") } }