-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hcl2template: recursively evaluate local variables
The logic for evaluating local variables used to rely on their definition order, with some edge cases. Typically `locals` blocks define multiple local variables, which don't necessarily appear in the same order at evaluation as within the template, leading to inconsistent behaviour, as the order in which those are added to the list of local variables is non-deterministic. To avoid this problem, we change how local variables are evaluated, and we're adopting a workflow similar to datasources, where the local variables first build a list of direct dependencies. Then when evaluation happens, we evaluate all the dependencies recursively for each local variable, which takes care of this issue. As with Datasources, we add a cap to the recursion: 10. I.e. if the evaluation of a single variable provokes an infinite recursion, we stop at that point and return an error to the user, telling them to fix their template.
- Loading branch information
1 parent
7ba6da7
commit 11815a1
Showing
4 changed files
with
91 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package packer_test | ||
|
||
func (ts *PackerTestSuite) TestEvalLocalsOrder() { | ||
pluginDir, cleanup := ts.MakePluginDir() | ||
defer cleanup() | ||
|
||
ts.PackerCommand().UsePluginDir(pluginDir). | ||
Runs(10). | ||
Check failure on line 8 in packer_test/local_eval_test.go GitHub Actions / Windows go tests
|
||
Stdin("local.test_local\n"). | ||
SetArgs("console", "./templates/locals_no_order.pkr.hcl"). | ||
Assert(MustSucceed(), Grep("\\[\\]", grepStdout, grepInvert)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
locals { | ||
test_local = can(local.test_data) ? local.test_data : [] | ||
|
||
test_data = [ | ||
{ key = "value" } | ||
] | ||
} |