From d068430abf13c9d9dfb1a74e3cf3fb648091841e Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 19 Mar 2020 15:30:34 +0100 Subject: [PATCH] make sure locals are evaluated only once variables are + test this (#8918) fix #8898 --- .../var-arg/{fuit_builder.pkr.hcl => fruit_builder.pkr.hcl} | 6 +++++- hcl2template/parser.go | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) rename command/test-fixtures/var-arg/{fuit_builder.pkr.hcl => fruit_builder.pkr.hcl} (67%) diff --git a/command/test-fixtures/var-arg/fuit_builder.pkr.hcl b/command/test-fixtures/var-arg/fruit_builder.pkr.hcl similarity index 67% rename from command/test-fixtures/var-arg/fuit_builder.pkr.hcl rename to command/test-fixtures/var-arg/fruit_builder.pkr.hcl index 8aff9951d..63ece8a56 100644 --- a/command/test-fixtures/var-arg/fuit_builder.pkr.hcl +++ b/command/test-fixtures/var-arg/fruit_builder.pkr.hcl @@ -3,6 +3,10 @@ variable "fruit" { type = string } +locals { + fruit = var.fruit +} + source "null" "builder" { communicator = "none" } @@ -13,6 +17,6 @@ build { ] provisioner "shell-local" { - inline = ["echo ${var.fruit} > ${var.fruit}.txt"] + inline = ["echo ${local.fruit} > ${local.fruit}.txt"] } } diff --git a/hcl2template/parser.go b/hcl2template/parser.go index d7d2591ba..5ea9a18a5 100644 --- a/hcl2template/parser.go +++ b/hcl2template/parser.go @@ -94,18 +94,17 @@ func (p *Parser) parse(filename string, varFiles []string, argVars map[string]st // Decode variable blocks so that they are available later on. Here locals // can use input variables so we decode them firsthand. + var locals []*Local { for _, file := range files { diags = append(diags, cfg.decodeInputVariables(file)...) } - var locals []*Local for _, file := range files { moreLocals, morediags := cfg.parseLocalVariables(file) diags = append(diags, morediags...) locals = append(locals, moreLocals...) } - diags = append(diags, cfg.evaluateLocalVariables(locals)...) } // parse var files @@ -151,6 +150,7 @@ func (p *Parser) parse(filename string, varFiles []string, argVars map[string]st diags = append(diags, moreDiags...) _, moreDiags = cfg.LocalVariables.Values() diags = append(diags, moreDiags...) + diags = append(diags, cfg.evaluateLocalVariables(locals)...) // decode the actual content for _, file := range files {