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

Compiling fails when trying to use to ObservableProperty source generator property #4174

Closed
2 tasks
Goz3rr opened this issue Aug 10, 2021 · 6 comments
Closed
2 tasks
Labels
external ⤴️ Requires an update to an external dependency or due to code outside the Toolkit. mvvm-toolkit 🧰 Issues/PRs for the Microsoft.Toolkit.Mvvm package
Milestone

Comments

@Goz3rr
Copy link

Goz3rr commented Aug 10, 2021

Describe the bug

When I try to do anything with the property generated from the ObservableProperty source generator the compiler says it doesn't exist. Intellisense however does think it exists, and I can press F12 to inspect the generated code with the property. Binding to the generated property at runtime also works without any issues. No errors show up in the error list in VS either, only in build output.

  • Is this bug a regression in the toolkit? If so, what toolkit version did you last see it work:

Steps to Reproduce

  • Can this be reproduced in the Sample App? (Either in a sample as-is or with new XAML pasted in the editor.) If so, please provide custom XAML or steps to reproduce. If not, let us know why it can't be reproduced (e.g. more complex setup, environment, dependencies, etc...)

Steps to reproduce the behavior:
Create a new WPF Application targeting .NET5.0 and add the Microsoft.Toolkit.Mvvm version 7.1.0-preview1 package from nuget.

Replace MainWindow.xaml with:

<Window x:Class="ObservablePropertyTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ObservablePropertyTest"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:MainViewModel/>
    </Window.DataContext>
    <TextBlock Text="{Binding Foo}" />
</Window>

Create MainViewModel.cs:

using Microsoft.Toolkit.Mvvm.ComponentModel;

namespace ObservablePropertyTest
{
    public partial class MainViewModel : ObservableRecipient
    {
        [ObservableProperty]
        private string foo = "bar";

        public MainViewModel()
        {
            Foo = "Hello World";
        }
    }
}

Expected behavior

App compiles and shows "Hello World". Instead, VS reports build errors while none show up.

Going to Output -> Build reveals:

Build started...
1>------ Build started: Project: ObservablePropertyTest, Configuration: Debug Any CPU ------
1>C:\Users\goz3rr\source\repos\ObservablePropertyTest\ObservablePropertyTest\MainViewModel.cs(12,13,12,16): error CS0103: The name 'Foo' does not exist in the current context
1>C:\Users\goz3rr\source\repos\ObservablePropertyTest\ObservablePropertyTest\MainViewModel.cs(8,24,8,27): warning CS0414: The field 'MainViewModel.foo' is assigned but its value is never used
1>Done building project "ObservablePropertyTest_5l0qwrrp_wpftmp.csproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Commenting out the Foo = "Hello World"; line makes the app compile fine and shows "bar", which suggests it successfully bound to the generated Foo property at runtime.

Screenshots

image

Environment

NuGet Package(s): Microsoft.Toolkit.Mvvm

Package Version(s): 7.1.0-preview1

Windows 10 Build Number:
- [ ] Fall Creators Update (16299)
- [ ] April 2018 Update (17134)
- [ ] October 2018 Update (17763)
- [ ] May 2019 Update (18362)
- [ ] May 2020 Update (19041)
- [x] 21H1 (19043.1110)
- [ ] Insider Build (build number: )

App min and target version:
- [ ] Fall Creators Update (16299)
- [ ] April 2018 Update (17134)
- [ ] October 2018 Update (17763)
- [ ] May 2019 Update (18362)
- [ ] May 2020 Update (19041)
- [ ] Insider Build (xxxxx)

Device form factor:
- [x] Desktop
- [ ] Xbox
- [ ] Surface Hub
- [ ] IoT

Visual Studio
- [ ] 2017 (version: )
- [x] 2019 (version: Enterprise 16.10.4)
- [ ] 2019 Preview (version: )

Additional context

Add any other context about the problem here.

@Goz3rr Goz3rr added the bug 🐛 An unexpected issue that highlights incorrect behavior label Aug 10, 2021
@ghost ghost added the needs triage 🔍 label Aug 10, 2021
@ghost
Copy link

ghost commented Aug 10, 2021

Hello Goz3rr, thank you for opening an issue with us!

I have automatically added a "needs triage" label to help get things started. Our team will analyze and investigate the issue, and escalate it to the relevant team if possible. Other community members may also look into the issue and provide feedback 🙌

@michael-hawker michael-hawker added mvvm-toolkit 🧰 Issues/PRs for the Microsoft.Toolkit.Mvvm package and removed needs triage 🔍 labels Aug 11, 2021
@michael-hawker michael-hawker added this to the 7.1 milestone Aug 11, 2021
@michael-hawker
Copy link
Member

@Sergio0694 is the field allowed to be set a default value like this:

    [ObservableProperty]
    private string foo = "bar";

I don't remember any of your examples with this. Was this a generation scenario you didn't think of?

@Goz3rr
Copy link
Author

Goz3rr commented Aug 12, 2021

Note that I just did that for testing. Leaving the default value out has the same compiler error, just no way to easily see that it binds correctly at runtime

@Sergio0694
Copy link
Member

Sergio0694 commented Aug 12, 2021

@michael-hawker That is supported - source generators can only generate new code, so annotated fields will just remain exactly how they're defined, and using a field initializer like that is just standard C# syntax and therefore perfectly valid as well 🙂

This issue kinda strikes me as potentially being more about tooling in some way rather than about a bug in the source generator itself. The fact that not referencing the property will let the project build and run fine (as well as @Goz3rr having said that the generated code is fine and visible in solution explorer) makes me think it might just be some issue with the way the source generator are run by Roslyn when inside a WPF compilation. I'll see if I can repro this on my end 👍

EDIT: I can repro, and the generated code looks fine. I'm not convinced this is a bug in the MVVM Toolkit.

@Sergio0694
Copy link
Member

So turns out the temporary fix is to just this to the .csproj file:

<IncludePackageReferencesDuringMarkupCompilation>true</IncludePackageReferencesDuringMarkupCompilation>

This is tracked in dotnet/wpf#3404, with the fix also being bundled with .NET 6 and the new .NET 5 SDK. Closing this as it's an external issue not related to the MVVM Toolkit itself. Thanks @Goz3rr for the report though! 😄

@Sergio0694 Sergio0694 added external ⤴️ Requires an update to an external dependency or due to code outside the Toolkit. and removed bug 🐛 An unexpected issue that highlights incorrect behavior labels Aug 12, 2021
@Goz3rr
Copy link
Author

Goz3rr commented Aug 13, 2021

Thank you for the temporary fix 👍

@ghost ghost locked as resolved and limited conversation to collaborators Nov 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
external ⤴️ Requires an update to an external dependency or due to code outside the Toolkit. mvvm-toolkit 🧰 Issues/PRs for the Microsoft.Toolkit.Mvvm package
Projects
None yet
Development

No branches or pull requests

3 participants