reactiveui / ReactiveUI.SourceGenerators

Use source generators to generate objects.
MIT License
31 stars 3 forks source link

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

Closed n-ski closed 3 weeks ago

n-ski commented 3 weeks ago

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

ethpch commented 3 weeks ago

Other attributes also need this one.

[Description("Property")]
[Reactive]
string? _myProperty;

generated

[global::System.CodeDom.Compiler.GeneratedCode("ReactiveUI.SourceGenerators.ReactiveGenerator", "2.0.0.0")]
/// <inheritdoc cref="_myProperty"/>
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public string? MyProperty { get => _myProperty; set => this.RaiseAndSetIfChanged(ref _myProperty, value); }

Description is missing.

Tested in version 2.0.9 and 1.1.31. Neither of them can handle this.

Maybe an optional parameter is better, like [Reactive(ForwardAttributes = true)].

github-actions[bot] commented 6 days ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.