Skip to content

Fix native AOT test failures in System.Reflection.Context tests#125080

Open
Copilot wants to merge 6 commits intomainfrom
copilot/fix-native-aot-test-coverage
Open

Fix native AOT test failures in System.Reflection.Context tests#125080
Copilot wants to merge 6 commits intomainfrom
copilot/fix-native-aot-test-coverage

Conversation

Copy link
Contributor

Copilot AI commented Mar 2, 2026

Tests added in PR #123026 for System.Reflection.Context fail in native AOT outerloop runs because they call reflection APIs that throw PlatformNotSupportedException or are otherwise unsupported under native AOT.

Changes

Applied guards to only the tests that were actually failing in native AOT CI runs, using the appropriate PlatformDetection predicate, ActiveIssue attribute, or platform-branching assertions for each API category:

  • IsMethodBodySupportedGetMethodBody(), GetILAsByteArray(), ExceptionHandlingClauses, LocalVariables, InitLocals, MaxStackSize, LocalSignatureMetadataToken

    • [ConditionalClass] on MethodBodyTests, ExceptionHandlingClauseTests, LocalVariableInfoTests (constructors call GetMethodBody(), so the entire class is skipped)
    • Individual [ConditionalFact] tests in CustomConstructorInfoTests, CustomMethodInfoTests, FinalCoverageTests, FinalCoverageTests2, MoreCoverageTests
  • IsMetadataTokenSupportedMetadataToken, ResolveField/Method/Type/Member

    • Tests in CustomConstructorInfoTests, CustomEventInfoTests, CustomMethodInfoTests, CustomModuleTests, CustomParameterInfoTests, CustomTypeTests, ExtendedFieldInfoTests, ExtendedPropertyInfoTests
  • IsNotNativeAot — APIs that throw PlatformNotSupportedException on native AOT (Module.IsResource(), EventInfo.GetOtherMethods()), plus GetEnumValues() (fails on projected types), and generic method Invoke()

    • Tests across CustomEventInfoTests, CustomModuleTests, ExtendedTypeTests, ExtendedAssemblyTests
  • Platform-branching assertions — Instead of skipping, tests assert the expected native AOT behavior:

    • GetReferencedAssemblies → asserts PlatformNotSupportedException on native AOT (ExtendedAssemblyTests)
    • GetSatelliteAssembly → asserts PlatformNotSupportedException on native AOT (ExtendedAssemblyTests2)
    • GetModule → validates Module.Name returns "<Unknown>" when !HasAssemblyFiles (ExtendedAssemblyTests2)
  • ActiveIssue (Type.GetInterfaceMap doesn't work for all cases #89157, native AOT)GetInterfaceMap() tests that fail due to a known native AOT limitation tracked in issue Type.GetInterfaceMap doesn't work for all cases #89157

    • ExtendedTypeTests.GetInterfaceMap_ReturnsProjectedMapping
    • FinalBranchCoverageTests.GetInterfaceMap_ReturnsMapping

Only tests confirmed as failing in the native AOT CI run were guarded. Tests that were passing on native AOT (including MethodHandle, FieldHandle, TypeHandle, StructLayoutAttribute, IsSecurityCritical/SafeCritical/Transparent, IsFullyTrusted, GetForwardedTypes, ModuleVersionId, FindTypes, and pointer type tests) remain as plain [Fact].

Original prompt

Problem

PR #123026 added new test files to src/libraries/System.Reflection.Context/tests/ that significantly improved code coverage. However, several tests are now failing in native AOT outerloop runs because they use APIs that are not supported on native AOT.

Fix Required

Apply the appropriate [ConditionalFact] guards to tests that use APIs unsupported on native AOT, following the existing patterns in the repo.

The key PlatformDetection properties to use are:

  • PlatformDetection.IsMethodBodySupported — for any test calling GetMethodBody(), GetILAsByteArray(), ExceptionHandlingClauses, LocalVariables, InitLocals, LocalSignatureMetadataToken, MaxStackSize, etc.
  • PlatformDetection.IsMetadataTokenSupported — for any test accessing MetadataToken, ResolveField(), ResolveMethod(), ResolveType(), ResolveMember(), ModuleVersionId, FindTypes
  • PlatformDetection.IsNotNativeAot — for tests accessing MethodHandle, FieldHandle, TypeHandle, StructLayoutAttribute, GetForwardedTypes, IsSecurityCritical/IsSecuritySafeCritical/IsSecurityTransparent (on type/constructor/method/field), IsFullyTrusted, GUID, and pointer types (int*)

Note that IsSecurityCritical/IsSecuritySafeCritical/IsSecurityTransparent tests are already guarded for Mono ([ActiveIssue(..., TestRuntimes.Mono)]) but not for native AOT.

Files to modify

All the new test files added by PR #123026:

src/libraries/System.Reflection.Context/tests/CustomConstructorInfoTests.cs

  • GetMethodBody_ReturnsBody[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMethodBodySupported))]
  • MethodHandle_ReturnsValue[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotNativeAot))]
  • MetadataToken_ReturnsValue[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMetadataTokenSupported))]
  • IsSecurityCritical_ReturnsValue, IsSecuritySafeCritical_ReturnsValue, IsSecurityTransparent_ReturnsValue — also add [ActiveIssue(..., TestPlatforms.NativeAot)] or [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotNativeAot))] in addition to the existing Mono guard

src/libraries/System.Reflection.Context/tests/CustomMethodInfoTests.cs

  • GetMethodBody_ReturnsBodyWithLocals[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMethodBodySupported))]
  • MethodHandle_ReturnsValue[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotNativeAot))]
  • MetadataToken_ReturnsValue[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMetadataTokenSupported))]
  • IsSecurityCritical/SafeCritical/Transparent — also guard for native AOT

src/libraries/System.Reflection.Context/tests/CustomEventInfoTests.cs

  • MetadataToken_ReturnsPositiveValue[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMetadataTokenSupported))]

src/libraries/System.Reflection.Context/tests/CustomTypeTests.cs

  • MetadataToken_ReturnsValue[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMetadataTokenSupported))]
  • TypeHandle_ReturnsValue[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotNativeAot))]
  • StructLayoutAttribute_ReturnsValue[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotNativeAot))]
  • IsSecurityCritical_ReturnsValue, IsSecuritySafeCritical_ReturnsValue, IsSecurityTransparent_ReturnsValue → add [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotNativeAot))]

src/libraries/System.Reflection.Context/tests/CustomModuleTests.cs

  • MetadataToken_ReturnsValue[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMetadataTokenSupported))]
  • ModuleVersionId_ReturnsNonEmptyGuid[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotNativeAot))]
  • ResolveField_ReturnsProjectedField[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMetadataTokenSupported))]
  • ResolveMethod_ReturnsProjectedMethod[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMetadataTokenSupported))]
  • ResolveType_ReturnsProjectedType[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMetadataTokenSupported))]
  • ResolveMember_ReturnsProjectedMember[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMetadataTokenSupported))]
  • FindTypes_ReturnsProjectedTypes[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMetadataTokenSupported))]

src/libraries/System.Reflection.Context/tests/CustomParameterInfoTests.cs

  • MetadataToken_ReturnsValue[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsMetadataTokenSupported))]

src/libraries/System.Reflection.Context/tests/ExtendedAssemblyTests.cs

  • `GetForwardedTypes_ThrowsNotImple...

This pull request was created from Copilot chat.


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

…ion.Context tests

Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix failing tests for native AOT compatibility Fix native AOT test failures in System.Reflection.Context tests Mar 2, 2026
@MichalStrehovsky
Copy link
Member

/azp run runtime-nativeaot-outerloop

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@MichalStrehovsky
Copy link
Member

@copilot this is disabling more tests than are actually failing. For example IsSecurityCritical_ReturnsValue doesn't fail at all. Please only address the tests that were actually failing in the pull request #124919 CI run.

…tive AOT CI

Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 3, 2026

@copilot this is disabling more tests than are actually failing. For example IsSecurityCritical_ReturnsValue doesn't fail at all. Please only address the tests that were actually fai...

Done in 335d280. Reverted the guards that weren't actually failing in PR #124919's CI run: MethodHandle, FieldHandle, TypeHandle, StructLayoutAttribute, IsSecurityCritical/SafeCritical/Transparent, IsFullyTrusted, GetForwardedTypes, ModuleVersionId, FindTypes, and pointer type tests are all back to plain [Fact].

@MichalStrehovsky
Copy link
Member

/azp run runtime-nativeaot-outerloop

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@MichalStrehovsky MichalStrehovsky marked this pull request as ready for review March 3, 2026 23:07
Copilot AI review requested due to automatic review settings March 3, 2026 23:07
Copy link
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

Updates System.Reflection.Context tests to avoid native AOT outerloop failures by conditionally skipping tests that rely on reflection APIs unsupported (or partially unsupported) under native AOT.

Changes:

  • Added [ConditionalFact] guards for method-body reflection APIs (PlatformDetection.IsMethodBodySupported).
  • Added [ConditionalFact] guards for metadata token / resolve APIs (PlatformDetection.IsMetadataTokenSupported).
  • Added [ConditionalFact] guards for native AOT-unsupported APIs (PlatformDetection.IsNotNativeAot) and assembly-files-dependent behavior (PlatformDetection.HasAssemblyFiles).

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 27 comments.

Show a summary per file
File Description
src/libraries/System.Reflection.Context/tests/MoreCoverageTests.cs Skips a method-body-dependent test when method bodies aren’t supported.
src/libraries/System.Reflection.Context/tests/MethodBodyTests.cs Broadly guards method-body tests to avoid native AOT failures.
src/libraries/System.Reflection.Context/tests/FinalCoverageTests2.cs Guards a filter clause test that relies on method body support.
src/libraries/System.Reflection.Context/tests/FinalCoverageTests.cs Guards a constructor method-body projection test.
src/libraries/System.Reflection.Context/tests/FinalBranchCoverageTests.cs Skips GetInterfaceMap coverage on native AOT.
src/libraries/System.Reflection.Context/tests/ExtendedTypeTests.cs Skips enum values + interface map behavior on native AOT.
src/libraries/System.Reflection.Context/tests/ExtendedPropertyInfoTests.cs Guards MetadataToken usage to only run when supported.
src/libraries/System.Reflection.Context/tests/ExtendedFieldInfoTests.cs Guards MetadataToken usage to only run when supported.
src/libraries/System.Reflection.Context/tests/ExtendedAssemblyTests2.cs Skips tests requiring assembly files / satellite assembly APIs on native AOT.
src/libraries/System.Reflection.Context/tests/ExtendedAssemblyTests.cs Skips referenced assemblies + generic invoke scenarios on native AOT.
src/libraries/System.Reflection.Context/tests/CustomTypeTests.cs Guards MetadataToken usage to only run when supported.
src/libraries/System.Reflection.Context/tests/CustomParameterInfoTests.cs Guards MetadataToken usage to only run when supported.
src/libraries/System.Reflection.Context/tests/CustomModuleTests.cs Guards metadata token/resolve APIs; skips IsResource() on native AOT.
src/libraries/System.Reflection.Context/tests/CustomMethodInfoTests.cs Guards method body + metadata token tests for native AOT compatibility.
src/libraries/System.Reflection.Context/tests/CustomEventInfoTests.cs Guards metadata token; skips GetOtherMethods() on native AOT.
src/libraries/System.Reflection.Context/tests/CustomConstructorInfoTests.cs Guards method body + metadata token tests for native AOT compatibility.

You can also share your feedback on Copilot code review. Take the survey.

#89157

Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
@MichalStrehovsky
Copy link
Member

@copilot implement feedback from @copilot code review[agent]

Copilot AI and others added 2 commits March 4, 2026 00:37
…tions

Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
…eption type unknown

Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 4, 2026

@copilot implement feedback from @copilot code review[agent]

Implemented in 04733fe and 7f4e2dd:

  • Moved per-test [ConditionalFact(IsMethodBodySupported)] to class-level [ConditionalClass] for MethodBodyTests, ExceptionHandlingClauseTests, and LocalVariableInfoTests
  • Replaced ConditionalFact skips with platform-branching assertions for GetReferencedAssemblies (asserts PlatformNotSupportedException), GetSatelliteAssembly (asserts PlatformNotSupportedException), and GetModule (validates Module.Name returns "<Unknown>")
  • Kept ConditionalFact for Invoke_OnMadeGenericMethod since the exact native AOT exception type can't be determined without an AOT test run

@MichalStrehovsky
Copy link
Member

/azp run runtime-nativeaot-outerloop

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants