dotnet
dotnet copied to clipboard
Results of the MVVM source generator are not always compiled
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
- Create a C# desktop application project within Visual Studio 17.7.2 based on WPF/XAML/.NET Framework 4.6.2.
- Add a nuget package MvvmToolkit v8.2.1
- 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
- [ ] CommunityToolkit.Common
- [ ] CommunityToolkit.Diagnostics
- [ ] CommunityToolkit.HighPerformance
- [X] CommunityToolkit.Mvvm (aka MVVM Toolkit)
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.
having the same problem with .Net Framework 4.7.2 and CommunityToolkit.Mvvm 8.2.1 try to build and fail as shown below
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?
#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?
A workaround:
- Download the ".NET Upgrade Assistent" and "Upgrade project features".
- Check all project settings
- If you use reference paths: Add these values manually into *.csproj.user and extend "AssemblySearchPaths". Example: <ReferencePath>MyPath1;MyPath2</ReferencePath> <AssemblySearchPaths>$(ReferencePath);$(AssemblySearchPaths)</AssemblySearchPaths>
- If you use build events with macro usage: Most of macros will probably be empty. Remove and add the event script. It will be moved to the end of the csproj-file within a tag "target".
- All comments within the csproj might be removed. Compare and restore such comments if needed.
- In my case, the property "Prefer32Bit" was not set and caused exceptions on loading dependend libs. I changed the target to "Any CPU" and set "prefer 32 bit". After that I changed the target back to "x86". That seems to work for me.
VS17.7.3 + MvvmToolkit8.2.1 don't encounter this bug anymore!
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.