hashicorp / hcl

HCL is the HashiCorp configuration language.
Mozilla Public License 2.0
5.21k stars 587 forks source link

Duplicate map keys #35

Open thorduri opened 9 years ago

thorduri commented 9 years ago

First off, I'm unsure if this is the expected behaviour or not.

When decoding maps it's currently not possible to detect duplicates, as HCL will overwrite (last one wins) the map key.

The HCL fix should be pretty easy, i.e. stick something like the following into decodeMap() right before setting the value on the map:

dupKey := resultMap.MapIndex(key)
if dupKey.IsValid() {
        return fmt.Errorf(
                "%s: duplicate map key %v", name, key)
}

See for example: https://github.com/hashicorp/consul/pull/1055 or generally any HCL file for that matter:

variable "foo" {
        description = "foo variable"
        default = "bar"
}

...

variable "foo" {
        description = "foo variable"
        default = "crux"
}

Could lead to interesting gotchas when depending on the defaults.

I have a patch + tests for HCL, but this probably will bubble up to HCL users that will need munging since this changes behaviour somewhat and I do not know how to best (or even if) handle that.

geoffreywiseman commented 2 years ago

Just ran into this on a project -- seems like this is still an issue and Terraform doesn't notice?

apparentlymart commented 2 years ago

This issue was about the first generation of HCL, which Terraform hasn't used since Terraform v0.12.

Terraform uses HCL 2, though I'm not really sure how best to translate this issue's situation into HCL 2 terms because HCL 2 has a pretty different design where the source of the problem here could correspond with a few different possible behaviors in HCL 2. If you are seeing something that seems incorrect in Terraform then I would suggest opening an issue in the Terraform repository first, and then the Terraform team will debug and figure out if it's a HCL problem and address it upstream if so.