Fix generator runtime assembly resolution for Abstractions and restore fluent source emission#16
Conversation
Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes a FileNotFoundException that prevented the GeneratesMethodGenerator source generator from running, by removing the direct compile-time reference from MattSourceGenHelpers.Generators to MattSourceGenHelpers.Abstractions. It also restores fluent emission paths that were broken due to the generator not executing.
Changes:
- Removed the
ProjectReferencefromMattSourceGenHelpers.GeneratorstoMattSourceGenHelpers.Abstractionsand replaced all direct type references with string-based constants for runtime reflection. - Normalized assembly resolution in both
AssemblyLoadContext.Resolvinghandlers to redirectrefassemblies to their implementation counterparts. - Extended
ExtractDefaultExpressionFromFluentMethodto recognize.WithBody(...)invocations, and added a zero-parameter early-return path inGenerateSwitchMethodSourceto emit a directreturnstatement instead of aswitch.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
MattSourceGenHelpers.Generators/MattSourceGenHelpers.Generators.csproj |
Removes the ProjectReference to Abstractions that caused the analyzer runtime failure |
MattSourceGenHelpers.Generators/GeneratesMethodExecutionRuntime.cs |
Replaces direct Abstractions type references with string constants; applies ResolveImplementationAssemblyPath in Resolving handlers |
MattSourceGenHelpers.Generators/GeneratesMethodPatternSourceBuilder.cs |
Adds WithBody to fluent default-expression extraction; emits direct return for zero-parameter methods |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| internal static class GeneratesMethodExecutionRuntime | ||
| { | ||
| // The Abstraction assembly cannot be referenced directly here, even though the IDE makes it look like it could. However, the source generation process will fail if it is. |
There was a problem hiding this comment.
The comment at line 18 is not indented, unlike all the members of the class that follow it. It starts at column 0 while the private constants below it are indented with 4 spaces. The comment should be indented to match the surrounding code members.
| // The Abstraction assembly cannot be referenced directly here, even though the IDE makes it look like it could. However, the source generation process will fail if it is. | |
| // The Abstraction assembly cannot be referenced directly here, even though the IDE makes it look like it could. However, the source generation process will fail if it is. |
| if (partialMethod.Parameters.Length == 0) | ||
| { | ||
| string fallbackExpression = defaultExpression ?? "default"; | ||
| builder.AppendLine($" return {fallbackExpression};"); | ||
| builder.AppendLine(" }"); | ||
| builder.AppendLine("}"); | ||
| return builder.ToString(); | ||
| } |
There was a problem hiding this comment.
The new zero-parameter direct-return path in GenerateSwitchMethodSource (lines 184–191) and the new WithBody recognition in ExtractDefaultExpressionFromFluentMethod (line 145) are not covered by any test in the test suite. There is an analogous ColorsClassFluent example in MattSourceGenHelpers.Examples that exercises this exact scenario (zero-parameter MethodImplementation<TReturnType>().WithBody(...)), but there's no corresponding test class in MattSourceGenHelpers.Tests verifying either the runtime output or the generated code for this pattern. A test similar to UnitTest1.cs / PiExampleFluentTests.cs should be added to confirm both paths work correctly.
The source generator was failing at compile time with
FileNotFoundExceptionforMattSourceGenHelpers.Abstractions, which prevented generated partial method implementations from being emitted. This PR removes the fragile runtime coupling and updates fluent emission paths that became reachable once generator execution resumed.Generator runtime loading
MattSourceGenHelpers.Abstractionsin generator runtime execution.MattSourceGenHelpers.Abstractions)Generate,RecordingGeneratorsFactory)CurrentGenerator,LastRecord)refassemblies) inAssemblyLoadContext.Resolving.Project dependency shape
ProjectReferencefromMattSourceGenHelpers.GeneratorstoMattSourceGenHelpers.Abstractionsto avoid analyzer runtime dependency assumptions.Fluent generation edge handling
WithBody(...).return ...;instead of generating aswitchover a synthetic argument.Original prompt
🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.