Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 3, 2025

The AssemblyLoaderGenerator was generating Assembly.Load() calls for all referenced assemblies, including those without physical files on disk. This could lead to runtime errors when attempting to load assemblies that don't exist as physical files, such as:

  • In-memory assemblies during compilation
  • Forwarded assemblies that don't have corresponding DLL files
  • The current compilation assembly itself (which doesn't exist as a file yet during source generation)

Changes Made

Modified the AssemblyLoaderGenerator to check if an assembly has a physical location before emitting assembly loading code:

  1. Added location validation: Introduced HasPhysicalLocation() method that checks if an assembly has a corresponding PortableExecutableReference with a valid file path
  2. Updated code generation: Modified WriteAssemblyLoad() to only emit Assembly.Load() calls for assemblies that have physical files
  3. Preserved existing filtering: Maintained the existing system assembly filtering logic

Example Impact

Before: Generated code would include problematic entries like:

global::TUnit.Core.SourceRegistrar.RegisterAssembly(() => 
    global::System.Reflection.Assembly.Load("CurrentCompilationAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"));

After: Only generates entries for assemblies with actual physical files:

global::TUnit.Core.SourceRegistrar.RegisterAssembly(() => 
    global::System.Reflection.Assembly.Load("TUnit.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b8d4030011dbd70c"));

The fix follows the same pattern used in ReferencesHelper.cs for filtering assemblies with valid locations, ensuring consistency across the codebase.

Fixes #2726.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@thomhurst thomhurst changed the title [WIP] In AssemblyLoaderGenerator only emit code to load an assembly if we the assembly location exists [WIP] In AssemblyLoaderGenerator only emit code to load an assembly if the assembly location exists Aug 3, 2025
Copilot AI changed the title [WIP] In AssemblyLoaderGenerator only emit code to load an assembly if the assembly location exists Fix AssemblyLoaderGenerator to only emit code for assemblies with physical locations Aug 3, 2025
Copilot AI requested a review from thomhurst August 3, 2025 23:36
Copilot finished work on behalf of thomhurst August 3, 2025 23:36
@thomhurst thomhurst marked this pull request as ready for review August 3, 2025 23:37
Copilot AI temporarily deployed to Pull Requests August 3, 2025 23:37 Inactive
Copilot AI temporarily deployed to Pull Requests August 3, 2025 23:37 Inactive
@thomhurst thomhurst merged commit 6b5640c into main Aug 4, 2025
6 of 14 checks passed
@thomhurst thomhurst deleted the copilot/fix-2726 branch August 4, 2025 00:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants