diff --git a/hcl2template/common_test.go b/hcl2template/common_test.go index c3b17b817..fe2a8afc7 100644 --- a/hcl2template/common_test.go +++ b/hcl2template/common_test.go @@ -60,7 +60,7 @@ func testParse(t *testing.T, tests []parseTest) { t.Run(tt.name, func(t *testing.T) { gotCfg, gotDiags := tt.parser.parse(tt.args.filename, tt.args.vars) if tt.parseWantDiags == (gotDiags == nil) { - t.Fatalf("Parser.parse() unexpected diagnostics. %s", gotDiags) + t.Fatalf("Parser.parse() unexpected %q diagnostics.", gotDiags) } if tt.parseWantDiagHasErrors != gotDiags.HasErrors() { t.Fatalf("Parser.parse() unexpected diagnostics HasErrors. %s", gotDiags) @@ -97,6 +97,7 @@ func testParse(t *testing.T, tests []parseTest) { packer.CoreBuild{}, packer.CoreBuildProvisioner{}, packer.CoreBuildPostProcessor{}, + null.Builder{}, ), ); diff != "" { t.Fatalf("Parser.getBuilds() wrong packer builds. %s", diff) diff --git a/hcl2template/parser.go b/hcl2template/parser.go index cf7af9fd4..071bf6206 100644 --- a/hcl2template/parser.go +++ b/hcl2template/parser.go @@ -101,10 +101,10 @@ func (p *Parser) parse(filename string, vars map[string]string) (*PackerConfig, } } - _, moreDiags := cfg.InputVariables.Values() - diags = append(diags, moreDiags...) - _, moreDiags = cfg.LocalVariables.Values() - diags = append(diags, moreDiags...) + // _, moreDiags := cfg.InputVariables.Values() + // diags = append(diags, moreDiags...) + // _, moreDiags = cfg.LocalVariables.Values() + // diags = append(diags, moreDiags...) // parse var files { diff --git a/hcl2template/testdata/variables/unset_unused_string_variable.pkr.hcl b/hcl2template/testdata/variables/unset_unused_string_variable.pkr.hcl new file mode 100644 index 000000000..1853a09d2 --- /dev/null +++ b/hcl2template/testdata/variables/unset_unused_string_variable.pkr.hcl @@ -0,0 +1,15 @@ + +variable "foo" { + type = string +} + + +build { + sources = [ + "source.null.null-builder", + ] +} + +source "null" "null-builder" { + communicator = "none" +} diff --git a/hcl2template/testdata/variables/unset_string_variable.pkr.hcl b/hcl2template/testdata/variables/unset_used_string_variable.pkr.hcl similarity index 100% rename from hcl2template/testdata/variables/unset_string_variable.pkr.hcl rename to hcl2template/testdata/variables/unset_used_string_variable.pkr.hcl diff --git a/hcl2template/types.variables.go b/hcl2template/types.variables.go index 29112cda7..29e4d51d6 100644 --- a/hcl2template/types.variables.go +++ b/hcl2template/types.variables.go @@ -59,7 +59,12 @@ func (v *Variable) Value() (cty.Value, *hcl.Diagnostic) { } } - return cty.UnknownVal(v.Type), &hcl.Diagnostic{ + value := cty.NullVal(cty.DynamicPseudoType) + if v.Type != cty.NilType { + cty.NullVal(v.Type) + } + + return value, &hcl.Diagnostic{ Severity: hcl.DiagError, Summary: fmt.Sprintf("Unset variable %q", v.Name), Detail: "A used variable must be set or have a default value; see " + @@ -248,6 +253,7 @@ func (variables Variables) collectVariableValues(env []string, files []*hcl.File if moreDiags.HasErrors() { continue } + val, valDiags := expr.Value(nil) diags = append(diags, valDiags...) diff --git a/hcl2template/types.variables_test.go b/hcl2template/types.variables_test.go index 687f8f5a4..8c3894927 100644 --- a/hcl2template/types.variables_test.go +++ b/hcl2template/types.variables_test.go @@ -10,6 +10,7 @@ import ( "github.com/zclconf/go-cty/cty/convert" "github.com/hashicorp/hcl/v2" + "github.com/hashicorp/packer/builder/null" "github.com/hashicorp/packer/packer" ) @@ -39,8 +40,8 @@ func TestParse_variables(t *testing.T) { }, }, LocalVariables: Variables{ - "owner": &Variable{}, - "service_name": &Variable{}, + "owner": &Variable{Name: "owner"}, + "service_name": &Variable{Name: "service_name"}, }, }, false, false, @@ -103,13 +104,13 @@ func TestParse_variables(t *testing.T) { }, }, }, - true, true, + true, false, []packer.Build{}, false, }, - {"unset variable", + {"unset used variable", defaultParser, - parseTestArgs{"testdata/variables/unset_string_variable.pkr.hcl", nil}, + parseTestArgs{"testdata/variables/unset_used_string_variable.pkr.hcl", nil}, &PackerConfig{ Basedir: filepath.Join("testdata", "variables"), InputVariables: Variables{ @@ -131,7 +132,40 @@ func TestParse_variables(t *testing.T) { }, true, true, []packer.Build{}, - true, + false, + }, + {"unset unused variable", + defaultParser, + parseTestArgs{"testdata/variables/unset_unused_string_variable.pkr.hcl", nil}, + &PackerConfig{ + Basedir: filepath.Join("testdata", "variables"), + InputVariables: Variables{ + "foo": &Variable{ + Name: "foo", + }, + }, + Sources: map[SourceRef]*SourceBlock{ + SourceRef{"null", "null-builder"}: &SourceBlock{ + Name: "null-builder", + Type: "null", + }, + }, + Builds: Builds{ + &BuildBlock{ + Sources: []SourceRef{SourceRef{"null", "null-builder"}}, + }, + }, + }, + false, false, + []packer.Build{ + &packer.CoreBuild{ + Type: "null", + Builder: &null.Builder{}, + Provisioners: []packer.CoreBuildProvisioner{}, + PostProcessors: [][]packer.CoreBuildPostProcessor{}, + }, + }, + false, }, } testParse(t, tests)