Ensure type/method refs use target module's core library#1824
Merged
Bertk merged 3 commits intocoverlet-coverage:masterfrom Feb 28, 2026
Merged
Ensure type/method refs use target module's core library#1824Bertk merged 3 commits intocoverlet-coverage:masterfrom
Bertk merged 3 commits intocoverlet-coverage:masterfrom
Conversation
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.
0bb087b to
b2573d6
Compare
Collaborator
Author
Summary of the FixThe fix uses a conditional approach:
|
Contributor
There was a problem hiding this comment.
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, andImportMethodToCoreLibraryhelpers 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.
Closed
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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