CommunityToolkit / dotnet

.NET Community Toolkit is a collection of helpers and APIs that work for all .NET developers and are agnostic of any specific UI platform. The toolkit is maintained and published by Microsoft, and part of the .NET Foundation.
https://docs.microsoft.com/dotnet/communitytoolkit/?WT.mc_id=dotnet-0000-bramin
Other
3.07k stars 299 forks source link

MVVM Toolkit Source Generator error Unsupported C# language version #613

Closed woz6 closed 1 year ago

woz6 commented 1 year ago

Describe the bug

MVVM Toolkit 8.1.0 Code Generator fails with error Unsupported C# language version.

The error description says: The source generator features from the MVVM Toolkit require consuming projects to set the C# language version to at least C# 8.0. Make sure to add <LangVersion>8.0</LangVersion> (or above) to your .csproj file.

I am running Visual Studio 2022, .NET 6.0 which should be using C# 10.0 by default. Adding <LangVersion>8.0</LangVersion> or <LangVersion>10.0</LangVersion> to the csproj file did not resolve the error.

I am trying to create a WinUI3 project, but have seen the same error reported on StackOverflow by someone who was creating a MAUI project.

This error does not appear when using MVVM Toolkit version 8.0.0

Regression

No response

Steps to reproduce

Windows 10
Visual Studio 2022, version 17.4.5
Create project Blank App, Packaged (WinUI3 in Desktop)
Update Nuget Packages to:
Microsoft.WindowsAppSDK  1.2.230118.102
Microsoft.Windows.SDK.BuildTools  10.0.22621.755

Install Nuget Package:
CommunityToolkit.Mvvm   8.1.0

In Solution Explorer Window, expand Dependencies->Analyzers->CommunityToolKit.Mvvm.SourceGenerators
Error messages are visible 

Source generators do not generate any code.

Expected behavior

There should be no C# language version error and Source Generators should generator code.

Screenshots

No response

IDE and version

VS 2022

IDE version

17.4.5

Nuget packages

Nuget package version(s)

8.1.0

Additional context

No response

Help us help you

No, just wanted to report this

woz6 commented 1 year ago

Misunderstood what I was looking at. Seems to be working. Sorry for any time wasted.

stevehemond commented 1 year ago

This seems to be an issue.

I have the same exact problem using version 8.1.0.

Version 8.1.0 gives me the "Unsupported C# language version" message.

Then, as soon as I add a new property inside a class that makes use of the mvvm attributes, the source generator seems to fail as intellisense underlines some existing observable properties as missing.

Version 8.0.0 works fine though : no error message at all under the source generator node and the source generators don't fail when I add code in classes.

Sergio0694 commented 1 year ago

Would you be able to share a minimal repro?

stevehemond commented 1 year ago

These are work related repos and not shareable.

I've created a new WinUI3 project from scratch and added a few observable objects to make use of the attributes.

I've imported the 8.0.1 version.

I still get the error messages :

image

The difference in this case is that, unlike my work related projects, the source generators doesn't randomly fail when I add properties throughout files. These work related projects have many other references and I'm not sure how one of them could make the source generators fail ?

Sergio0694 commented 1 year ago

Those are not error messages. That's just VS showing you that the generator can produce those diagnostics, as they're declared in the various analyzers. But those being there is not an actual warning at all 🙂

stevehemond commented 1 year ago

Ok, so, why these warnings are not showing up when using the 8.0.0 version ?

Also, that makes me really wonder why the source generators from the 8.0.0 doesn't fail while the 8.0.1 fails ...

Sergio0694 commented 1 year ago

Because in 8.0 all those diagnostics are produces by source generators, whereas in 8.1 we moved most of them to diagnostic analyzers (eg. see #433 and #581). The benefit is that diagnostic analyzers run out of process and asynchronously, so moving the diagnostics there help keep the IDE faster and more responsive, as the generators have less work to do (since they run synchronously as you type instead). As a side bonus, diagnostic analyzers explicitly declare all diagnostics they can potentially produce, so VS also shows all of them in that list, which also gives you the ability to optionally override the severity of each of them, if you want. So it's both faster and more configurable 🙂

Sergio0694 commented 1 year ago

"makes me really wonder why the source generators from the 8.0.0 doesn't fail while the 8.0.1 fails"

That might be related to #493, which is a separate issue.