Sergio0694 / PolySharp

PolySharp provides generated, source-only polyfills for C# language features, to easily use all runtime-agnostic features downlevel. Add a reference, set your C# version to latest and have fun! 🚀

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How are all the polyfill source files generated?

0xced opened this issue · comments

Looking inside src/PolySharp.SourceGenerators/EmbeddedResources all polyfill source files are marked as // <auto-generated/> but I could not figure out how exactly those files are generated. I was expecting to find a tool inside this repository that was responsible for automatically generating those files, but could not find one! I even tried looking at the commit history to find clues but it did not help.

@Sergio0694 Could you please shed some light?

Some context: I wanted to contribute a polyfill for UnreachableException (issue #60) and I had to copy/paste the code from https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Diagnostics/UnreachableException.cs It felt very much cheating to not use automatic code generation like all other polyfills.

Hey there! The polyfills are generated by an incremental generator bundled in the package 🙂
It's this one:

[Generator(LanguageNames.CSharp)]
public sealed partial class PolyfillsGenerator : IIncrementalGenerator

OK, I think my question was not clear enough. Let's try to rephrase it: where do files inside the src/PolySharp.SourceGenerators/EmbeddedResources directory come from when they are checked into the repository? For example, the System.Diagnostics.CodeAnalysis.NotNullWhenAttribute.cs file that was added in commit f337dca starts with // <auto-generated/>. So I assumed it was generated by some tool, wasn't it? Was it generated by the PolyfillsGenerator incremental generator? From what I understand this source generator is reading those files from the embedded resources, not generating those files.

Were the 3 first lines manually added despite the first line implying the opposite?

// <auto-generated/>
#pragma warning disable
#nullable enable annotations

I hope my question makes sense now.

Oooh gotcha, sorry. Yeah those were just manually added to those files 😄
Those files are not autogenerated in this repo, they're more like templates.

OK, got it. I was totally fooled by the // <auto-generated/> comment. I was expecting something like what Simon Cropp does in its NullabilityInfoContext polyfill, i.e. syncing from the dotnet/runtime repository and applying some fixes on top of it: https://github.com/SimonCropp/NullabilityInfo/blob/1e49e8d7f3bb6a2146ea0ca606ab803ac3ceb6ac/src/Nullability.Source.Tests/Sync.cs#L12-L40