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

C# library version check can match the wrong package

Open jsteinich opened this issue 8 months ago • 5 comments

Expected Behavior

Library vs cli version check passes

Actual Behavior

Errors with The major version of the library (9.0.1) and the CLI (0.19.1) are different...

Steps to Reproduce

  1. Create a new C# library project with a reference to cdktf
  2. Create a new C# cdktf project with a reference to the project created in step 1
  3. Add a reference to a pre-built provider
  4. Run a cdktf command

Versions

language: csharp cdktf-cli: 0.19.1 node: v19.9.0 cdktf: 0.19.1 constructs: 10.3.0 jsii: 1.92.0 terraform: 1.4.5 arch: x64 os: win32 10.0.22631 dotnet: 8.0.100

Providers

┌─────────────────────┬──────────────────┬─────────┬────────────┬──────────────────────────────────────┬─────────────────┐
│ Provider Name       │ Provider Version │ CDKTF   │ Constraint │ Package Name                         │ Package Version │
├─────────────────────┼──────────────────┼─────────┼────────────┼──────────────────────────────────────┼─────────────────┤
│ external            │ 2.3.2            │ ^0.19.0 │            │ HashiCorp.Cdktf.Providers.External   │ 9.0.1           │
├─────────────────────┼──────────────────┼─────────┼────────────┼──────────────────────────────────────┼─────────────────┤
│ http                │ 3.4.0            │ ^0.19.0 │            │ HashiCorp.Cdktf.Providers.Http       │ 8.0.0           │
├─────────────────────┼──────────────────┼─────────┼────────────┼──────────────────────────────────────┼─────────────────┤
│ cyrilgdn/postgresql │ 1.21.0           │ ^0.19.0 │            │ HashiCorp.Cdktf.Providers.Postgresql │ 10.0.0          │
├─────────────────────┼──────────────────┼─────────┼────────────┼──────────────────────────────────────┼─────────────────┤
│ random              │ 3.6.0            │ ^0.19.0 │            │ HashiCorp.Cdktf.Providers.Random     │ 10.0.1          │
└─────────────────────┴──────────────────┴─────────┴────────────┴──────────────────────────────────────┴─────────────────┘
┌─────────────────────┬──────────────────┬─────────┬────────────┬──────────────────────────────────────┬─────────────────┐
│ Provider Name       │ Provider Version │ CDKTF   │ Constraint │ Package Name                         │ Package Version │
├─────────────────────┼──────────────────┼─────────┼────────────┼──────────────────────────────────────┼─────────────────┤
│ external            │ 2.3.2            │ ^0.19.0 │            │ HashiCorp.Cdktf.Providers.External   │ 9.0.1           │
├─────────────────────┼──────────────────┼─────────┼────────────┼──────────────────────────────────────┼─────────────────┤
│ http                │ 3.4.0            │ ^0.19.0 │            │ HashiCorp.Cdktf.Providers.Http       │ 8.0.0           │
├─────────────────────┼──────────────────┼─────────┼────────────┼──────────────────────────────────────┼─────────────────┤
│ cyrilgdn/postgresql │ 1.21.0           │ ^0.19.0 │            │ HashiCorp.Cdktf.Providers.Postgresql │ 10.0.0          │
├─────────────────────┼──────────────────┼─────────┼────────────┼──────────────────────────────────────┼─────────────────┤
│ random              │ 3.6.0            │ ^0.19.0 │            │ HashiCorp.Cdktf.Providers.Random     │ 10.0.1          │
└─────────────────────┴──────────────────┴─────────┴────────────┴──────────────────────────────────────┴─────────────────┘

Gist

No response

Possible Solutions

  • Use an exact match for the package in the check
  • Use the json output format when available (new in .net 7)

Workarounds

Add a direct reference to cdktf package

Anything Else?

Strangely this doesn't happen every time.

References

No response

Help Wanted

  • [X] 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

jsteinich avatar Dec 06 '23 19:12 jsteinich

Ouch, that's a very annoying bug 🙈 I guess we need to update the heuristic for detecting the package versions

DanielMSchmidt avatar Dec 08 '23 10:12 DanielMSchmidt

As a C# newbie, I'm having a bit of trouble following these reproduction steps. Here's what I did, please let me know where I went wrong, since I can't reproduce an issue.

  1. I created a new folder (test-3339)
  2. dotnet new sln
  3. Make the folders, CdktfHelper (library project) and Infra (Cdktf project)
  4. dotnet new classlib -o CdktfHelper
  5. dotnet sln add CdktfHelper/CdktfHelper.csproj and dotnet sln add Infra/MyTerraformStack.csproj
  6. Within the library, use cdktf by: dotnet add package HashiCorp.Cdktf
  7. Reference the library to Cdktf project by: dotnet add reference ../CdktfHelper/CdktfHelper.csproj

I can get cdktf synth to work with version 0.20.0.

Here's some additional information:

.
├── CdktfHelper
│   ├── Amazon.JSII.Runtime.1.93.0
│   ├── CdktfHelper.csproj
│   ├── Class1.cs
│   ├── Constructs.10.0.25
│   ├── HashiCorp.Cdktf.0.20.0
│   ├── ...
│   ├── Newtonsoft.Json.13.0.3
│   ├── System.Buffers.4.5.1
│   ├── ...
│   ├── bin
│   └── obj
├── Infra
│   ├── MainStack.cs
│   ├── MyTerraformStack.csproj
│   ├── Program.cs
│   ├── TestProgram.cs
│   ├── bin
│   ├── cdktf.json
│   ├── cdktf.out
│   ├── help
│   └── obj
└── test-3339.sln

34 directories, 9 files

CdktfHelper/CdktfHelper.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="HashiCorp.Cdktf" Version="0.20.0" />
  </ItemGroup>

</Project>

Infra/MyTerraformStack.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <GenerateProgramFile>false</GenerateProgramFile>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="HashiCorp.Cdktf" Version="0.20.0" />
    <PackageReference Include="HashiCorp.Cdktf.Providers.External" Version="10.0.0" />
    <PackageReference Include="HashiCorp.Cdktf.Providers.Http" Version="9.0.0" />
    <PackageReference Include="HashiCorp.Cdktf.Providers.Postgresql" Version="11.0.0" />
    <PackageReference Include="HashiCorp.Cdktf.Providers.Random" Version="11.0.0" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
    <PackageReference Include="xunit" Version="2.4.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\CdktfHelper\CdktfHelper.csproj" />
  </ItemGroup>

</Project>

CdktfHelper/MyLocal

namespace CdktfHelper;

using HashiCorp.Cdktf;

public static class MyLocal
{

    public static TerraformLocal CreateSpecialLocal(TerraformStack stack)
    {
        return new TerraformLocal(stack, "CdktfHelper-special", "foo");
    }
}

Infra/MainStack.cs

public static class StringLibrary
{
    public static bool StartsWithUpper(this string? str)
    {
        if (string.IsNullOrWhiteSpace(str))
            return false;

        char ch = str[0];
        return char.IsUpper(ch);
    }
}

When I synth this, I get:

terraform {
  required_providers {
    random = {
      version = "3.6.0"
      source  = "hashicorp/random"
    }
  }
  backend "local" {
    path = "/Users/mutahhir/src/scratchpad/csharp/test-3339/Infra/terraform.Infra.tfstate"
  }


}

locals {
  CdktfHelper-special = "foo"
}

provider "random" {
}
resource "random_pet" "my-pet" {
}

Is this problem resolved, or am I just doing it all wrong?

mutahhir avatar Jan 12 '24 13:01 mutahhir

Try removing the reference to cdktf from MyTerraformStack.csproj. That did seem to make a difference in the testing that I was doing.

I did observe that this issue was inconsistent, so I believe that the output of dotnet list package --include-transitive to be non-deterministic (or at least platform dependent).

jsteinich avatar Jan 12 '24 14:01 jsteinich

Hmm, that didn't seem to make any difference for me and also running dotnet list package (which we use for version checking) doesn't seem to have any non-determinism (I ran it a 100 times, and diffed the output). Can you provide me a bit more of a concrete example that I can clone and try to reproduce?

mutahhir avatar Jan 16 '24 16:01 mutahhir

Hi there! 👋 We haven't heard from you in 15 days and would like to know if the problem has been resolved or if you still need help. If we don't hear from you before then, I'll auto-close this issue in 30 days.

github-actions[bot] avatar Feb 04 '24 02:02 github-actions[bot]

I'm closing this issue because we haven't heard back in 45 days. ⌛️ If you still need help, feel free to comment or reopen the issue!

github-actions[bot] avatar Mar 06 '24 02:03 github-actions[bot]

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 Apr 06 '24 01:04 github-actions[bot]