louthy / language-ext

C# functional language extensions - a base class library for functional programming
MIT License
6.51k stars 420 forks source link

Plans to replace CodeGeneration.Roslyn dependency with Source Generators (introduced in .NET 5) in LanguageExt.CodeGen? #875

Closed Malgefor closed 2 weeks ago

Malgefor commented 3 years ago

Hi!

I am using the LanguageExt.CodeGen package to generate Discriminated Unions. However, the dependency https://github.com/AArnott/CodeGeneration.Roslyn is marked as deprecated in the repo.

Are there plans to use the Source Generators (https://devblogs.microsoft.com/dotnet/introducing-c-source-generators/) for the CodeGen package?

Cheers

Malgefor commented 3 years ago

To elaborate a bit more on why the dependency on the deprecated package isn't great: if you want to run the codegen tool in your build you need to .NET Core 2.1 SDK installed on the machine / Docker image to be able to build. So when building a .NET 5 project, you still need 2.1.

If you don't have the 2.1 SDK you will get errors along the lines of:

.nuget/packages/codegeneration.roslyn.buildtime/0.6.1/build/CodeGeneration.Roslyn.BuildTime.targets(61,5): warning MSB3073: The command "dotnet codegen --version" exited with code 150.

.nuget/packages/codegeneration.roslyn.buildtime/0.6.1/build/CodeGeneration.Roslyn.BuildTime.targets(61,5): warning MSB4181: The "Exec" task returned false but did not log an error.

.nuget/packages/codegeneration.roslyn.buildtime/0.6.1/build/CodeGeneration.Roslyn.BuildTime.targets(73,5): error CGR1001: CodeGeneration.Roslyn.Tool (dotnet-codegen) is not available, code generation won't run. Please check https://github.com/AArnott/CodeGeneration.Roslyn for usage instructions.

To fix this you have to install the .NET Core 2.1 SDK, which goes out of support August 2021. (https://dotnet.microsoft.com/platform/support/policy/dotnet-core)

Example config of installing the 2.1 SDK in a Docker file: FROM mcr.microsoft.com/dotnet/sdk:5.0-focal AS build COPY --from=mcr.microsoft.com/dotnet/sdk:2.1-focal /usr/share/dotnet /usr/share/dotnet

harrhp commented 3 years ago

you should not need 2.1 sdk. try to define roll forward policy https://github.com/AArnott/CodeGeneration.Roslyn/issues/180#issuecomment-562895670

Malgefor commented 3 years ago

Ah you are right! Thanks for pointing that out : ) Missed that when I was trying to get the codegen tool to work.

louthy commented 3 years ago

Regarding the original question, yes, I will be updating the CodeGen to use Source Generators. I'm super overloaded with other projects at the moment, so I'm having to keep my interaction with LangExt at a minimum for a while, so it probably won't be for a month or so.

gabomgp4 commented 3 years ago

you should not need 2.1 sdk. try to define roll forward policy

In my case, it was required to install the runtime 2.1. Installing the SDK was not enough.

bmargula commented 2 years ago

Hi, I'm totally new to LanguageExt and tried to setup the codegen functionality as mentioned in the wiki. But I do have the same problem as other in that I can't get it to work with my .NET6 SDK. I saw there was no activity here for 6 months. Is this still on the roadmap, @louthy ?

StefanBertels commented 2 years ago

@bmargula: Installing .NET 2.1 (SDK or runtime, don't know) on your developer machine (and your build server) should be enough to make codegen working as documented. This is not a runtime dependency for your binaries. Your project can target e.g. net60. It's working fine with latest LanguageExt, too.

Malgefor commented 2 years ago

you should not need 2.1 sdk. try to define roll forward policy AArnott/CodeGeneration.Roslyn#180 (comment)

Have you tried this? After I set the rollforwardpolicy as an environment variable it worked without having to install the 2.1 sdk/runtime

louthy commented 2 weeks ago

https://github.com/louthy/language-ext/wiki/Frequently-Asked-Questions#why-doesnt-languageextcodegen-work