Skip to content

Commit

Permalink
Make assignments to undeclared variables an error (#1941)
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 committed Dec 23, 2023
1 parent 628d369 commit 3274417
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
14 changes: 13 additions & 1 deletion terraform/input_value.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package terraform

import (
Expand Down Expand Up @@ -50,6 +53,7 @@ func DefaultVariableValues(configs map[string]*Variable) InputValues {

// EnvironmentVariableValues looks up `TF_VAR_*` env variables and returns InputValues.
// Declared variables are required because the parsing mode of the variable value is type-dependent.
// However, in the case of environment variables, no error is returned even if the variable is not declared.
func EnvironmentVariableValues(declVars map[string]*Variable) (InputValues, hcl.Diagnostics) {
envVariables := make(InputValues)
var diags hcl.Diagnostics
Expand Down Expand Up @@ -88,6 +92,7 @@ func EnvironmentVariableValues(declVars map[string]*Variable) (InputValues, hcl.

// ParseVariableValues parses the variable values passed as CLI flags and returns InputValues.
// Declared variables are required because the parsing mode of the variable value is type-dependent.
// Return an error if the variable is not declared.
func ParseVariableValues(vars []string, declVars map[string]*Variable) (InputValues, hcl.Diagnostics) {
variables := make(InputValues)
var diags hcl.Diagnostics
Expand All @@ -97,6 +102,7 @@ func ParseVariableValues(vars []string, declVars map[string]*Variable) (InputVal
if idx == -1 {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Subject: &hcl.Range{Filename: "<input-value>", Start: hcl.InitialPos, End: hcl.InitialPos},
Summary: "invalid variable value format",
Detail: fmt.Sprintf(`"%s" is invalid. Variables must be "key=value" format`, raw),
})
Expand All @@ -110,7 +116,13 @@ func ParseVariableValues(vars []string, declVars map[string]*Variable) (InputVal
if declared {
mode = declVar.ParsingMode
} else {
mode = VariableParseLiteral
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Subject: &hcl.Range{Filename: fmt.Sprintf("<value for var.%s>", name), Start: hcl.InitialPos, End: hcl.InitialPos},
Summary: "Value for undeclared variable",
Detail: fmt.Sprintf("A variable named %q was assigned, but the root module does not declare a variable of that name.", name),
})
continue
}

val, parseDiags := mode.Parse(name, rawVal)
Expand Down
18 changes: 4 additions & 14 deletions terraform/input_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,21 +172,11 @@ func TestParseVariableValues(t *testing.T) {
declared: map[string]*Variable{},
vars: []string{
"foo=bar",
"bar=[\"foo\"]",
"baz={ foo=\"bar\" }",
},
want: InputValues{
"foo": &InputValue{
Value: cty.StringVal("bar"),
},
"bar": &InputValue{
Value: cty.StringVal("[\"foo\"]"),
},
"baz": &InputValue{
Value: cty.StringVal("{ foo=\"bar\" }"),
},
want: InputValues{},
errCheck: func(diags hcl.Diagnostics) bool {
return diags.Error() != `<value for var.foo>:1,1-1: Value for undeclared variable; A variable named "foo" was assigned, but the root module does not declare a variable of that name.`
},
errCheck: neverHappend,
},
{
name: "declared",
Expand Down Expand Up @@ -219,7 +209,7 @@ func TestParseVariableValues(t *testing.T) {
vars: []string{"foo"},
want: InputValues{},
errCheck: func(diags hcl.Diagnostics) bool {
return diags.Error() != `<nil>: invalid variable value format; "foo" is invalid. Variables must be "key=value" format`
return diags.Error() != `<input-value>:1,1-1: invalid variable value format; "foo" is invalid. Variables must be "key=value" format`
},
},
{
Expand Down

0 comments on commit 3274417

Please sign in to comment.