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! 🚀
MIT License
1.83k stars 49 forks source link

How are all the polyfill source files generated? #88

Closed 0xced closed 1 year ago

0xced commented 1 year ago

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.

Sergio0694 commented 1 year ago

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

https://github.com/Sergio0694/PolySharp/blob/2689b7e2e70db52c6947b75245948c040417ddf0/src/PolySharp.SourceGenerators/PolyfillsGenerator.cs#L10-L11

0xced commented 1 year ago

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 f337dcad5bd866c7dee6d3dcd51282f5957a5460 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.

Sergio0694 commented 1 year ago

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.

0xced commented 1 year ago

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