hashicorp / packer

Packer is a tool for creating identical machine images for multiple platforms from a single source configuration.
http://www.packer.io
Other
14.99k stars 3.33k forks source link

[HCL] Allow to interpolate locals from the same locals block #8754

Closed eliezio closed 4 years ago

eliezio commented 4 years ago

Overview of the Issue

Packer HCL parser fails to interpolate local variables referenced in other local variables.

Reproduction Steps

On the snippet below, based on the documentation example, Packer always fails if you replace the definition on line 10 by similar definitions on lines 11 or 12.

variable "project_name" {
  default = "test"
}

variable "name_prefix" {
  default = "foo"
}

locals {
  default_name_prefix = "${var.project_name}-web"
  name_prefix         = "${var.name_prefix != "" ? var.name_prefix : local.default_name_prefix}"  // works
  //name_prefix         = "${local.default_name_prefix}"  // FAILS
  //name_prefix         = local.default_name_prefix  // FAILS
}

Packer version

From packer --version: 1.5.4

Simplified Packer Buildfile

Included above.

Operating system and Environment details

macOS 10.15.3

Log Fragments and crash.log files

2020/02/16 14:24:39 [INFO] Packer version: 1.5.4 [go1.13.8 darwin amd64]
2020/02/16 14:24:39 Checking 'PACKER_CONFIG' for a config file path
2020/02/16 14:24:39 'PACKER_CONFIG' not set; checking the default config file path
2020/02/16 14:24:39 Attempting to open config file: /Users/ebo/.packerconfig
2020/02/16 14:24:39 [WARN] Config file doesn't exist: /Users/ebo/.packerconfig
2020/02/16 14:24:39 Setting cache directory: /Users/ebo/tmp/packer-locals-issue/packer_cache
2020/02/16 14:24:39 [ERR] Checkpoint error: EOF

  on main.pkr.hcl line 12, in locals:
  12:   name_prefix         = "${local.default_name_prefix}"

This object does not have an attribute named "default_name_prefix".

2020/02/16 14:24:39 Build debug mode: false
2020/02/16 14:24:39 Force build: false
2020/02/16 14:24:39 On error:
2020/02/16 14:24:39 Waiting on builds to complete...
==> Builds finished but no artifacts were created.
Error: Unsupported attribute

2020/02/16 14:24:39 [INFO] (telemetry) Finalizing.
  on main.pkr.hcl line 12, in locals:
  12:   name_prefix         = "${local.default_name_prefix}"

This object does not have an attribute named "default_name_prefix".

==> Builds finished but no artifacts were created.
2020/02/16 14:24:40 waiting for all plugin processes to complete...
azr commented 4 years ago

Hello @eliezio, thanks for opening and trying out HCL2.

Currently this works but the locals blocks have to be split:

variable "project_name" {
  default = "test"
}

variable "name_prefix" {
  default = "foo"
}

locals {
  default_name_prefix = "${var.project_name}-web"
  name_prefix         = "${var.name_prefix != "" ? var.name_prefix : local.default_name_prefix}"  // works
}

locals {
  foo  = "${local.default_name_prefix}"  // YAY
  bar  = local.default_name_prefix  // YAY
}

I think your example makes sense and think I should have also made this possible too. I'm going to fix this soon.

azr commented 4 years ago

Hey @eliezio, it is not possible to use locals within a same locals block, this is due to a limitation/requirement of our HCL2 parsing library.

Again splitting the blocks should fix the issue:

locals {
  default_name_prefix = "${var.project_name}-web"
  name_prefix         = "${var.name_prefix != "" ? var.name_prefix : local.default_name_prefix}"  // works
}

locals {
  foo  = "${local.default_name_prefix}"  // YAY
  bar  = local.default_name_prefix  // YAY
}

I'm going to go ahead and close the issue.

Sorry all !!!

azr commented 4 years ago

Ah wait, it works for Terraform ! We will try to investigate a little more.

ghost commented 4 years ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.