diff --git a/command/init.go b/command/init.go index d1583d1ef..f328509b5 100644 --- a/command/init.go +++ b/command/init.go @@ -866,10 +866,13 @@ func (c *InitCommand) backendConfigOverrideBody(flags rawFlags, schema *configsc } // Generate an HCL body schema for the backend block. var bodySchema hcl.BodySchema - for name, attr := range schema.Attributes { + for name := range schema.Attributes { + // We intentionally ignore the `Required` attribute here + // because backend config override files can be partial. The + // goal is to make sure we're not loading a file with + // extraneous attributes or blocks. bodySchema.Attributes = append(bodySchema.Attributes, hcl.AttributeSchema{ - Name: name, - Required: attr.Required, + Name: name, }) } for name, block := range schema.BlockTypes { diff --git a/command/init_test.go b/command/init_test.go index afdf82f09..0b5d7bdb8 100644 --- a/command/init_test.go +++ b/command/init_test.go @@ -18,6 +18,7 @@ import ( "github.com/hashicorp/terraform/addrs" "github.com/hashicorp/terraform/configs" + "github.com/hashicorp/terraform/configs/configschema" "github.com/hashicorp/terraform/helper/copy" "github.com/hashicorp/terraform/internal/getproviders" "github.com/hashicorp/terraform/internal/providercache" @@ -429,6 +430,30 @@ func TestInit_backendConfigFile(t *testing.T) { t.Errorf("wrong config\ngot: %s\nwant: %s", got, want) } }) + + // simulate the local backend having a required field which is not + // specified in the override file + t.Run("required-argument", func(t *testing.T) { + c := &InitCommand{} + schema := &configschema.Block{ + Attributes: map[string]*configschema.Attribute{ + "path": { + Type: cty.String, + Optional: true, + }, + "workspace_dir": { + Type: cty.String, + Required: true, + }, + }, + } + flagConfigExtra := newRawFlags("-backend-config") + flagConfigExtra.Set("input.config") + _, diags := c.backendConfigOverrideBody(flagConfigExtra, schema) + if len(diags) != 0 { + t.Errorf("expected no diags, got: %s", diags.Err()) + } + }) } func TestInit_backendConfigFilePowershellConfusion(t *testing.T) {