Skip to content

Refactor System.Reflection.MetadataLoadContext tests to work for single file #72908

Open

Description

@MichalStrehovsky

MetadataLoadContext test has a clever trick to map runtime types to their MetadataLoadContext version by using Assembly.Location, opening them in a metadata context and finding the corresponding type. This doesn't work in single-file testing in general and NativeAOT in particular.

// Given a runtime Type, load up the equivalent in the Test MetataLoadContext. This is for test-writing convenience so
// that tests can write "typeof(TestClass).Project()" and get the benefits of compile-time typename checking and Intellisense.
// It also opens the possibility of sharing Reflection tests between different type providers with minimal fuss.
public static Type Project(this Type type)
{
if (type == null)
return null;
Assembly assembly = type.Assembly;
string location = assembly.Location;
if (PlatformDetection.IsNotBrowser && (location == null || location == string.Empty))
{
throw new Exception("Could not find the IL for assembly " + type.Assembly + " on disk. The most likely cause " +
"is that you built the tests for a Jitted runtime but are running them on an AoT runtime.");
}

The best way forward would probably be to introduce a test assembly that will have all the types we need to project and to include it as Content. Like so:

<ProjectReference Include="..\System.Diagnostics.FileVersionInfo.TestAssembly\System.Diagnostics.FileVersionInfo.TestAssembly.csproj" Condition="'$(TargetOS)' != 'Browser'">
<OutputItemType>Content</OutputItemType>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</ProjectReference>

We can have the test types in both the test project and in the Content assembly - the projection would be updated to look in the Content assembly instead of Assembly.Location.

We'll probably need another Content assembly that will be a simulated CoreLib with whatever dummy content we need to make the test assembly build. It wouldn't be the first fake CoreLib in the runtime repo.

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Jul 27, 2022
  2. added this to the Future milestone on Jul 31, 2022
  3. ghost removed
    untriagedNew issue has not been triaged by the area owner
    on Jul 31, 2022
  4. steveharter commented on Nov 4, 2022

    @steveharter
    Contributor

    Linking #72917 and #27803

  5. modified the milestones: Future, 8.0.0 on Nov 4, 2022
  6. steveharter commented on Aug 2, 2023

    @steveharter
    Contributor

    Moving to v9 based on priority+resources.

  7. modified the milestones: 8.0.0, 9.0.0 on Aug 2, 2023
  8. steveharter commented on Jul 24, 2024

    @steveharter
    Contributor

    @MichalStrehovsky is this still a valid scenario and is the possible implementation below still valid?

    The best way forward would probably be to introduce a test assembly that will have all the types we need to project and to include it as Content.

  9. modified the milestones: 9.0.0, 10.0.0 on Jul 24, 2024
  10. MichalStrehovsky commented on Jul 25, 2024

    @MichalStrehovsky
    MemberAuthor

    @MichalStrehovsky is this still a valid scenario and is the possible implementation below still valid?

    The best way forward would probably be to introduce a test assembly that will have all the types we need to project and to include it as Content.

    Yes, it's still a test gap and including as Content should work. E.g. the System.Diagnostics.FileVersionInfo.Tests test does that to include System.Diagnostics.FileVersionInfo.TestAssembly (a test assembly file that is loaded to check its version).

  11. ericstj commented on Jun 3, 2025

    @ericstj
    Member

    We'll probably need another Content assembly that will be a simulated CoreLib with whatever dummy content we need to make the test assembly build. It wouldn't be the first fake CoreLib in the runtime repo.

    What about just including System.Runtime.dll with the test assembly. @jkotas and I were discussing improving the test coverage with more behavior diffing in #116080 (comment) and we have other reasons to include it.

    So we could build a test assembly of interesting types, and include it as content, along with the minimal set of references it required to build.

    I guess including the test assembly itself as content could also solve this, but I like the better factoring of having an assembly of only interesting test types.

  12. jkotas commented on Jun 3, 2025

    @jkotas
    Member

    What about just including System.Runtime.dll

    Yes, including System.Runtime ref assembly should be enough.

  13. modified the milestones: 10.0.0, Future on Jul 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions