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.78k stars 441 forks source link

CDKTF tsconfig paths not working #2237

Closed sabinayakc closed 1 year ago

sabinayakc commented 1 year ago

Community Note

cdktf & Language Versions

language: typescript
cdktf-cli: 0.13.0
node: v16.17.0
cdktf: 0.13.2
constructs: 10.1.142
jsii: null
terraform: 1.3.2
arch: x64
os: linux 5.10.102.1-microsoft-standard-WSL2

Affected Resource(s)

CDKTF Typescript

// Not all constructs are taggable, so we need to filter it type TaggableConstruct = IConstruct & { tags?: { [key: string]: string }; tagsInput?: { [key: string]: string }; };

function isTaggableConstruct(x: IConstruct): x is TaggableConstruct { return "tags" in x && "tagsInput" in x; }

export class TagsAddingAspect implements IAspect { constructor(private tagsToAdd: Record<string, string>) {}

// This method is called on every Construct within the specified scope (resources, data sources, etc.). visit(node: IConstruct) { if (isTaggableConstruct(node)) { // We need to take the input value to not create a circular reference const currentTags = node.tagsInput || {}; this.tagsToAdd["name"] = node.node.id; node.tags = { ...this.tagsToAdd, ...currentTags }; } } }

### Debug Output

⠋ Synthesizing [2022-10-31T10:37:56.103] [ERROR] default - /home/projects/github/metronet-iac-core/node_modules/@cdktf/provider-aws/src/provider/index.ts:3003 export class AwsProvider extends cdktf.TerraformProvider { ^ [2022-10-31T10:37:56.104] [ERROR] default - TypeError: Class extends value undefined is not a constructor or null at Object. (/home/projects/github/metronet-iac-core/node_modules/@cdktf/provider-aws/src/provider/index.ts:3003:40) at Module._compile (node:internal/modules/cjs/loader:1126:14) at Module._extensions..js (node:internal/modules/cjs/loader:1180:10) at Object.require.extensions. [as .js] (/home/projects/github/metronet-iac-core/node_modules/ts-node/src/index.ts:1608:43) at Module.load (node:internal/modules/cjs/loader:1004:32) at Function.Module._load (node:internal/modules/cjs/loader:839:12) at Module.require (node:internal/modules/cjs/loader:1028:19) at require (node:internal/modules/cjs/helpers:102:18) at Object. (/home/projects/github/metronet-iac-core/tgw-stack.ts:3:1) ERROR: cdktf encountered an error while synthesizing

Synth command: npx ts-node -r tsconfig-paths/register --prefer-ts-exts src/main.ts Error: non-zero exit code 1


<!---
Please provide a link to a GitHub Gist containing the complete debug output. Please do NOT paste the debug output in the issue; just paste a link to the Gist.
--->

### Expected Behavior
- CDKTF should be able to synthesize the stack. 
<!--- What should have happened? --->

### Actual Behavior
- CDKTF throws the above error

<!--- What actually happened? --->

### Steps to Reproduce
- Add paths to tsconfig.json file
- Use one of the paths in your imports. Such as `import { TagsAddingAspect } from "@helpers/tag-aspect";`
- Add some generic stack with AWS Provider.
- Add the Aspect to the stack.

Aspects.of(stack).add( new TagsAddingAspect({ createdBy: "cdktf", environment: "global" }) );


- Install tsconfig-paths and update your cdktf.json > app.
- Run CDKTF synth.

<!--- Please list the steps required to reproduce the issue. --->

### References

<!---
Information about referencing Github Issues: https://help.github.com/articles/basic-writing-and-formatting-syntax/#referencing-issues-and-pull-requests

Are there any other GitHub issues (open or closed) or pull requests that should be linked here? Vendor documentation? For example:
--->

- https://discuss.hashicorp.com/t/cdktf-tsconfig-paths-not-working-for-local-imports/46163
- https://github.com/aws/jsii/issues/865#issuecomment-950261852 
DanielMSchmidt commented 1 year ago

Does the problem exist as-well if you run npx ts-node -r tsconfig-paths/register --prefer-ts-exts src/main.ts directly?

DanielMSchmidt commented 1 year ago

Ah I just saw your answer. It seems like the error stems from the app command, so there is not much cdktf itself can do here, we just execute this command you give us. The error looks like importing cdktf inside of the pre-built provider fails to resolve, in TS this can sometimes hint to a circular dependency if I recall correctly.

github-actions[bot] commented 1 year 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.