return partial config from LoadConfig with errors

LoadConfig should return any parsed configuration in order for the
caller to verify `required_version`.
This commit is contained in:
James Bardin 2021-09-28 13:02:26 -04:00
parent 625e768678
commit a53faf43f6
3 changed files with 18 additions and 4 deletions

View File

@ -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)

View File

@ -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))

View File

@ -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")
}
}