dotnet / docs

This repository contains .NET Documentation.
https://learn.microsoft.com/dotnet
Creative Commons Attribution 4.0 International
4.13k stars 5.8k forks source link

Source Generators: Necessity to target `netstandard2.0`? #40603

Closed jacobjmarks closed 1 week ago

jacobjmarks commented 2 weeks ago

Type of issue

Missing information

Description

When authoring a source generator, documentation states that "The source generator project needs to target the netstandard2.0 TFM, otherwise it will not work.". However, in my testing I was able to create a basic source generator — following the current documentation — targeting net8.0 with seemingly no issues at all; this using Visual Studio Code and the .NET CLI with SDK 8.0.204.

Is this documentation out of date? Is it only applicable in certain scenarios? More information here would be very much appreciated.

Possible related issue: #40058

Page URL

https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview

Content source URL

https://github.com/dotnet/docs/blob/main/docs/csharp/roslyn-sdk/source-generators-overview.md

Document Version Independent Id

f1ed9873-3e29-b4bd-2dbb-44dd9a05d790

Article author

@BillWagner

Metadata

BillWagner commented 2 weeks ago

adding @KathleenDollard to comment.

Youssef1313 commented 2 weeks ago

You shouldn't really use net8.0 for roslyn components (analyzers, codefixes, or generators).

The thing is that Visual Studio is still .NET Framework, while dotnet CLI is .NET Core. So for a roslyn component to work in both, it must target netstandard2.0.

In your case, it has possibly worked because you were in a .NET Core environment. If you tried that in Visual Studio, it shouldn't work.

Youssef1313 commented 2 weeks ago

And from C# Discord:

image

jacobjmarks commented 2 weeks ago

Thanks, that's all understandable. Though for the documentation here — at least on this page — to state that it just "will not work" is not very helpful to anyone; because depending on what you're doing, it can and will. Some additional context should be added.