diff --git a/internal/command/meta_vars.go b/internal/command/meta_vars.go index 9f3ea336e37d..f082daa0c776 100644 --- a/internal/command/meta_vars.go +++ b/internal/command/meta_vars.go @@ -102,6 +102,14 @@ func (m *Meta) collectVariableValues() (map[string]backend.UnparsedVariableValue } name := raw[:eq] rawVal := raw[eq+1:] + if strings.HasSuffix(name, " ") { + diags = diags.Append(tfdiags.Sourceless( + tfdiags.Error, + "Invalid -var option", + fmt.Sprintf("Variable name %q is invalid due to trailing space. Did you mean -var=\"%s=%s\"?", name, strings.TrimSuffix(name, " "), strings.TrimPrefix(rawVal, " ")), + )) + continue + } ret[name] = unparsedVariableValueString{ str: rawVal, name: name, diff --git a/internal/command/plan_test.go b/internal/command/plan_test.go index b0709b92b2e8..555771aba360 100644 --- a/internal/command/plan_test.go +++ b/internal/command/plan_test.go @@ -575,6 +575,51 @@ func TestPlan_vars(t *testing.T) { } } +func TestPlan_varsInvalid(t *testing.T) { + testCases := []struct { + args []string + wantErr string + }{ + { + []string{"-var", "foo"}, + `The given -var option "foo" is not correctly specified.`, + }, + { + []string{"-var", "foo = bar"}, + `Variable name "foo " is invalid due to trailing space.`, + }, + } + + // Create a temporary working directory that is empty + td := t.TempDir() + testCopyDir(t, testFixturePath("plan-vars"), td) + defer testChdir(t, td)() + + for _, tc := range testCases { + t.Run(strings.Join(tc.args, " "), func(t *testing.T) { + p := planVarsFixtureProvider() + view, done := testView(t) + c := &PlanCommand{ + Meta: Meta{ + testingOverrides: metaOverridesForProvider(p), + View: view, + }, + } + + code := c.Run(tc.args) + output := done(t) + if code != 1 { + t.Fatalf("bad: %d\n\n%s", code, output.Stdout()) + } + + got := output.Stderr() + if !strings.Contains(got, tc.wantErr) { + t.Fatalf("bad error output, want %q, got:\n%s", tc.wantErr, got) + } + }) + } +} + func TestPlan_varsUnset(t *testing.T) { // Create a temporary working directory that is empty td := t.TempDir()