andrewlock / StronglyTypedId

A Rosyln-powered generator for strongly-typed IDs
MIT License
1.53k stars 82 forks source link

[1.0.0-beta07] source generation fails with SDK 6 #125

Closed dbossard closed 9 months ago

dbossard commented 9 months ago

Thanks @andrewlock for the new release! I encountered an issue with source generation while trying to integrate this in existing projects :

with the following code

using StronglyTypedIds;

Console.WriteLine(new ExecutionId(1));

[StronglyTypedId(Template.Int)]
public partial struct ExecutionId
{
}

if SDK 6 is explicitly used as per global.json:

{
  "sdk": {
    "version": "6.0.0",
    "rollForward": "latestMinor",
    "allowPrerelease": false
  }
}

then build fails with:

error CS1729: 'ExecutionId' does not contain a constructor that takes 1 arguments 

if SDK 7 is used explicitly (or by specifying "rollForward": "latestMajor"):

{
  "sdk": {
    "version": "7.0.0",
    "rollForward": "latestMinor",
    "allowPrerelease": false
  }
}

then everything builds fine 🤔

andrewlock commented 9 months ago

Hi @dbossard - thanks for trying it out and for raising this. It's true, I can repro it with your example, and I get this warning:

CSC : warning CS8032: An instance of analyzer StronglyTypedIds.StronglyTypedIdGenerator cannot be created from C:\Users\Sock\.nuget\packages\stronglytypedid\1.0.0-beta07\analyzers\dotnet\
cs\StronglyTypedIds.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file spec
ified.. [C:\repos\temp\temp40\temp40.csproj]

Unfortunately (or fortunately, depending on how you look at it! 😅) I updated the generator to use the improved ForAttributeWithMetadataName() source generator method, introduced in .NET 7. This gives a significant perf boost to the generator itself, putting much less strain on the IDE. Unfortunately, as it was introduced in the .NET 7 SDK, the generator no longer supports the .NET 6 SDK 🙁

I confess, I didn't actually realise this was the case - it's really not clear that updating the version of Microsoft.CodeAnalysis.CSharp I use in my generator would have this effect, even though it makes sense in hindsight. To confirm, I searched inside the .NET 6 SDK folder on my PC and found the Microsoft.CodeAnalysis.* files here: C:\Program Files\dotnet\sdk\6.0.320\Roslyn\bincore, and sure enough, it includes version 4.2.0, whereas I reference 4.4.0.

Given you can still build for earlier TFMs (.NET 6 etc) with later versions of the SDK, I really don't want to support older versions of the SDK at this point, as it will definitely add complexity (I'd need to provide a ForAttributeWithMetadataName shim for the .NET SDK). If there are sufficient requests (or if someone does a PR for it) I may look into it, but otherwise I don't believe it's worth the effort.

Out of interest, why are you using the older version of the SDK instead of a newer one? 🤔

dbossard commented 9 months ago

Hi @andrewlock , thanks for the followup !

My using of SDK 6 was to work around an issue in Swashbuckle.AspNetCore.Cli 6.5.0 (https://stackoverflow.com/questions/77264929/generate-swagger-json-file-after-build-failed). We didn't make the jump to .net7 or 8 so this was a lazy solution that fitted our needs so far 😅.

But allowing recent SDKs and downgrading Swashbuckle.AspNetCore.Cli to 6.4.0 works perfectly, and allows me to use StronglyTypedIds 👍.

(for the long story: we produce swagger file at dev time and push it to github, so that other CODEOWNER teams - front, QA get notified of changes in PR; in addition, our PR workflow rebuilds it and fails if dirty, to ensure developers push up-to-date openapi spec.)