feat(locals): add support for terraform locals variables #7946
+1,214
−5
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.








Closes #7944
Reason for Proposed Changes
Proposed Changes
getLocals()in terraform.go aftergetInputVariables()so variables are available for local evaluation. Uses directory-level caching for O(n) performance similar to variables.go;Validation against actual Terraform v1.14.3:
• Duplicates: "Duplicate local value definition"
• Circular: "Cycle: local.x, local.y"
• Missing vars: Handled with warnings (appropriate for static analysis)
Terraform validate or init command error outputs
Duplicates
╷
│ Error: Terraform encountered problems during initialisation, including problems
│ with the configuration, described below.
│
│ The Terraform configuration must be valid before initialization so that
│ Terraform can determine which modules and providers need to be installed.
│
│
╵
╷
│ Error: Duplicate local value definition
│
│ on file2.tf line 3, in locals:
│ 3: duplicate_name = "from_file2"
│
│ A local value named "duplicate_name" was already defined at
│ file1.tf:2,3-32. Local value names must be unique within a module.
╵
╷
│ Error: Duplicate local value definition
│
│ on file2.tf line 3, in locals:
│ 3: duplicate_name = "from_file2"
│
│ A local value named "duplicate_name" was already defined at
│ file1.tf:2,3-32. Local value names must be unique within a module.
╵
Overrides
╷
│ Error: Terraform encountered problems during initialisation, including problems
│ with the configuration, described below.
│
│ The Terraform configuration must be valid before initialization so that
│ Terraform can determine which modules and providers need to be installed.
│
│
╵
╷
│ Error: Duplicate local value definition
│
│ on override_locals_b.tf line 3, in locals:
│ 3: app_name = "overridden_name"
│
│ A local value named "app_name" was already defined at
│ override_locals_a.tf:2,3-26. Local value names must be unique within a
│ module.
╵
╷
│ Error: Duplicate local value definition
│
│ on override_locals_b.tf line 3, in locals:
│ 3: app_name = "overridden_name"
│
│ A local value named "app_name" was already defined at
│ override_locals_a.tf:2,3-26. Local value names must be unique within a
│ module.
Missing var
╷
│ Error: Reference to undeclared input variable
│
│ on locals_with_missing_var.tf line 4, in locals:
│ 4: with_missing_var = var.nonexistent_variable
│
│ An input variable with the name "nonexistent_variable" has not been
│ declared. This variable can be declared with a variable
│ "nonexistent_variable" {} block.
terraform validate
╷
│ Error: Cycle: local.circular_b (expand), local.circular_a (expand)
│
│
╵
For more information:
I submit this contribution under the Apache-2.0 license.