Skip to content

Conversation

@Sergio0694
Copy link
Owner

Closes #50, #103

Description

This PR updates the generator to also emit the [Embedded] attribute for polyfills, to avoid conflicts for internal types.

@cremor
Copy link

cremor commented Sep 1, 2025

Would this work if you don't have the latest SDK or VS installed? Because according to this you couldn't declare the EmbeddedAttribute yourself if the compiler would have created one prior to VS 17.13. See also dotnet/roslyn#76523

Also, just to have it documented here too: @MarkKharitonov found a problem with this solution, see #50 (comment)

@Sergio0694
Copy link
Owner Author

@cremor yeah this will work fine, I'm still using Roslyn 4.3.0, and not that new API that was added 🙂

@cremor
Copy link

cremor commented Sep 4, 2025

I didn't mean the new API. I meant that quote from the PR dotnet/roslyn#76523:

Today, [...] if we need to generate it, we error if the user defines one.

I understand this as:

If you use a compiler version before VS 17.13
and
you define EmbeddedAttribute yourself (e.g. by using PolySharp with this new changes)
and
you have something in your code that would lead Roslyn to emit the EmbeddedAttribute itself

then you would get an error.

I haven't tested this. I just wanted to raise awareness.

@jviau
Copy link

jviau commented Sep 11, 2025

Small suggestion. Recent Roslyn APIs have an convenience API .AddEmbeddedAttributeDefinition(), which will emit the EmbeddedAttribute type for you. As opposed to including it directly like you are in this PR. If you make including of the [Embedded] marker on all the types opt-in via an MSBuild variable, then you can also opt in having the EmbeddedAttribute at all via conditionally calling that API.

You could re-create this helper if you are not on the newer Roslyn packages: https://github.com/dotnet/roslyn/blob/51d2ed9e47364b57024f7715c2e2b2e385fe1454/src/Compilers/Core/Portable/SourceGeneration/IncrementalContexts.cs#L144

Introduces a new MSBuild property 'PolySharpUseEmbeddedAttributeForGeneratedTypes' and corresponding generation option to control whether the [Embedded] attribute is emitted and applied to generated types. Refactors the generator to emit the EmbeddedAttribute only when this option is enabled, improving flexibility and reducing unnecessary code emission.
Document and expose the new PolySharpUseEmbeddedAttributeForGeneratedTypes property in both READMEs and PolySharp.targets. This option allows generated polyfill types to be marked with the [Embedded] attribute.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

InternalsVisibleTo from two other projects leads to not being able to use PolySharp

4 participants