Skip to content

Fix generator runtime assembly resolution for Abstractions and restore fluent source emission#16

Merged
dex3r merged 3 commits into
mainfrom
copilot/fix-source-generator-issue
Feb 28, 2026
Merged

Fix generator runtime assembly resolution for Abstractions and restore fluent source emission#16
dex3r merged 3 commits into
mainfrom
copilot/fix-source-generator-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 28, 2026

The source generator was failing at compile time with FileNotFoundException for MattSourceGenHelpers.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

    • Removed direct type coupling to MattSourceGenHelpers.Abstractions in generator runtime execution.
    • Switched to string-based lookup for:
      • assembly name (MattSourceGenHelpers.Abstractions)
      • runtime types (Generate, RecordingGeneratorsFactory)
      • reflected members (CurrentGenerator, LastRecord)
    • Normalized assembly resolution to load implementation assemblies (not ref assemblies) in AssemblyLoadContext.Resolving.
  • Project dependency shape

    • Removed ProjectReference from MattSourceGenHelpers.Generators to MattSourceGenHelpers.Abstractions to avoid analyzer runtime dependency assumptions.
  • Fluent generation edge handling

    • Extended fluent default-expression extraction to recognize WithBody(...).
    • For zero-parameter generated methods, emit direct return ...; instead of generating a switch over a synthetic argument.
// Before: compile-time coupling in generator assembly
Type? generatorStaticType = abstractionsAssembly.GetType(typeof(Generate).FullName!);

// After: runtime-decoupled lookup
private const string GenerateTypeName = "MattSourceGenHelpers.Abstractions.Generate";
Type? generatorStaticType = abstractionsAssembly.GetType(GenerateTypeName);
Original prompt

This section details on the original issue you should resolve

<issue_title>Fix source generator</issue_title>
<issue_description>The project does not build, because the source generator fails with the following exception:

1>CSC: Warning CS8785 : Generator 'GeneratesMethodGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'FileNotFoundException' with message 'Could not load file or assembly 'MattSourceGenHelpers.Abstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.'.

Fix it.</issue_description>

Comments on the Issue (you are @copilot in this section)


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

Co-authored-by: dex3r <3155725+dex3r@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix source generator to resolve build failures Fix generator runtime assembly resolution for Abstractions and restore fluent source emission Feb 28, 2026
Comment thread MattSourceGenHelpers.Generators/GeneratesMethodExecutionRuntime.cs
@dex3r dex3r marked this pull request as ready for review February 28, 2026 18:06
Copilot AI review requested due to automatic review settings February 28, 2026 18:06
@dex3r dex3r merged commit 4cf02dd into main Feb 28, 2026
3 checks passed
@dex3r dex3r deleted the copilot/fix-source-generator-issue branch February 28, 2026 18:09
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ProjectReference from MattSourceGenHelpers.Generators to MattSourceGenHelpers.Abstractions and replaced all direct type references with string-based constants for runtime reflection.
  • Normalized assembly resolution in both AssemblyLoadContext.Resolving handlers to redirect ref assemblies to their implementation counterparts.
  • Extended ExtractDefaultExpressionFromFluentMethod to recognize .WithBody(...) invocations, and added a zero-parameter early-return path in GenerateSwitchMethodSource to emit a direct return statement instead of a switch.

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.
Copy link

Copilot AI Feb 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
// 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.

Copilot uses AI. Check for mistakes.
Comment on lines +184 to +191
if (partialMethod.Parameters.Length == 0)
{
string fallbackExpression = defaultExpression ?? "default";
builder.AppendLine($" return {fallbackExpression};");
builder.AppendLine(" }");
builder.AppendLine("}");
return builder.ToString();
}
Copy link

Copilot AI Feb 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix source generator

3 participants