Roslyn: Source generators: Net Framework compatibility

Created on 2 May 2020  ·  7Comments  ·  Source: dotnet/roslyn

We tried the new source generators feature present in Visual Studio 16.6 preview 5 and it works like a charm for our POCs.

We are planning to remove all heavy components using reflection like AutoMapper for all our projects.

We have a mix of net standard and net framework 4.7.1 projects, so we have to support both of them and today the source generators works without any problems, especially for net framework projects...

But when we looked at the planning of @chsienki, we find a card stating to limit the functionnality to only netstandard / netcoreapp projects
https://github.com/dotnet/roslyn/projects/54#card-34860099

Why this limitation ? and could you consider to let a minimal support for net framework for such a great feature ?

Area-Compilers New Feature - Source Generators

Most helpful comment

It is currently technically possible to load a generator compiled against e.g, net472 but it's an unsupported scenario

In fact, we plan to report a warning if an analyzer or generator targets desktop framework in future. This is because in future the compiler and the IDE will run all analyzers on .NET Core/.NET 5. If the analyzer used any features from .NET Framework that are not available on .NET 5 it will fail to load or to run.

All 7 comments

@u7pro I'm not on the team, but source generators seem to be slated for the .Net 5 release which is the merging of .Net framework and .Net Core. Also, since .Net framework is not being updated with features going forward, I can't see the team adding source generators to .Net framework.

Also, since this is a compiler feature and you can still target full framework with the dotnet tooling, i think you would be able to still use source generators, if not for the whole solution, at least for your shared .net standard libraries. we targeted full framework with the new sdk system for a while without issues

@flipchart, @aL3891 :
the source generators feature is not related to .net 5, it's a roslyn feature and you can test it without installing .net 5, it's present with the latest preview of roslyn included in VS 16.6 preview 5

and as mentionned in the RunGenerators method, it's agnostic to the language version, which means, it will not require any specific c# version :
https://github.com/dotnet/roslyn/blob/bc2d6a2597d07a574be4e292f9dbd9c8f975fd7f/src/Compilers/CSharp/Portable/CommandLine/CSharpCompiler.cs#L404

i asked yesterday @jaredpar in twitter about compatibilty of this feature with compiled projects that are targeting net fx, and he said that's fine
https://twitter.com/jaredpar/status/1257337162716020737

and i think that the card of @chsienki stating to limit support to netstandard / netcoreapp is specific to source generators, not projects that are being compiled..

Yep, the above is correct.

At release source generators will be able to target any runtime / language version. We're currently gating behind language version == preview so that users of Source Generators have to explicitly opt into them and understand they are an in progress feature.

The generator itself must be compiled as a netstandard / netcoreapp assembly. It is currently technically possible to load a generator compiled against e.g, net472 but it's an unsupported scenario. The card item mentioned above is to actively block loading a generator compiled against an incorrect framework.

Hopefully that clears up any confusion?

Yes, it's clear ! by the way, you have done a great job, thank you

It is currently technically possible to load a generator compiled against e.g, net472 but it's an unsupported scenario

In fact, we plan to report a warning if an analyzer or generator targets desktop framework in future. This is because in future the compiler and the IDE will run all analyzers on .NET Core/.NET 5. If the analyzer used any features from .NET Framework that are not available on .NET 5 it will fail to load or to run.

@u7pro

the source generators feature is not related to .net 5

Thanks, good to know. The blog post announcing source generators says you need the .Net 5 preview, but obviously that's a mistake 🤷‍♂️

Was this page helpful?
0 / 5 - 0 ratings