Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CommunityToolkit.Mvvm source generators fail on Visual Studio 17.4.0 Preview 6 and 17.5.0 Preview 1 #493

Closed
1 of 4 tasks
nxtn-staged opened this issue Nov 9, 2022 · 122 comments
Labels
external ⤴️ Something related to or caused by an external project mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit

Comments

@nxtn-staged
Copy link

nxtn-staged commented Nov 9, 2022

Note: this is an external issue in Roslyn, see: dotnet/roslyn#67123.

Describe the bug

I recently updated Visual Studio to 17.5.0 Preview 1.0 and my project starts to fail to build.

1>CSC : warning CS8785: Generator 'INotifyPropertyChangedGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'MissingMethodException' with message 'Method not found: 'System.ReadOnlySpan`1<System.__Canon> System.Collections.Immutable.ImmutableArray`1.AsSpan()'.'
1>CSC : warning CS8785: Generator 'ObservablePropertyGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'MissingMethodException' with message 'Method not found: 'System.ReadOnlySpan`1<System.__Canon> System.Collections.Immutable.ImmutableArray`1.AsSpan()'.'

Regression

No response

Steps to reproduce

  1. Add project files
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="CommunityToolkit.Mvvm" Version="8.1.0-preview1" />
  </ItemGroup>
  
</Project>
using CommunityToolkit.Mvvm.ComponentModel;

[INotifyPropertyChanged]
partial class Foo
{
    [ObservableProperty]
    private int bar;
}

static class Program
{
    static void Main()
    {
        new Foo().Bar = 42;
    }
}
  1. Build in Visual Studio

Expected behavior

It works.

Screenshots

No response

IDE and version

VS 2022 Preview

IDE version

No response

Nuget packages

  • CommunityToolkit.Common
  • CommunityToolkit.Diagnostics
  • CommunityToolkit.HighPerformance
  • CommunityToolkit.Mvvm (aka MVVM Toolkit)

Nuget package version(s)

8.1.0-preview1

Additional context

I can't always reproduce this. I'm also not sure if it's a problem in this project, but my other source generator dependencies are not affected.

Help us help you

Yes, but only if others can assist

@nxtn-staged nxtn-staged added the bug 🐛 An unexpected issue that highlights incorrect behavior label Nov 9, 2022
@nxtn-staged nxtn-staged changed the title CommunityToolkit.Mvvm fails on Visual Studio 17.4.0/17.5.0 Preview CommunityToolkit.Mvvm source generators fail on Visual Studio 17.4.0/17.5.0 Preview Nov 9, 2022
@Sergio0694 Sergio0694 added the mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit label Nov 9, 2022
@Sergio0694
Copy link
Member

Mmh this is weird. Just to clarify:

  • Does this also repro on VS 17.4?
  • Does this repro on 8.0.0 as well, or just on 8.1.0-Preview1?

@nxtn-staged
Copy link
Author

nxtn-staged commented Nov 10, 2022

It first reproduced when I updated Visual Studio to the 17.4.0 Preview 6 and some package dependencies. Downgrading CommunityToolkit.Mvvm from 8.1.0-Preview1 to 8.0.0 didn't help, and I managed to work around it by downgrading Microsoft.Windows.CsWin32 from 0.2.104-beta to 0.2.63-beta 😕. After updating VS to 17.5.0 Preview 1 it doesn't work anymore.

@arkadym
Copy link

arkadym commented Nov 10, 2022

Im stuck with below errors after upgrade

1>...\CommunityToolkit.Maui.Markup.SourceGenerators\CommunityToolkit.Maui.Markup.SourceGenerators.TextAlignmentExtensionsGenerator\EditorTextAlignmentExtensions.g.cs(122,147,122,167): error CS0121: The call is ambiguous between the following methods or properties: 'CommunityToolkit.Maui.Markup.TextAlignmentExtensions_Editor.TextCenterHorizontal(Microsoft.Maui.Controls.Editor)' and 'CommunityToolkit.Maui.Markup.TextAlignmentExtensions_Editor.TextCenterHorizontal(Microsoft.Maui.Controls.Editor)'
1>...\CommunityToolkit.Maui.Markup.SourceGenerators\CommunityToolkit.Maui.Markup.SourceGenerators.TextAlignmentExtensionsGenerator\EntryTextAlignmentExtensions.g.cs(122,145,122,165): error CS0121: The call is ambiguous between the following methods or properties: 'CommunityToolkit.Maui.Markup.TextAlignmentExtensions_Entry.TextCenterHorizontal(Microsoft.Maui.Controls.Entry)' and 'CommunityToolkit.Maui.Markup.TextAlignmentExtensions_Entry.TextCenterHorizontal(Microsoft.Maui.Controls.Entry)'
1>...\CommunityToolkit.Maui.Markup.SourceGenerators\CommunityToolkit.Maui.Markup.SourceGenerators.TextAlignmentExtensionsGenerator\LabelTextAlignmentExtensions.g.cs(122,145,122,165): error CS0121: The call is ambiguous between the following methods or properties: 'CommunityToolkit.Maui.Markup.TextAlignmentExtensions_Label.TextCenterHorizontal(Microsoft.Maui.Controls.Label)' and 'CommunityToolkit.Maui.Markup.TextAlignmentExtensions_Label.TextCenterHorizontal(Microsoft.Maui.Controls.Label)'
1>...\CommunityToolkit.Maui.Markup.SourceGenerators\CommunityToolkit.Maui.Markup.SourceGenerators.TextAlignmentExtensionsGenerator\PickerTextAlignmentExtensions.g.cs(122,147,122,167): error CS0121: The call is ambiguous between the following methods or properties: 'CommunityToolkit.Maui.Markup.TextAlignmentExtensions_Picker.TextCenterHorizontal(Microsoft.Maui.Controls.Picker)' and 'CommunityToolkit.Maui.Markup.TextAlignmentExtensions_Picker.TextCenterHorizontal(Microsoft.Maui.Controls.Picker)'

@nxtn-staged nxtn-staged changed the title CommunityToolkit.Mvvm source generators fail on Visual Studio 17.4.0/17.5.0 Preview CommunityToolkit.Mvvm source generators fail on Visual Studio 17.4.0 Preview 6 and 17.5.0 Preview 1 Nov 11, 2022
@huoyaoyuan
Copy link

It reproduces for VS 17.4 stable, for version 8.0.0 and 8.1.0-preview1 both.

@huoyaoyuan
Copy link

In decompilation, the System.Collections.Immutable.dll in .NET Framework version of VS at MSBuild\Current\bin\Roslyn does have the method AsSpan(). So does the one at MSBuild\Current\bin.
I have totally no idea why it can cause MissingMethodException.

A workaround is to run dotnet build once in command line to use the .NET Core version of compiler, as long as it's not a WinUI project (or the vm project has been separated from UI).

@Sergio0694
Copy link
Member

Mmh this is weird. I can't repro locally with VS 17.4, but the fact people can with 8.0 stable is worrying.

@sharwell @chsienki do you have any idea what could be causing this? 😅

@JPhilC
Copy link

JPhilC commented Nov 16, 2022

Yesterday I was trying to work out why the source generators sometimes worked with WPF (,NET Framework 4.7.1) App and sometimes didn't. This morning I update VS 2022 to 17.4.1 and the source generators have stopped working completely.

Testing with the following code in a class definition.

using CommunityToolkit.Mvvm.ComponentModel;

namespace WpfApp4
{
    public class MainWindowModelview: ObservableObject
    {
        [ObservableProperty]
        private string _firstName = "Phil";

        
    }
}

The solution builds but then the expected property FirstName is not generated by the source generator. If I look under References\Analyzers the Analyzers node is not expandable.

Steps to reproduce,

Create a new WPF Desktop App (.NET Framework 4.7.1)
Add nuget V8.1.0 ..Preview 1
Add class MainMenuModelview with above definition.

Build.

Additional info...

If I convert packages.conf to ReferencedPackages and include transitive assemblies by ticking the top level box then the source generation starts but it leads to a whole load of messages at the top of the Visual Studio IDE.

image

and the following build errors.

image

The stack trace of the first messages is:

System.ArgumentException : An element with the same key but a different value already exists. Key: '(DocumentId, #a64613e5-d79c-9c32-3ac4-a27dcc380fd0 - System.Diagnostics.CodeAnalysis.NotNullAttribute.g.cs)'
   at System.Collections.Immutable.ImmutableSortedDictionary`2.FillFromEmpty(IEnumerable`1 items,Boolean overwriteOnCollision)
   at System.Collections.Immutable.ImmutableSortedDictionary`2.AddRange(IEnumerable`1 items,Boolean overwriteOnCollision,Boolean avoidToSortedMap)
   at System.Collections.Immutable.ImmutableSortedDictionary.ToImmutableSortedDictionary[TSource,TKey,TValue](IEnumerable`1 source,Func`2 keySelector,Func`2 elementSelector,IComparer`1 keyComparer)
   at Microsoft.CodeAnalysis.TextDocumentStates`1..ctor(IEnumerable`1 states)
   at async Microsoft.CodeAnalysis.SolutionState.CompilationTracker.FinalizeCompilationAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.SolutionState.CompilationTracker.BuildCompilationInfoAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.SolutionState.CompilationTracker.GetOrBuildCompilationInfoAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.SolutionState.CompilationTracker.GetCompilationSlowAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.Document.GetSemanticModelAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.SemanticDocument.CreateAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.IntroduceVariable.AbstractIntroduceVariableService`6.IntroduceVariableAsync[TService,TExpressionSyntax,TTypeSyntax,TTypeDeclarationSyntax,TQueryExpressionSyntax,TNameSyntax](<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.IntroduceVariable.IntroduceVariableCodeRefactoringProvider.ComputeRefactoringsAsync(<Unknown Parameters>)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at async Microsoft.CodeAnalysis.CodeRefactorings.CodeRefactoringService.GetRefactoringFromProviderAsync(<Unknown Parameters>)

In all there are 12 messages they all seem to have the same stack trace.

'IntroduceVariableCodeRefactoringProvider' encountered an error and has been disabled.
'CSharpConvertConcatenationToInterpolatedStringRefactoringProvider' encountered an error and has been disabled.
'CSharpConvertPlaceholderToInterpolatedStringRefactoringProvider' encountered an error and has been disabled.
'CSharpConvertIfToSwitchCodeRefactoringProvider' encountered an error and has been disabled.
'CSharpPullMemberUpCodeRefactoringProvider' encountered an error and has been disabled.
'CSharpMoveStaticMembersRefactoringProvider' encountered an error and has been disabled.
'CSharpExtractClassCodeRefactoringProvider' encountered an error and has been disabled.
'CSharpAddDebuggerDisplayCodeRefactoringProvider' encountered an error and has been disabled.
'MoveTypeCodeRefactoringProvider' encountered an error and has been disabled.
'MoveToNamespaceCodeActionProvider' encountered an error and has been disabled.
'GenerateDefaultConstructorsCodeRefactoringProvider' encountered an error and has been disabled.
'EncapsulateFieldRefactoringProvider' encountered an error and has been disabled.

@jansaris
Copy link

jansaris commented Dec 2, 2022

On our build environment at the customer we also have this issue.
It's a build-flipper, since it sometime causes the build to fail and sometimes to succeed

We can't see any difference in the generated csproj file when looking at the msbuild binlog file :-(

@Sergio0694
Copy link
Member

Given that there's a whole bunch of inbox analyzers also failing for the same reason, this seems unrelated from the MVVM Toolkit specifically. Also note that the generators have known issue with non-SDK-style .NET Framework projects. Does the issue still repro for you if you switch to an SDK-style .csproj format?

@Sergio0694 Sergio0694 added the needs author feedback 📝 This issue or PR needs a reply from the author label Dec 8, 2022
@Lightczx
Copy link

Lightczx commented Dec 11, 2022

I'm using .NET 7, VS2022 17.4.2, SDK style project

Still can reproduce these,@Sergio0694
Everything works fine before I upgrade my Microsoft.Windows.CsWin32

<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.2.138-beta">
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.2.164-beta">

@virzak
Copy link

virzak commented Dec 12, 2022

I have the same issue which is also intermittent and I have no idea what triggers it and how it gets resolved.

My error message happens with RelayCommand:

5>CSC : warning CS8785: Generator 'RelayCommandGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'MissingMethodException' with message 'Method not found: 'System.ReadOnlySpan`1<!0> System.Collections.Immutable.ImmutableArray`1.AsSpan()'.'

There is nothing that seems to be a proven workaround if using VS. Deleting all obj / bin folders does not help. The only thing that helps is switching to VS Code and after a while switching back to VS.

I also use Microsoft.Windows.CsWin32, but not sure this has anything to do with this problem.

I have an SDK-style .NET 7 project where this happens.

@Lightczx
Copy link

@virzak I solved this by downgrade the Cswin32 version, save and close proj, reopen vs ,and then manually upgrade back by editing the csproj file.
After reopen vs, everything works.

@Sergio0694
Copy link
Member

This is... Uh... Weird. I don't think different generators should/could interfere with one another 🤔

FYI @sharwell

@Lightczx
Copy link

@Sergio0694
Copy link
Member

Sweet, thanks! I'll close this as external then 😄

@Sergio0694 Sergio0694 added external ⤴️ Something related to or caused by an external project and removed bug 🐛 An unexpected issue that highlights incorrect behavior needs author feedback 📝 This issue or PR needs a reply from the author labels Dec 14, 2022
@hymccord
Copy link
Contributor

I still don't think this is fixed yet. I'm still getting the same error on a personal project of mine.

Installed v17.4.3 in a Windows Sandbox since I normally use the preview versions (and was still seeing the error in 17.5p2)

CommunityToolkit: v8.1.0-preview2
Microsoft.Windows.CsWin32: v0.2.164-beta

1>CSC : warning CS8785: Generator 'RelayCommandGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'MissingMethodException' with message 'Method not found: 'System.ReadOnlySpan`1<System.__Canon> System.Collections.Immutable.ImmutableArray`1.AsSpan()'.'

@Sergio0694
Copy link
Member

Just to clarify:

  • Does this only happen in a .NET Framework project or any project?
  • Does this only happen if you also reference CsWin32?

@hymccord
Copy link
Contributor

hymccord commented Dec 19, 2022

I'm putting together a minimal repro now.

@Lightczx
Copy link

Exactly Microsoft.Windows.CsWin32: v0.2.164-beta.
Lower version like 0.2.138 won't cause any problem.

@hymccord
Copy link
Contributor

hymccord commented Dec 19, 2022

Only with CsWin32, above version 0.2.138-beta. I'll post and issue over there as well.

Edit: As stated below, it may be an even lower version but the warnings may not be as evident. CsWin32 updated System.Memory to 4.5.5 on their public beta version 0.2.104-beta.

Quick repo

> mkdir CTRepro
> cd CTRepro
> dotnet new wpf
> dotnet add package CommunityToolkit.Mvvm -v 8.1.0-preview2
> dotnet add package Microsoft.Windows.CsWin32 -v 0.2.164-beta

Then add a .cs file so a RelayCommand will generate

using CommunityToolkit.Mvvm.Input;

namespace CTRepro;

internal partial class MainWindowViewModel
{
    [RelayCommand]
    private void Execute()
    { }
}

You can do either one of these. MSBuild will at least warn you something went wrong.

msbuild /t:Rebuild

or

dotnet build

What's even worse is that dotnet build will say it compiled successfully (with NO warning), yet if you inspect the dll with ILSpy or dnSpy, the ExecuteCommand will NOT exist.

Here's the binlogs from dotnet and msbuild.

binlogs.zip

@Sergio0694
Copy link
Member

This is... Very weird. I would say I have no idea what's going on. It would seem like CsWin32 is somehow interfering with the toolkit, but even then... How? Like, maybe a .targets that's accidentally interfering with other analyzers? 🤔

Marking this as external for now as it does seem unrelated to the MVVM Toolkit.

@codengine
Copy link

codengine commented May 16, 2023

@codengine What is the exact warning message produced by the IDE when you observe the problem? It should say something specific to an exception occurring in the source generator.

Maybe I was too quick with my judgement. So here is what I observed.
I went through the Roslyn Analyzer warnings and saw a couple of places where I had [ObservableProperty] on a property and then used the private _property instead of referencing the generated one. This happened a couple of times.

Then there was one place where I had something like this:
Class Name: ScrewStructure
Property: [ObservableProperty] ScrewStructure _screwStructure
I think this example could lead to the code generation to fail as the generated Property name is equal to the class name (ScrewStructure in this example).

To make sure there was no other influence, I restarted my computer, cleaned the whole temp directory, build the solution from scratch and for now, the Analyzer is happy.

I then created a test project which contains the scenario in isolation.

public partial class TestClass: ObservableObject
{
    [ObservableProperty]
    private AnotherClass _anotherClass;

    public void Foo()
    {
        if (AnotherClass != null)
        {
            
        }
    }
}

First, Roslyn was not happy (unfortunately Rider is a bit hard to debug in that regard).
Then I renamed the property to "Another", Roslyn was happy again, I renamed it back to "AnotherClass" and Roslyn was still happy.

So in the end, it could have been me who accidentally had the Class Name as the Property Name in the generated source.
I will keep and eye on that and will report back as soon I notice something else.

@polymorphicshade
Copy link

@Sergio0694 I've updated everything on my end, and I'm no longer seeing the problem! However I think I have to play around with it some more to confirm.

@Balkoth
Copy link

Balkoth commented May 24, 2023

I'm on 8.2.0 and VS 17.6.1 and the problem is still there.

@sharwell
Copy link

sharwell commented May 24, 2023

I'm on 8.2.0 and VS 17.6.1 and the problem is still there.

@Balkoth can you provide some specific messages you are observing with these versions when the errors appear?

@failwyn
Copy link

failwyn commented May 25, 2023

I'm on 8.2.0 and VS 17.6.1 and the problem is still there.

@Balkoth can you provide some specific messages you are observing with these versions when the errors appear?

I'm experiencing the same thing; where can I look for the messages? The source generator just appears to stop working; restarting Visual Studio fixes it for a short time, but I'm restarting at least a dozen times a day.

@polymorphicshade
Copy link

polymorphicshade commented May 31, 2023

@Sergio0694 update: after several days of use, I can confirm the issue is still here.

Over a small period of time, I can code normally and never see this issue. However, some time after that I start seeing the problem again.

Now just like before, as soon as I do anything related to the CommunityToolkit.Mvvm (even as simple as placing [RelayCommand] over a private method), intellisense completely breaks in both XAML and CS code.

Still, for me, the only temporary solution to this problem is to clean the solution, close Visual Studio, manually delete all my bin and obj folders, and re-launch Visual Studio.

I'm using the latest release version of Visual Studio and CommunityToolkit.Mvvm.

@balthasart
Copy link

@Sergio0694 update: after several days of use, I can confirm the issue is still here.

Same thing for me - however, as all my mvvm related code is on a separate library project - I juste need to "Unload Project" and reload it, then every thing is fine (no need to quit VS, delete bin and obj folders)

@heartacker
Copy link

the important thing is it stops the hot reload :cry

@jaredpar
Copy link

jaredpar commented Jun 1, 2023

Based on the feedback it seems like we addressed part of the problem but not all of it here. Unfortunately the inability to have a reliable repro is making it difficult to track down this issue. It's not even clear right now if there is a bug in the roslyn side or in the mvvm generator.

One item we're going to try on our end is expand our diagnostic capabilities:

Taken together those two issues should let us view the internal mechanics of generators if you submit an ETL trace. That should be integrated into the VS feedback system such that it can be automated to a large degree.

Our goal is to get this all implemented in time for VS 17.7 Preview 3 or Preview 4. Hopefully that's in time for you all to send us ETL traces with the expanded diagnostics and we can track down where the remaining issues are.

@Sergio0694
Copy link
Member

@jaredpar I have a (pretty consistent) repro!! 🎉

  1. Checkout to jenius-apps/ambie@375e076
  2. Open the solution (needs the UWP SDK installed as well, just let VS install anything missing)
  3. Open SoundViewModel.cs
  4. Scroll down to DeleteSound() (line 373)
  5. Delete the line with [RelayCommand] over the method
  6. Add the attribute back (eg. by manually typing it, or pasting, doesn't seem to matter)

With these steps, I can pretty consistently get the generator to crash. It doesn't always happen though. One time it didn't work, but after closing and reopening VS, doing the same steps did reproduce the issue. Happens almost all the times.

Error message:

Severity Code Description Project File Line Suppression State Detail Description
Warning CS8785 Generator 'RelayCommandGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'ArgumentException' with message 'The hintName 'AmbientSounds.ViewModels.SoundListViewModel.LoadAsync.g.cs' of the added source file must be unique within a generator.
Parameter name: hintName' AmbientSounds C:\Users\sergiopedri\git\ambie\src\AmbientSounds\AmbientSounds.csproj 1 Active Generator threw the following exception:
'System.ArgumentException: The hintName 'AmbientSounds.ViewModels.SoundListViewModel.LoadAsync.g.cs' of the added source file must be unique within a generator.
Parameter name: hintName
at Microsoft.CodeAnalysis.AdditionalSourcesCollection.Add(String hintName, SourceText source)
at Microsoft.CodeAnalysis.SourceOutputNode`1.AppendOutputs(IncrementalExecutionContext context, CancellationToken cancellationToken)'.

Also happy to share more from my end if you need (eg. if there's a way to collect more telemetry for you or anything).

"It's not even clear right now if there is a bug in the roslyn side or in the mvvm generator."

Not excluding there might be a bug in the MVVM Toolkit, but FWIW this repro is using a different generator (the one for [RelayCommand]) that is completely different than the one for [ObservableProperty] that almost everyone else here and in dotnet/roslyn#67123 was using. I mean I guess it's possible both have the same bug, but I'm kinda inclined to think this might rather be a bug in the generator infrastructure, given the error message is also the same 🤔

Does the repro help?

@jaredpar
Copy link

jaredpar commented Jun 1, 2023

@Sergio0694

Exception was of type 'ArgumentException' with message 'The hintName 'AmbientSounds.ViewModels.SoundListViewModel.LoadAsync.g.cs' of the added source file must be unique within a generator

That is generally indicative of a bug in the generator though, not roslyn. It's asserting that you provided the same hint name twice. Have you debugged in to make sure you're not doing this?

@chsienki
Copy link

chsienki commented Jun 1, 2023

@jaredpar @Sergio0694 This sounds a lot like a previous issue we had where we were incorrectly handling removed entries: dotnet/roslyn#66643

I would bet that this is probably the same kind of issue.

@chsienki
Copy link

chsienki commented Jun 1, 2023

@Sergio0694 As an aside, I feel like this is the first time we're actually seeing the Warning CS8785 for this issue, is there a case where VS is not reporting this and just silently failing?

@Sergio0694
Copy link
Member

Ok I have investigated this a bit, if it's an issue in my generator I have to be honest I just don't see it 🥲
I have however done the following tests as well:

  • Changed the RegisterSourceOutput to just always use a new Guid for filename. Works just fine and no longer repros.
  • Changed the incremental model to also have a Guid property, which I always set to a new Guid in my FAWMN transform. This also works just fine and no longer repros.

So it seems the only way to reproduce the issue (at least, in this case) is if the incremental models compare to be equal across pipelines. I am also a bit confused as to why using a new guid from the output node doesn't result in a build error in case the generator driver was in fact accidentally passing the same model twice (which would explain the ArgumentException from the generator) I was seeing. But then wouldn't it have resulted in the same code being generated twice now, which would presumably fail to build (since the generated member would end up being duplicated)? 🤔

"I feel like this is the first time we're actually seeing the Warning CS8785 for this issue, is there a case where VS is not reporting this and just silently failing?"

Not sure, this is the only case that I can consistently reproduce locally, not sure about others. Hopefully others in this thread that have also encountered the issue (especially with the other generator, for observable properties) can also comment.

@bad-samaritan
Copy link

bad-samaritan commented Jun 3, 2023

I was experiencing MissingMethodException for ObservablePropertyGenerator in my WPF project.
I tried various workarounds including <IncludePackageReferencesDuringMarkupCompilation>true but none of them worked.
Finally I have upgraded Visual Studio 2022 to 17.6.2 and the error is now gone.

However, I'm not entirely sure it's gone forever as previously error appeared spontaneously when jumping between different commits of MahApps.Metro (linked as project reference to my app, not nuget) and did not go away after going back to the commit I started with. Cleanups, manually removing bin&obj, restarting VS - nothing helped.

Project details: WPF MVVM app, built on top of MahApps.Metro, targeted to .NET Framework 4.8 & NET 6.0 Windows, CommunityToolkit.Mvvm 8.0.0, View Model with generated properties placed in the same project as main UI (with <UseWPF>true).

@TranquilChaoss
Copy link

Not sure this is of much use, but updating to 17.6.2 from a much older version (17.3.something) solved this issue for me. Am a novice coder and was following the ".NET MAUI Data Binding with MVVM & XAML [5 of 8] | .NET MAUI for Beginners" youtube tutorial from the dotnet team and got this issue. Will leave a comment about this solution on the YouTube video as well as the option to downgrade to 8.0. Sadly didn't think to document the 60+ errors and warnings (which seems like more than others getting the same issue) I was getting on 8.2, but outside of using the 8.0 version the build would fail and the source code generators would not generate any files.

@Sergio0694
Copy link
Member

The last issues related to this have been addressed by dotnet/roslyn#68451.
Fixes should flow downstream from VS 17.7 Preview 4. Closing this 🙂

@DanielD
Copy link

DanielD commented Jun 28, 2023

It's still happening on Visual Studio for Mac version 17.6 (build 1575).

@sharwell
Copy link

@DanielD You can write the following in a source file to see the compiler version in use:

#error version

The fix would only be in Roslyn 4.7 and higher.

@LiamJM96
Copy link

LiamJM96 commented Jul 17, 2023

The last issues related to this have been addressed by dotnet/roslyn#68451.
Fixes should flow downstream from VS 17.7 Preview 4. Closing this 🙂

Hi @Sergio0694, would you happen to know when this will likely reach the current channel for mainstream use? I know that it is 3 months or so after the preview is released

@Sergio0694
Copy link
Member

Sorry, we can't share ETAs on when will public releases of Visual Studio will be released 🥲
You can however assume it'll be available by November 8th at the latest, since .NET 8 and C# 12 also release then.

@LiamJM96
Copy link

@Sergio0694 Thanks for the update and quick response 🙂

@CodingOctocat
Copy link

Its still happening on WPF + .NET 7.0 + VS 17.6.5 + MvvmToolkit v8.2.1,.

@Sergio0694 Sergio0694 unpinned this issue Aug 9, 2023
@mscholz5
Copy link

mscholz5 commented Aug 24, 2023

I observe probably the same issue:
The project is a Windows-Application based on WPF with VS 17.7.2, MvvmToolkit v8.2.1 and .NET Framework 4.6.2.

I use Mvvm to generate properties and commands.

[ObservableProperty]
int _foo;

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

IntelliSense does not show errors and I can see the generated code under "Analyse".
But the compiler itself produces a CS0103 error for accessing "Foo".

I do not observe this issue within library projects of the same solution (same target framework, same toolkit).
"CompilerGeneratedFilesOutputPath" and "EmitCompilerGeneratedFiles" also work only for the library projects, but not for the app projects (OutputType "WinExe").

I have not found a workaround. The source generator is just not working for me.
I can also not go back to v8.0 as I need to add attributes on the generated properties.

Update:
I assume that the XAML files add intermediate compilation steps, which overwrite or replace the mvvm files.
Setting "IncludePackageReferencesDuringMarkupCompilation" to "true" does not solve anything.

@sharwell
Copy link

@mscholz5 That does not sound like the same issue. Can you file a new issue?

@mscholz5
Copy link

I filed #750.

@LancashireDave
Copy link

This issue is also happening for me on 17.8.0 Preview 7.0 and 8.2.2.

@michael-hawker
Copy link
Member

This issue is also happening for me on 17.8.0 Preview 7.0 and 8.2.2.

@LancashireDave this should have been resolved; can you see if it's similar to #750 or file a new issue to provide more information?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
external ⤴️ Something related to or caused by an external project mvvm-toolkit 🧰 Issues/PRs for the MVVM Toolkit
Projects
None yet
Development

No branches or pull requests