Build generated mappers on netstandard2.0, and stand aside below C# 9 - #104
Conversation
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 37 minutes. View limit detailsLimit details: You’ve used the included review currently available. Review configuration: ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (13)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
No blocking issues were identified, and focused tests cover attribute generation, language gating, registration, and runtime fallback.
Review effort: Balanced
Findings: None
What changed in this PR
Fixes Mapsicle’s optional source generator for netstandard2.0 and .NET Framework consumers while preserving runtime mapping below C# 9.
Changes:
- Generates an internal
ModuleInitializerAttributewhen needed. - Skips generation below C# 9 and reports
MSG003once. - Adds compatibility fixtures, regression tests, and documentation.
| File | Description |
|---|---|
| tests/Mapsicle.SourceGen.Tests/Mapsicle.SourceGen.Tests.csproj | Enables direct generator-driver tests. |
| tests/Mapsicle.SourceGen.Tests/LanguageVersionTests.cs | Tests language gating and attribute reuse. |
| tests/Mapsicle.SourceGen.OldLanguage.Fixture/NsOld.cs | Exercises runtime fallback under C# 7.3. |
| tests/Mapsicle.SourceGen.OldLanguage.Fixture/Mapsicle.SourceGen.OldLanguage.Fixture.csproj | Configures the C# 7.3 consumer fixture. |
| tests/Mapsicle.SourceGen.NetStandard.Fixture/NsGenerated.cs | Exercises generated conversions on netstandard2.0. |
| tests/Mapsicle.SourceGen.NetStandard.Fixture/Mapsicle.SourceGen.NetStandard.Fixture.csproj | Configures the C# 9 consumer fixture. |
| tests/Mapsicle.NetStandard.Tests/NetStandardGeneratedTests.cs | Checks registration, mapping, and fallback. |
| tests/Mapsicle.NetStandard.Tests/Mapsicle.NetStandard.Tests.csproj | References both compatibility fixtures. |
| src/Mapsicle.SourceGen/MapperGenerator.cs | Adds language gating and conditional attribute generation. |
| src/Mapsicle.SourceGen/AnalyzerReleases.Unshipped.md | Records the new MSG003 diagnostic. |
| README.md | Explains language-version requirements. |
| Mapsicle.sln | Includes both fixture projects. |
| changelog.d/sourcegen-netstandard.fixed.md | Documents the compatibility fix. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
A netstandard2.0 or .NET Framework project that installs
Mapsicle.SourceGenand declares a pair now builds. It failed three ways on main, all inside the generated files: CS0234 (noModuleInitializerAttributeon the target), CS8370 (module initializers need C# 9) and CS8627 (unconstrainedTDest?needs C# 9).Two changes in the generator:
ModuleInitializerAttributethe assembly can use, the generated file declares an internal one. The compiler matches it by name and every runtime runs a module initializer. A project that already declares its own copy gets no second one.MSG003warning once, naming the language version and the<LangVersion>9.0</LangVersion>fix. Every pair keeps mapping through the engine. It stays silent when the assembly declared nothing. The issue allowed either emitting 7.3 code or refusing with a diagnostic. I chose refusing, because 7.3 output would be a third shape of the emitted code to keep inside the band.Proof:
tests/Mapsicle.SourceGen.NetStandard.Fixtureis a netstandard2.0 project at C# 9 with a declared pair (widening, cross enum, enum to string, DateTime to DateTimeOffset, nested, flattened, list and array into list). On main's generator it fails to build with CS0234. Here it builds, the pair is in the registry from the module initializer, and it maps the same asMapperFactory.Create().tests/Mapsicle.SourceGen.OldLanguage.Fixtureis the same at C# 7.3. On main it fails with CS8370 and CS8627. Here it builds with one MSG003 in the log, the pair is not registered, and it maps through the engine.LanguageVersionTestsruns the generator through the Roslyn driver: one MSG003 and no trees at 7.3 and 8.0, for a declared pair and forMapsicleGenerateAll; two trees and no diagnostic at 9; silence at 7.3 with nothing declared; no second attribute when the project or the target already has one.Mapsicle.SourceGen.Testscompiles with the project forced to C# 9, so nothing emitted needs a newer language version.dotnet test Mapsicle.sln -c Releasepasses on net8.0 and net10.0 (SourceGen 94, NetStandard 14).dotnet format --verify-no-changesexits 0. The coverage job's-f net8.0run passes with the two new netstandard2.0 projects in the solution.No emitted mapper changes for a project already on C# 9 with the attribute present, so the band is not affected.
This touches
MapperGenerator.csandREADME.mdin different hunks from #103. Whichever merges second may need a rebase.Review was done in one session, so the finder and the judge shared a context.
Closes #74