reactiveui / ReactiveUI.SourceGenerators

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

[Bug]: Conflict between Source Generator Attributes defined in different assemblies #111

Closed PremekTill closed 2 weeks ago

PremekTill commented 2 weeks ago

Describe the bug 🐞

When creating two projects where Project B depends on Project A and both use ReactiveUI.SourceGenerators, when a class in Project B uses one of the source gen attributes (ReactiveAttribute, ReactiveCommandAttribute, ObservableAsPropertyAttribute et cetera) and at the same time references a class from Project A which uses the attribute as well, a build warning is generated.

The warning is CS0436, for example: The type ReactiveAttribute in <PathProjectB>\obj\Debug\ReactiveUI.SourceGenerators\ReactiveUI.SourceGenerators.ReactiveGenerator\ReactiveUI.SourceGenerators.ReactiveAttribute.g.cs conflicts with the imported type ReactiveAttribute in <AssemblyProjectA>. Using the type defined in <PathProjectB>\<...ReactiveAttribute.g.cs>.

The compiled project runs as expected, however, the superfluous warnings clutter the Build output and cause problems in CI/CD pipelines.

Step to reproduce

  1. Create a solution with two projects where ProjectB references ProjectA.
  2. Create ClassA and ClassB in the corresponding projects where both have a field with the Reactive attribute and ClassB also references ClassA (so that we include the namespace of ProjectA into ClassB).
  3. Build the solution.
  4. See warning CS0436 in the Build output.

Reproduction repository

No response

Expected behavior

There should be no warnings in the Build output.

Screenshots 🖼️

No response

IDE

Rider Windows

Operating system

Windows 10

Version

ReactiveUI.SourceGenerators 1.1.31

Device

No response

ReactiveUI Version

20.1.63

Additional information ℹ️

No response

ChrisPulman commented 2 weeks ago

There will be a release in the next couple of days that will resolve this, V2.0.x

PremekTill commented 2 weeks ago

Unfortunately, this particular issue got worse with 2.0.9. The original conflict is gone, but now I get a CS0122 "\<AttributeName> is inaccessible due to its protection level" error instead and cannot compile at all. It seems that the generator tries to pull in the attribute generated in ProjectA from ProjectB, instead of generating one reference for each project separately, thus breaking the internal modifier. Cleaning, rebuilding and manually deleting bin and obj folders didn't help.

I tried different settings for PrivateAssets and IncludeAssets in both csproj files, but to no avail. Just for reference, these are my default settings:

<PackageReference Include="ReactiveUI.SourceGenerators" Version="2.0.9">
    <PrivateAssets>all</PrivateAssets>
    <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
ChrisPulman commented 2 weeks ago

Thank you for your feedback, I am looking further into this and will come up with a resolve as soon as possible.

ChrisPulman commented 2 weeks ago

@PremekTill Could you try <PackageReference Include="ReactiveUI.SourceGenerators" Version="2.0.9" PrivateAssets="all" />

PremekTill commented 2 weeks ago

Unfortunately, it does not have any effect. Additionally, as I was changing the versions and parameters back and forth, the generator was getting more and more broken (at times refusing to generate anything at all without throwing any errors, even after dotnet clean and restore/rebuild).

However, switching over to Visual Studio and compiling the projects once with v2.0.9 seems to have fixed the issue completely – even going back to Rider, I can now switch between v1.1.31 and v2.0.9 freely.

I'm not sure what the root cause of the issue was, but it might have been an issue Rider-side, maybe having to do with some internal cache? I will try pulling the project on a clean machine and checking if it works there without the VS compilation – if it doesn't resurface, I think this issue can be closed for now, though I'd maybe leave it open for a couple of days while I try to repro in case someone else runs into it when upgrading from 1.1.31.

glennawatson commented 2 weeks ago

Sounds like maybe old nuget package cache.

github-actions[bot] commented 1 day 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.