CDK not respecting pom.xml

Hello,

I’m attempting to run cdktf get in my Java environment, but since I’ve upgraded to version 0.13.0, I get the following error

❯ cdktf get
Usage Error: The minor version of the library (0.12.3) and the CLI (0.13.0) are different. Please update the library to the same minor version and regenerate your provider bindings with 'cdktf get' and update your prebuilt providers.

It’s possible that my pom.xml isn’t working correctly. When I run mvn install -up -X, I see this in the debut logs

[DEBUG] Could not find metadata com.hashicorp:cdktf/maven-metadata.xml in local (/Users/zach/.m2/repository)
[DEBUG] Skipped remote request for com.hashicorp:cdktf/maven-metadata.xml, locally cached metadata up-to-date
...
[DEBUG]       com.hashicorp:cdktf:jar:0.12.3:compile
...

Thoughts?

The log message also mentions 0.12.3, but it should be 0.13.0 as the error message points out. Is the equivalent of this line in your pom.xml set to 0.13.0? terraform-cdk/pom.xml at main · hashicorp/terraform-cdk · GitHub

That’s correct, my pom.xml has it set to 0.13.0. So here’s a gist of my pom.xml.

Note

    <dependency>
      <groupId>com.hashicorp</groupId>
      <artifactId>cdktf</artifactId>
      <version>0.13.0</version>
    </dependency>

It does seem like my pom.xml and my cdktf.json aren’t being respected, though, because my plugins are just using the latest versions, not what’s in cdktf.json. I’ll post that too in case it’s important

{
  "language": "java",
  "app": "mvn -e -q compile exec:java",
  "projectId": "fece0b5f-6717-4dcd-bdd5-bbe64ff709b8",
  "sendCrashReports": "false",
  "terraformProviders": [
    "hashicorp/azurerm@3.23.0",
    "hashicorp/azuread@2.15.0",
    "carlpett/sops@0.7.1"
  ],
  "terraformModules": [],
  "context": {
    "excludeStackIdFromLogicalIds": "true",
    "allowSepCharsInLogicalIds": "true"
  }
}

Okay, I see. Did you run cdktf get after updating?

So that’s actually when I see the error:

❯ cdktf get
Usage Error: The minor version of the library (0.12.3) and the CLI (0.13.0) are different. Please update the library to the same minor version and regenerate your provider bindings with 'cdktf get' and update your prebuilt providers.

Ah sorry, my bad. So I looked in the CLI code and we consult mvn dependency:list to understand which cdktf version is installed on the library side. It seems like the output indicates 0.12.3 as installed cdktf version (please check), so maybe running mvn install will help? Not much of a Java expert so I might have the wrong name in mind here. You can disable this check by setting the environment variable DISABLE_VERSION_CHECK=1

That was helpful. Thanks for the tricks. I did a bit more debugging with this new information and was able to get it to work. I think the end issue was a difference in dependency requirements on a couple of my cdktf providers
I updated these two as well

[INFO] +- com.hashicorp:cdktf-provider-azurerm:jar:3.0.9:compile
[INFO] +- com.hashicorp:cdktf-provider-azuread:jar:3.0.9:compile

and I was able to get cdktf 0.13.0 installed. Thanks for looking into this!

Ah I see, since you had a few providers in your cdktf.json I haven’t thought that you might also use pre-built ones. You might be able to remove these two providers from your cdktf.json then, that should make cdktf get faster.

So the issue I’m having with cdktf get and pom.xml is that cdktf get always creates an imports and resources directory which contain the entirety of the prebuilt providers downloaded on my machine. Then cdktf synth tries to synthesize the providers, which causes errors. Thoughts there?

cdktf get generates bindings for providers locally, it can be used instead of the pre-built providers you are using. It is expected to create those bindings in the folders you mentioned. In general you would normally only use either the pre-built version that is installed through maven or the generated local one, but not both.

Ah I see. That’s just my misunderstanding then. To make sure I’m understanding right, there are two files where providers can be set. pom.xml and cdktf.json. If I set the provider using cdktf provider add or by adding it to cdktf.json, TF will use the pre-built provider when synthesizing? However, if I set the providers in pom.xml, it will be installed with cdktf get and generate the local bindings? So if I don’t set it in pom.xml, I won’t download the providers when I run cdktf get?

Not really, let me try to rephrase it so it’s a bit clearer. There are two mechanisms:

  • Downloading a provider (pre-built provider is how we call those)
  • Generating provider bindings
    • All providers and modules are supported
    • You add the name (the same full name and version syntax you use in Terraform) of the provider / module into your cdktf.json file and run cdktf get. This creates local folder (that should ideally not be in version control) with the bindings you need for the provider / module

The cdktf provider add command takes one or multiple provider names (with version constraints) and checks if there is a pre-built provider in place with these constraints. If yes it installs the provider it found, if not it falls back to the cdktf.json and runs cdktf get for you.

1 Like

Thank you for the clarification. I’ll take a look at doing that on Monday.