Skip to content

cdktf get fails with "unknown type" error when Terraform module contains inline comments in variable type definitions #3940

@paulannetts

Description

@paulannetts

Expected Behavior

CDKTF should handle inline comments in variable type definitions gracefully, either by:

  1. Stripping comments before parsing type definitions, or
  2. Parsing the type definition in a way that ignores comments

The Terraform CLI itself has no issues with this syntax, as inline comments are valid HCL.

Actual Behavior

CDKTF fails with an "unknown type" error and cannot generate the module bindings.

Steps to Reproduce

  1. Create a cdktf.json with:
{
  "language": "python",
  "terraformModules": [
    {
      "name": "karpenter",
      "source": "terraform-aws-modules/eks/aws//modules/karpenter",
      "version": "~> 21.0"
    }
  ]
}
  1. Run cdktf get
  2. Observe the error

Versions

python: Python 3.11.12
pip: pip 25.3 from ... python 3.11)
pipenv: null
providers
cdktf-cdktf-provider-aws (PREBUILT)
terraform provider version: 6.19.0
prebuilt provider version: 21.16.0
cdktf version: ^0.21.0
cdktf-cdktf-provider-helm (PREBUILT)
terraform provider version: 3.0.2
prebuilt provider version: 12.0.1
cdktf version: ^0.21.0
cdktf-cdktf-provider-kubernetes (PREBUILT)
terraform provider version: 2.38.0
prebuilt provider version: 12.1.0
cdktf version: ^0.21.0
cdktf-cdktf-provider-time (PREBUILT)
terraform provider version: 0.13.1
prebuilt provider version: 11.0.0
cdktf version: ^0.21.0

Providers

───────────────┬─────────────────┬────────┬───────────┬─────────────────────────────────────────────────┐

Provider Name │ Provider Version│ CDKTF │ Constraint│ Package Name Package Version │

───────────────┼─────────────────┼────────┼───────────┼─────────────────────────────────────────────────┤

aws │ 6.19.0 │ ^0.21.0│ │ cdktf-cdktf-provider-aws 21.16.0 │

───────────────┼─────────────────┼────────┼───────────┼─────────────────────────────────────────────────┤

helm │ 3.0.2 │ ^0.21.0│ │ cdktf-cdktf-provider-helm 12.0.1 │

───────────────┼─────────────────┼────────┼───────────┼─────────────────────────────────────────────────┤

kubernetes │ 2.38.0 │ ^0.21.0│ │ cdktf-cdktf-provider-kubernetes 12.1.0 │

───────────────┼─────────────────┼────────┼───────────┼─────────────────────────────────────────────────┤

time │ 0.13.1 │ ^0.21.0│ │ cdktf-cdktf-provider-time 11.0.0 │

───────────────┴─────────────────┴────────┴───────────┴─────────────────────────────────────────────────┘

Gist

No response

Possible Solutions

Workarounds

Pin the module to a version before the inline comment was added:

{
  "name": "karpenter",
  "source": "terraform-aws-modules/eks/aws//modules/karpenter",
  "version": "21.4.0"
}

Anything Else?

Error Output

Error: unknown type list(object({ # TODO - change to `map(object({...
    at cL (/Users/paulannetts/.nvm/versions/node/v22.20.0/lib/node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:112:1008)
    at lL.emitSubmodule (/Users/paulannetts/.nvm/versions/node/v22.20.0/lib/node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:112:2380)
    at new lL (/Users/paulannetts/.nvm/versions/node/v22.20.0/lib/node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:112:1493)
    at Pre.generateTypescriptModule (/Users/paulannetts/.nvm/versions/node/v22.20.0/lib/node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:114:76332)
    at Pre.generateTypescript (/Users/paulannetts/.nvm/versions/node/v22.20.0/lib/node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:114:76559)
    at /Users/paulannetts/.nvm/versions/node/v22.20.0/lib/node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:117:618
    at Array.map (<anonymous>)
    at Pre.generate (/Users/paulannetts/.nvm/versions/node/v22.20.0/lib/node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:117:606)
    at async kMe (/Users/paulannetts/.nvm/versions/node/v22.20.0/lib/node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:197:3611)

Root Cause

The issue occurs in the terraform-aws-modules/eks/aws//modules/karpenter module (v21.5.0+) at line 121 of modules/karpenter/variables.tf:

variable "iam_policy_statements" {
  description = "A list of IAM policy [statements](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document#statement) - used for adding specific IAM permissions as needed"
  type = list(object({ # TODO - change to `map(object({...}))` in next major version
    sid           = optional(string)
    actions       = optional(list(string))
    not_actions   = optional(list(string))
    effect        = optional(string)
    resources     = optional(list(string))
    not_resources = optional(list(string))
    principals = optional(list(object({
      type        = string
      identifiers = list(string)
    })))
    not_principals = optional(list(object({
      type        = string
      identifiers = list(string)
    })))
    condition = optional(list(object({
      test     = string
      values   = list(string)
      variable = string
    })))
  }))
  default = null
}

The inline comment # TODO - change to map(object({...})) in the type definition causes CDKTF's parser to fail, even though this is valid HCL syntax that Terraform CLI handles without issues.

Source: https://github.com/terraform-aws-modules/terraform-aws-eks/blob/v21.8.0/modules/karpenter/variables.tf#L121

Related Issues

This is similar to #928 (unknown type set(string)) but the root cause is different - it's not an unsupported type, but the parser failing to handle valid HCL comments within type definitions.

Impact

This issue can occur with any Terraform module that includes inline comments in variable type definitions. As module maintainers add documentation or TODO comments inline, more modules may become incompatible with CDKTF unless they're specifically aware of this limitation.

The parser should be made more robust to handle all valid HCL syntax, including inline comments in any context.

References

No response

Help Wanted

  • I'm interested in contributing a fix myself

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingnewUn-triaged issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions