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

Results of the MVVM source generator are not always compiled #750

Open mscholz5 opened 1 year ago

mscholz5 commented 1 year ago

Describe the bug

I have a solution consisting of multiple projects. Some are libraries and some are desktop applications based on WPF/XAML. All projects use the code MVVM source generator.

The build of the desktop applications fail with CS0103 in the output window. The build of the library projects works.

In both cases does IntelliSense not show any errors and I also find the generated code under "Analyse".

"CompilerGeneratedFilesOutputPath" and "EmitCompilerGeneratedFiles" also work only for the library projects, but not for the app projects. Setting "IncludePackageReferencesDuringMarkupCompilation" to "true" does not solve anything.

Steps to reproduce

  1. Create a C# desktop application project within Visual Studio 17.7.2 based on WPF/XAML/.NET Framework 4.6.2.
  2. Add a nuget package MvvmToolkit v8.2.1
  3. Add the following code
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace example
{
    internal partial class MyClass : ObservableObject
    {
        [ObservableProperty]
        int _foo; // warning CS0169

        [RelayCommand]
        void Bar()
        {
            Foo = 1;  // error CS0103
        }
    }
}

Expected behavior

I expect, that the code generator is working also for the WPF/XAML projects.

IDE and version

VS 2022

IDE version

17.7.2

Nuget packages

Nuget package version(s)

8.2.1

Help us help you

I could support, but I do not know how to start. It looks as if the compiler discards the generated sources under unknown circumstances. Perhaps this is a topic for the Microsoft VS-Team.

ahmad-syafrudin commented 1 year ago

having the same problem with .Net Framework 4.7.2 and CommunityToolkit.Mvvm 8.2.1 try to build and fail as shown below

image

image

Sergio0694 commented 1 year ago

Duplicate of #643 or #158 I think?

Are you using packages.config, by any chance? If not, is all your code in the same project as your app, and are you using a legacy-style .csproj file?

mscholz5 commented 1 year ago

158 looks indeed like a duplicate, which I have not seen before.

I do not have a packages.config within the project. The MVVM Toolkit is added with a PackageReference-Tag. The original csproj was created with the template "WPF App (.NET Framework)". Does this qualify as "legacy-style"?

I do not understand the answer of Chris Sienkiewicz. Is there way to upgrade the csproj style without changing the target framework?

mscholz5 commented 1 year ago

A workaround:

CodingOctocat commented 1 year ago

VS17.7.3 + MvvmToolkit8.2.1 don't encounter this bug anymore!

Yuval-pa commented 1 year ago

VS17.7.3 + MvvmToolkit8.2.1 don't encounter this bug anymore!

using VS17.7.5 and MvvmToolKit8.2.1 to create a .Net Framework 4.8 WPF project. I migrated to PackageReference, changed the language version to c# 8 and was able to reproduce this error. After I read this issue I switched to MvvmToolKit8.0.0 and the problem remained unresolved.