Skip to content

cdktn-cli: 'get' subcommand fails when HCL has an in-line comment containing closing bracket #39

@josh-hook

Description

@josh-hook

Expected Behavior

CDKTN should handle HCL variable type definitions which contain an in-line comment with a closing bracket.

Actual Behavior

CDKTN errors with an unknown type error.

cdktn get
⠋ starting...
[2026-02-18T15:57:23.519] [INFO] default - The CDKTN version has changed, generating all constraints. The previous veError: unknown type list(object({ # TODO - change to `map(object({...
    at mW (/Users/joshh/cdk-terrain/packages/cdktn-cli/bundle/bin/cmds/handlers.js:112:1008)
    at pW.emitSubmodule (/Users/joshh/cdk-terrain/packages/cdktn-cli/bundle/bin/cmds/handlers.js:112:2380)
    at new pW (/Users/joshh/cdk-terrain/packages/cdktn-cli/bundle/bin/cmds/handlers.js:112:1493)
    at Pre.generateTypescriptModule (/Users/joshh/cdk-terrain/packages/cdktn-cli/bundle/bin/cmds/handlers.js:114:76332)
    at Pre.generateTypescript (/Users/joshh/cdk-terrain/packages/cdktn-cli/bundle/bin/cmds/handlers.js:114:76559)
    at /Users/joshh/cdk-terrain/packages/cdktn-cli/bundle/bin/cmds/handlers.js:117:618
    at Array.map (<anonymous>)
    at Pre.generate (/Users/joshh/cdk-terrain/packages/cdktn-cli/bundle/bin/cmds/handlers.js:117:606)
    at async LUe (/Users/joshh/cdk-terrain/packages/cdktn-cli/bundle/bin/cmds/handlers.js:197:3611)
    at async /Users/joshh/cdk-terrain/packages/cdktn-cli/bundle/bin/cmds/handlers.js:514:17054
⠏ downloading and generating modules and providers...
Error: unknown type list(object({ # TODO - change to `map(object({...

Steps to Reproduce

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

Versions

python 3.11.14
cdktn 0.22.0 (built from yarn build)

Providers

N/A

Gist

No response

Possible Solutions

The offending HCL can be seen here: https://github.com/terraform-aws-modules/terraform-aws-eks/blob/v21.15.1/modules/karpenter/variables.tf#L121

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
}

Issue appears to be caused by the CDKTN code in packages/@cdktn/provider-schema/src/provider-schema.ts transformVariables function:

const matched = (variable["type"] as string)?.match(/\$\{(.*)\}/);
variableType = matched ? matched[1] : "any";

Since the regex does not match newlines, it ends up matching to the commented out } character, causing the resulting type to be parsed as list(object({ # TODO - change to map(object({..., which then later can't be parsed to an expression.

I managed to fix locally by replacing this with the following:

      const rawVariableType = variable["type"];
      if (
        typeof rawVariableType === "string" &&
        rawVariableType.startsWith("${") &&
        rawVariableType.endsWith("}") &&
        !rawVariableType.includes("\n")
      ) {
        variableType = rawVariableType.slice(2, -1);
      } else {
        variableType = "any";
      }

Which ensures the passed type string is fully encapsulated in a ${...}, and also persists the existing behaviour of not parsing multi-line type definitions.

Workarounds

No response

Anything Else?

Already fixed this locally, happy to submit a PR if the possible solutions sounds sensible!

References

Original issue submitted here: hashicorp/terraform-cdk#3940

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 working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions