hashicorp / hashicorp/hcl

hcl.Unmarshal adding spurious arrays

Open
#162 5 comments 9 reactions 0 assignees View on GitHub
v1
Dominant language
Go
Stars
5.8k
Forks
657
Avg merge
20h 36m
Merged PRs (30d)
6

Description

I'm not _entirely_ sure if this is a bug or not. However, I was using https://github.com/kvz/json2hcl in reverse mode to convert a HCL example to JSON, and stumbled on this...
### HCL Template

Taken directly from https://vagrantcloud.com/help/vault/vsi/configuration

``` hcl
environment "aws" {
}

vault {
address = "https://vault.service.consul:8200"
mount_path = "auth/aws"
}

serve "file" {
path = "/ramdisk/vault-token"
}
```
### Expected behavior

What should have happened?

``` hcl
{
"environment": {
"aws": {}
},
"serve": {
"file": {
"path": "/ramdisk/vault-token"
}
},
"vault": {
"address": "https://vault.service.consul:8200",
"mount_path": "auth/aws"
}
}
```
### Actual behavior

An invalid configuration, according to VSI:

``` hcl
{
"environment": [
{
"aws": [
{}
]
}
],
"serve": [
{
"file": [
{
"path": "/ramdisk/vault-token"
}
]
}
],
"vault": [
{
"address": "https://vault.service.consul:8200",
"mount_path": "auth/aws"
}
]
}
```

This JSON has an extra level of arrays inside each top-level object, which is detected as invalid.
### Steps to reproduce

I was using https://github.com/kvz/json2hcl for this, which is a CLI wrapper around hcl.
1. Clone and build https://github.com/kvz/json2hcl
2. Write the hcl template example to a file
3. `cat example.hcl | json2hcl -reverse`
### References
- https://github.com/kvz/json2hcl/issues/3#issuecomment-255741732

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the example through hcl.Unmarshal, using the json2hcl reverse-mode command and the HCL template in the issue. Trace how repeated blocks are represented and add a regression test for the expected object-shaped JSON without spurious arrays.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.