terraform-cdk icon indicating copy to clipboard operation
terraform-cdk copied to clipboard

Show warning when usage of `npm link` causes `instanceof TerraformElement` to fail

Open ansgarm opened this issue 3 years ago • 4 comments

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

Description

After extracting commonly used logic (which instantiates constructs) into a separate npm package, npm link is commonly used in local development to test the library with an cdktf project.

However the way npm link works breaks the instanceof check in TerraformStack. It creates a symlink from e.g. <cdktf-project>/node_modules/<custom-library> pointing to the custom library directory. And as the custom library also has the cdktf package installed, that one is instead used for resolving dependencies to cdktf.TerraformStack (for constructs created in the custom library code). This causes the check in TerraformStack.allocateLogicalId to fail and produces the following error: TypeError: node.tryGetContext is not a function.

Instead of this hard to debug error, we could e.g. probe the passed argument and check whether it probably is in fact a TerraformStack and print a more useful warning message (e.g. recommending yalc – see workaround section).

We could also get rid of instanceof for the check but that could only defer the underlying issue to a later (possibly even harder to debug) place in the code.

Workaround for the issue caused by npm link

Using yalc to link packages for local development solves the issue as it works differently.

References

  • https://github.com/hashicorp/terraform-cdk/blob/0732e9f5bbf2e85f7eb2cd131855fe6bf0e8b641/packages/cdktf/lib/terraform-stack.ts#L118-L121

ansgarm avatar Oct 11 '21 15:10 ansgarm

We should check if using symbols solves this problem, e.g. like we do in https://github.com/hashicorp/terraform-cdk/blob/main/packages/cdktf/lib/terraform-stack.ts#L43-L45

DanielMSchmidt avatar Dec 09 '21 16:12 DanielMSchmidt

This has been biting me pretty regularly, and it's not limited to the node instanceof check; I tried hacking the js source for that file inside of my node_modules directory to get past this error, and I hit another one on this line:

https://github.com/hashicorp/terraform-cdk/blob/abe8435e74de96cc1d969fb257a0f9123702a63c/packages/cdktf/lib/validations/validate-provider-presence.ts#L22

In general those instanceof checks do not play nicely with local library dev and it would be super helpful if there was a different way to implement the checks.

cprice404 avatar Jun 22 '22 04:06 cprice404

I got this error when I didnt have a required/used provider installed, FYI

cloneluke avatar Jul 11 '22 14:07 cloneluke

I got this error when I used Oracle OCI non-prebuilt Provider.

amarflybot avatar Aug 20 '22 20:08 amarflybot

@ansgarm - are you planning to release that as a prerelease anytime soon? (We have been trying to run our config with 0.14.0-pre.5 with no luck so far)

mbazalik avatar Oct 18 '22 14:10 mbazalik

Hey @mbazalik, we can merge that PR soon – we needed to wait making breaking changes because of the guarantee we gave for 0.12 prebuilt providers working with 0.13 (and patch releases of 0.13) 😇

ansgarm avatar Oct 20 '22 09:10 ansgarm

@mbazalik fyi: the PR was just merged 👍

ansgarm avatar Oct 27 '22 15:10 ansgarm

I got this error when I was using cdktf-cli v0.13.2, but one of my dependencies was accidentally pulling in v0.12.3 inside node_modules.

jlsalmon avatar Nov 03 '22 01:11 jlsalmon

Closed via https://github.com/hashicorp/terraform-cdk/pull/2125

ansgarm avatar Nov 08 '22 15:11 ansgarm

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.

github-actions[bot] avatar Dec 09 '22 01:12 github-actions[bot]