Skip to content

Ensure type/method refs use target module's core library#1824

Merged
Bertk merged 3 commits intocoverlet-coverage:masterfrom
Bertk:fix-instrumenter-net481
Feb 28, 2026
Merged

Ensure type/method refs use target module's core library#1824
Bertk merged 3 commits intocoverlet-coverage:masterfrom
Bertk:fix-instrumenter-net481

Conversation

@Bertk
Copy link
Copy Markdown
Collaborator

@Bertk Bertk commented Feb 19, 2026

Replaced direct ImportReference calls with new helpers ImportToCoreLibrary and ImportMethodToCoreLibrary to guarantee that all type and method references are correctly scoped to the instrumented module's core library (e.g., mscorlib or System.Private.CoreLib). This prevents incorrect references to System.Runtime from the coverlet assembly and improves compatibility and correctness across different .NET runtimes. All relevant field, parameter, variable, and exception handler type references are now properly redirected.

#1818

Add ImportToCoreLibrary helpers to redirect type and method references to mscorlib when instrumenting .NET Framework targets. This ensures compatibility when the coverlet assembly (built for .NET 8) references types through System.Runtime, which does not exist on .NET Framework. Replaces direct ImportReference calls with these helpers to prevent runtime errors and ensure correct cross-targeting behavior.
@Bertk Bertk force-pushed the fix-instrumenter-net481 branch from 0bb087b to b2573d6 Compare February 19, 2026 17:05
@Bertk
Copy link
Copy Markdown
Collaborator Author

Bertk commented Feb 19, 2026

Summary of the Fix

The fix uses a conditional approach:

  1. IsNetFrameworkTarget(module) - Checks if module.TypeSystem.CoreLibrary.Name == "mscorlib" which indicates a .NET Framework target.
  2. For .NET Framework targets (IsNetFrameworkTarget returns true):
    • Redirects type scopes from System.Runtime to mscorlib
    • This fixes the FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=8.0.0.0' issue
  3. For .NET Core/.NET 5+ targets (IsNetFrameworkTarget returns false):
    • Just calls module.ImportReference(type) and returns the result unchanged
    • This preserves the original working behavior
    Please run the integration tests again after rebuilding and repacking. The tests should:
    • ✅ Pass for .NET Core targets - original behavior preserved
    • ✅ Pass for .NET Framework targets - scope redirected to mscorlib (you'll need a .NET Framework test to verify this)

@Bertk Bertk marked this pull request as ready for review February 19, 2026 17:27
Copilot AI review requested due to automatic review settings February 19, 2026 17:27
@Bertk Bertk added bug Something isn't working coverlet-core netfx Issue happens only on .NET Framework version labels Feb 19, 2026
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 adjusts how Instrumenter imports type and method references when injecting the ModuleTrackerTemplate into an instrumented module, aiming to ensure references are scoped to the target module’s core library (not the coverlet assembly’s).

Changes:

  • Replaced several module.ImportReference(...) calls with new helper methods intended to redirect scopes to the target module’s core library for .NET Framework targets.
  • Added IsNetFrameworkTarget, ImportToCoreLibrary, and ImportMethodToCoreLibrary helpers to centralize the redirection logic.
  • Applied the helpers to imported field types, parameter/variable types, instruction operands, and exception handler catch types during tracker injection.

Add deletion of coverage.net9.0.cobertura.xml file.
Introduce a regression test to verify that the injected Tracker class in instrumented .NET Framework assemblies references mscorlib instead of System.Runtime. The test uses Mono.Cecil to inspect type references and is only run on Windows if the sample assembly is available. Also, update usages of Document to explicitly reference Coverlet.Core.Instrumentation.Document for clarity.
@Bertk Bertk assigned Bertk and unassigned daveMueller Feb 23, 2026
@Bertk Bertk requested review from daveMueller and removed request for daveMueller February 23, 2026 16:21
@Bertk Bertk merged commit 7d599be into coverlet-coverage:master Feb 28, 2026
11 checks passed
@Bertk Bertk deleted the fix-instrumenter-net481 branch February 28, 2026 07:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working coverlet-core netfx Issue happens only on .NET Framework version

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants