Skip to content

[Bug]: IViewForGenerator "leaks" other attributes to generated code #116

@n-ski

Description

@n-ski

Describe the bug 🐞

If a class is marked with the IViewForAttribute<T>, the generator will carry over other attributes to the generated partial class. This means that such attributes as SingleInstanceViewAttribute and ViewContractAttribute cannot be used in conjunction with IViewForAttribute<T>.

Step to reproduce

  1. From the CLI, run dotnet new globaljson --sdk-version 8.0.400

  2. dotnet new wpf

  3. dotnet add package ReactiveUI --version 20.1.63

  4. dotnet add package ReactiveUI.SourceGenerators --version 2.0.9

  5. Add a minimal view model class and attributes in MainWindow.xaml.cs.

    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    +using ReactiveUI;
    +using ReactiveUI.SourceGenerators;
    
    namespace WpfApp1;
    
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    +[IViewFor<MainViewModel>]
    +[SingleInstanceView]
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
    
    +public class MainViewModel : ReactiveObject;
  6. Run dotnet build to compile. Observe the compilation error.

    C:\Users\anon\source\WpfApp1\obj\Debug\net8.0-windows\ReactiveUI.SourceGenerators\ReactiveUI.SourceGenerators.IViewForGenerator\MainWindow.IViewFor.g.cs(14,10): error CS0579: Duplicate 'global::ReactiveUI.SingleInstanceViewAttribute' attribute [C:\Users\anon\source\WpfApp1\WpfApp1_bnyjhzvr_wpftmp.csproj]
    

Reproduction repository

https://github.com/n-ski/RxUIIViewForGeneratorBugSample

Expected behavior

I should be able to use IViewForAttribute<T> with other attributes.

Screenshots 🖼️

No response

IDE

Rider Windows

Operating system

Windows

Version

21H2

Device

No response

ReactiveUI Version

20.1.63

Additional information ℹ️

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions