hashicorp / packer

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

"packer validate" validates HCL file as JSON if not suffixed with 'pkr.hcl' #9637

Closed deepbrook closed 4 years ago

deepbrook commented 4 years ago

When filing a bug, please include the following headings if possible. Any example text in this template can be deleted.

Overview of the Issue

Running the validate command against my HCL file does not validate the file as expected after https://github.com/hashicorp/packer/pull/9346 was merged and released with 1.6.0, if the template's suffix is not .pkr.hcl.

Reproduction Steps

Vaidating a template named template.hcl with packer validate throws the following:

❯ packer validate --syntax-only resource/package/template.hcl
Failed to parse template: Error parsing JSON: invalid character 'v' looking for beginning of value
At line 1, column 1 (offset 1):
    1: v
      ^

Renaming the file to template.pkr.hcl solves the issue.

Packer version

❯ packer --version
1.6.0

Operating system and Environment details

Ubuntu 18.04

deepbrook commented 4 years ago

Oh right. Renaming the template to template.pkr.hcl works as expected. That's picky. Updated the issue accordingly.

azr commented 4 years ago

Hey @deepbrook, thanks for opening. Yup, that's the behaviour, because the schema/layout of an HCL file is tool dependant -- the schema for a Terraform file != the schema for a Packer file -- Packer HCL2 ignores non *.pkr.hcl files because it does not and will not know about other schemas from other tools. Because it might be a Terraform file, a Nomad file or something else Packer is 'blind to it'.

Currently the default main maintained format is the JSON format and because no HCL file is found, this falls back to reading files with the JSON parser, which will open anything.

Note that in the (most likely far) future when the current JSON layout is going to be deprecated; Packer is still going to be blind to files that are not suffixed with *.pkr.hcl.

This has a lot of benefits, like allowing developer tools to differentiate between a TF files and a PKR file; which will make the ecosystem better etc. etc.

Cheers !

deepbrook commented 4 years ago

This has a lot of benefits, like allowing developer tools to differentiate between a TF files and a PKR file; which will make the ecosystem better etc. etc.

i'd expect the tool to just eat the file and validate it, but I'm also not a big fan of hand-holding. Additionally, wouldn't it make more sense to have a hashbang-esque header line in the file to declare the tool the file is used for? But I was also under the impression that HCL is tool agnostic, so never you mind me. I'm sure you've had plenty of discussions on this. :) Thanks for the quick reply.

azr commented 4 years ago

Sure no worries, :D yes HCL2 is tool specific ! That's why it's so nice because you can bend it to your will a bit more, read things a bit later with a context, etc. But then coming from JSON it can be a bit confusing.

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.