Skip to content
This repository was archived by the owner on Dec 10, 2025. It is now read-only.
This repository was archived by the owner on Dec 10, 2025. It is now read-only.

cdktf: CDKTF init from Template Creates Duplicate/Missing Import Libs #3930

@HackedRico

Description

@HackedRico

Expected Behavior

After running the following CDKTF init command, it should produce a working main.ts file. ../vbox-terraform is an terraform project utilizing the VirtualBox Provider whereas initing from this project should have this provider in main.ts. Furthermore, there is an duplicate TerraformStack lib being imported which causes errors.

cdktf init --template=typescript \
    --from-terraform-project=../vbox-terraform \
    --local
    --providers=local

The result of the above command produces:

import { Construct } from "constructs";
import {
  VariableType,
  TerraformVariable,
  TerraformCount,
  TerraformOutput,
  Fn,
  TerraformStack, // Terraform Stack lib imported here already
} from "cdktf";
/*
 * Provider bindings are generated by running `cdktf get`.
 * See https://cdk.tf/provider-generation for more details.
 */
import { Vm } from "./.gen/providers/virtualbox/vm";
import { App, TerraformStack } from "cdktf"; // BUG: Duplicate Terraform Stack Import

import { VirtualboxProvider } from "./.gen/providers/virtualbox/provider";

class MyStack extends TerraformStack {
  constructor(scope: Construct, id: string) {
    super(scope, id);
    new VirtualboxProvider(this, "virtualbox", {});
    // existing code...
  }
}

Actual Behavior

A working main.ts to run cdktf deploy which does not have duplicate import of TerraformStack lib and has imported VirtualboxProvider lib.

Expected:

import { Construct } from "constructs";
import {
  VariableType,
  TerraformVariable,
  TerraformCount,
  TerraformOutput,
  Fn,
  TerraformStack,
} from "cdktf";
/*
 * Provider bindings are generated by running `cdktf get`.
 * See https://cdk.tf/provider-generation for more details.
 */
import { Vm } from "./.gen/providers/virtualbox/vm";
import { App } from "cdktf"; // Fix: Removed Duplicate TerraformStack Lib

import { VirtualboxProvider } from "./.gen/providers/virtualbox/provider";

class MyStack extends TerraformStack {
  constructor(scope: Construct, id: string) {
    super(scope, id);
    new VirtualboxProvider(this, "virtualbox", {});
    // existing code...
  }
}

Steps to Reproduce

  1. Create a directory with a test Terraform Project, a simple test could be:
terraform {
  required_providers {
    virtualbox = {
      source = "terra-farm/virtualbox"
      version = "0.2.2-alpha.1"
    }
  }
}

provider "virtualbox" {}

resource "virtualbox_vm" "single_vm" {
  name      = "simple-vm"
  image     = "https://app.vagrantup.com/ubuntu/boxes/bionic64/versions/20180903.0.0/providers/virtualbox.box"
  cpus      = 2
  memory    = "1024 mib"

  network_adapter {
    type = "bridged"
    host_interface = "en0: Wi-Fi"
    device = "IntelPro1000MTServer"
  }
}
  1. Run CDKTF init command on the created Terraform Project Directory
cdktf init --template=typescript \
    --from-terraform-project=<PATH-TO-TF-PROJECT> \
    --local
    --providers=local
  1. View generated CDKTF directory's main.ts

Versions

cdktf debug
language: typescript
cdktf-cli: 0.21.0
node: v22.17.1
cdktf: 0.21.0
constructs: 10.4.2
jsii: null
terraform: 1.13.0
arch: x64
os: darwin 24.6.0
providers
terra-farm/[email protected] (LOCAL)
        terraform provider version: 0.2.2-alpha.1
@cdktf/provider-local (PREBUILT)
        terraform provider version: 2.5.3 
        prebuilt provider version: 11.0.0
        cdktf version: ^0.21.0

Providers

┌───────────────────────┬──────────────────┬─────────┬───────────────┬───────────────────────┬─────────────────┐
│ Provider Name         │ Provider Version │ CDKTF   │ Constraint    │ Package Name          │ Package Version │
├───────────────────────┼──────────────────┼─────────┼───────────────┼───────────────────────┼─────────────────┤
│ terra-farm/virtualbox │ 0.2.2-alpha.1    │         │ 0.2.2-alpha.1 │                       │                 │
├───────────────────────┼──────────────────┼─────────┼───────────────┼───────────────────────┼─────────────────┤
│ local                 │ 2.5.3            │ ^0.21.0 │               │ @cdktf/provider-local │ 11.0.0          │
└───────────────────────┴──────────────────┴─────────┴───────────────┴───────────────────────┴─────────────────┘

Gist

No response

Possible Solutions

No response

Workarounds

No response

Anything Else?

No response

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