hashicorp / terraform-cdk

Define infrastructure resources using programming constructs and provision them using HashiCorp Terraform
https://www.terraform.io/cdktf
Mozilla Public License 2.0
4.79k stars 442 forks source link

hcl2json parse: error occurs when trying to convert null key (associated with the null provider) as string #2898

Closed vascoalramos closed 1 year ago

vascoalramos commented 1 year ago

Expected Behavior

It should prase just fine

Actual Behavior

It is giving the following error

panic: value is null

goroutine 6 [running]:
github.com/zclconf/go-cty/cty.Value.AsString({{{0x9f670, 0x410129}}, {0x0, 0x0}})
        /github/home/go/pkg/mod/github.com/zclconf/go-cty@v1.8.0/cty/value_ops.go:1254 +0x19
github.com/tmccombs/hcl2json/convert.(*converter).convertStringPart(0x4bdc60, {0x9fb68, 0x42e960})
        /github/home/go/pkg/mod/github.com/tmccombs/hcl2json@v0.3.2/convert/convert.go:243 +0x1a
github.com/tmccombs/hcl2json/convert.(*converter).convertKey(0x4bdc60, {0x9fbe8, 0x40abb8})
        /github/home/go/pkg/mod/github.com/tmccombs/hcl2json@v0.3.2/convert/convert.go:266 +0x18
github.com/tmccombs/hcl2json/convert.(*converter).convertExpression(0x4bdc60, {0x9fba8, 0x4be000})
        /github/home/go/pkg/mod/github.com/tmccombs/hcl2json@v0.3.2/convert/convert.go:187 +0x46
github.com/tmccombs/hcl2json/convert.(*converter).convertBody(0x4bdc60, 0x45e630)
        /github/home/go/pkg/mod/github.com/tmccombs/hcl2json@v0.3.2/convert/convert.go:87 +0x18
github.com/tmccombs/hcl2json/convert.(*converter).convertBlock(0x4bdc60, 0x45a7e0, 0x48c8d0)
        /github/home/go/pkg/mod/github.com/tmccombs/hcl2json@v0.3.2/convert/convert.go:137 +0x7
github.com/tmccombs/hcl2json/convert.(*converter).convertBody(0x4bdc60, 0x45e6e0)
        /github/home/go/pkg/mod/github.com/tmccombs/hcl2json@v0.3.2/convert/convert.go:80 +0x8
github.com/tmccombs/hcl2json/convert.convertFile(0x453a80, {0x0})
        /github/home/go/pkg/mod/github.com/tmccombs/hcl2json@v0.3.2/convert/convert.go:68 +0x5
github.com/tmccombs/hcl2json/convert.File(0x453a80, {0x0})
        /github/home/go/pkg/mod/github.com/tmccombs/hcl2json@v0.3.2/convert/convert.go:37 +0x2
github.com/tmccombs/hcl2json/convert.Bytes({0x432180, 0x59, 0x60}, {0x410310, 0xe}, {0x0})
        /github/home/go/pkg/mod/github.com/tmccombs/hcl2json@v0.3.2/convert/convert.go:27 +0x8
main.main.func1({{}, 0x0, 0x0}, {0x48c840, 0x2, 0x3})
        /__w/terraform-cdk/terraform-cdk/packages/@cdktf/hcl2json/main.go:62 +0x7
main.registrationWrapper.func1({{}, 0x0, 0x0}, {0x48c840, 0x3, 0x3})
        /__w/terraform-cdk/terraform-cdk/packages/@cdktf/hcl2json/main.go:29 +0x5
syscall/js.handleEvent()
        /opt/golang/go/src/syscall/js/func.go:94 +0x26
exit code: 2

Steps to Reproduce

  1. Use javascript pakcage: npm i @cdktf/hcl2json
  2. Run the following code:

    const { parse } = require("@cdktf/hcl2json");
    
    const hcl = `
    provider "null" {
        alias = "null"
    }
    module "test" {
        source = "./test"
    
        providers = {
            null  = null.null
        }
    }
    `;
    
    (async () => {
        const json = await parse("test.tf", hcl);
        console.log(json);
    })();

Versions

cdktf-cli: 0.16.1 node: v18.16.0 terraform: 1.4.6 arch: x64 os: darwin 22.5.0

Providers

No response

Gist

No response

Workarounds

The issue is linked to a dependency of yours (tmccombs/hcl2json, on version v0.3.2 - source code).

The error comes from that dependency: https://github.com/tmccombs/hcl2json/issues/35 This error is fixed from version v0.3.4 onwards

Anything Else?

No response

References

No response

Help Wanted

Community Note

vascoalramos commented 1 year ago

If it's helpful, I am interested in working on this issue and submitting a pull request with the fix

mutahhir commented 1 year ago

Seems like the issue with handling null on hcl2json has been resolved and we need to update our dependency version. We'd be happy to review if you'd like to submit a PR.

vascoalramos commented 1 year ago

Yes, for sure. I'll submit a PR

github-actions[bot] commented 11 months 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've 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.