meziantou / Meziantou.Polyfill

MIT License
43 stars 1 forks source link

Duplicated polyfills if polyfill is available in an assembly which has InternalsVisibleTo set #2

Closed latonz closed 1 year ago

latonz commented 1 year ago

If an assembly has InternalsVisibleTo set to another assembly, and Meziantou.Polyfill is added to both projects, polyfills are generated for both. This leads to duplicated types.

Eg (Assembly 1 has InternalsVisibleTo("Assembly 2")):

The type 'CallerArgumentExpressionAttribute' exists in both '<Assembly 1>' and <Assembly 2>.

Reproduction steps:

meziantou commented 1 year ago

Can you provide a repro project (zip or github repo)? On my side it seems to be supported. You can see that ClassLib2 doesn't contain any generated files.

image
latonz commented 1 year ago

Thank you for the fast response. I poked around and found out that ClassLibrary2 used a later tfm net7.0, but ClassLibrary1 had a tfm of netstandard2.0. I added net7.0 as tfm to ClassLibrary1 and it worked. The problem was that ClassLibrary1 had only netstandard2.0 as tfm and therefore generated the polyfills while also having InternalsVisibleTo set for ClassLibrary2, but ClassLibrary2 had those types already due to the later tfm. I'm not even sure if this can be fixed without adding multitargeting to ClassLibrary1 🤔 Repro: https://github.com/latonz/duplicated-polyfill-repro

Btw: thank you for this great source generator and your interesting blog posts!

meziantou commented 1 year ago

Thanks for the repro. What you can do is to not generate the conflicting types if you don't use them:

<PropertyGroup>
  <MeziantouPolyfill_IncludedPolyfills>T:Type1;T:Type2;M:Member1</MeziantouPolyfill_IncludedPolyfills>
  <MeziantouPolyfill_ExcludedPolyfills>M:System.</MeziantouPolyfill_ExcludedPolyfills>
</PropertyGroup>

However, if you need to use conflicting types, I don't think there is a way to do it properly.

latonz commented 1 year ago

Thank you for your explanations. I'll try using multi-targeting which should work in my setup.