-
Notifications
You must be signed in to change notification settings - Fork 849
Fix CS0757 error when multi-targeting with Microsoft.Extensions.Telemetry.Abstractions #7062
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: evgenyfedorov2 <[email protected]>
…gging.Abstractions Co-authored-by: evgenyfedorov2 <[email protected]>
...metry.Abstractions/buildTransitive/net10.0/Microsoft.Extensions.Telemetry.Abstractions.props
Show resolved
Hide resolved
...emetry.Abstractions/buildTransitive/net9.0/Microsoft.Extensions.Telemetry.Abstractions.props
Show resolved
Hide resolved
The net8.0 buildTransitive folder is automatically selected by NuGet for net9.0 and net10.0 through nearest compatible framework selection. Only the target name fix was needed. Co-authored-by: evgenyfedorov2 <[email protected]>
Instead of TFM-specific folders (net6.0, net8.0, net9.0, net10.0), use generic buildTransitive files that apply to all target frameworks. This prevents the issue where NuGet's framework selection doesn't automatically apply net8.0 files to net9.0. - Moved buildTransitive files to root of buildTransitive folder (no TFM subfolder) - Disabled automatic TFM-specific buildTransitive generation via DisableNETStandardCompatErrors - Updated packaging to only include root-level buildTransitive files Co-authored-by: evgenyfedorov2 <[email protected]>
|
@copilot good that you removed net8.0 folder, but what about net6.0 folder? |
...crosoft.Extensions.Telemetry.Abstractions/Microsoft.Extensions.Telemetry.Abstractions.csproj
Outdated
Show resolved
Hide resolved
The net6.0 folder was also removed in commit 25054c3. The files from net6.0/ were moved to the root buildTransitive/ folder (no subfolder) to make them generic and apply to all TFMs. The package now only contains:
No TFM-specific subfolders remain (no net6.0, net8.0, net9.0, or net10.0). |
|
@copilot are you available? |
There was a problem hiding this 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 fixes CS0757 compilation errors when using Microsoft.Extensions.Telemetry.Abstractions in multi-targeting projects (e.g., targeting both net9.0 and net10.0). The error occurred because MSBuild targets in both Microsoft.Extensions.Logging.Abstractions and Microsoft.Extensions.Telemetry.Abstractions had identical names, causing the Telemetry.Abstractions targets to be overridden and preventing proper removal of duplicate source generators.
Key Changes:
- Renamed MSBuild targets from
_Microsoft_Extensions_Logging_AbstractionsRemoveAnalyzers*to_Microsoft_Extensions_Telemetry_AbstractionsRemoveAnalyzers*to prevent name collision - Switched from TFM-specific buildTransitive files (net8.0/) to generic buildTransitive files that apply to all target frameworks
- Added
DisableNETStandardCompatErrors=trueto prevent automatic TFM-specific buildTransitive folder generation
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/buildTransitive/net8.0/Microsoft.Extensions.Telemetry.Abstractions.targets | Deleted TFM-specific targets file in favor of generic approach |
| src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/buildTransitive/net8.0/Microsoft.Extensions.Telemetry.Abstractions.props | Deleted TFM-specific props file in favor of generic approach |
| src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/buildTransitive/Microsoft.Extensions.Telemetry.Abstractions.targets | Created generic targets file with renamed targets to avoid collision |
| src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/buildTransitive/Microsoft.Extensions.Telemetry.Abstractions.props | Created generic props file that applies to all target frameworks |
| src/Libraries/Microsoft.Extensions.Telemetry.Abstractions/Microsoft.Extensions.Telemetry.Abstractions.csproj | Updated packaging configuration to use generic buildTransitive files and disable automatic TFM-specific generation |
|
@joperezr could you review this, please? |
...crosoft.Extensions.Telemetry.Abstractions/Microsoft.Extensions.Telemetry.Abstractions.csproj
Outdated
Show resolved
Hide resolved
...crosoft.Extensions.Telemetry.Abstractions/Microsoft.Extensions.Telemetry.Abstractions.csproj
Show resolved
Hide resolved
|
@copilot please return back framework specific buildTransitive files and folders, please also add one for net9.0 |
- Reverted from generic buildTransitive files to TFM-specific folders (net6.0, net8.0, net9.0) - Restored _AdditionalNETStandardCompatErrorFileContents for net462 compatibility - Kept renamed targets (_Microsoft_Extensions_Telemetry_AbstractionsRemoveAnalyzers) to prevent collision - This addresses feedback that generic buildTransitive files get overridden by netstandard compat errors Co-authored-by: evgenyfedorov2 <[email protected]>
Done in d864ca3. Restored framework-specific buildTransitive folders (net6.0, net8.0) and added net9.0 as requested. Also restored the _AdditionalNETStandardCompatErrorFileContents section that was needed for net462. The renamed targets are kept to prevent the name collision issue. |
|
@copilot please check if the bug has been fixed with these changes. |
Yes, the bug has been fixed. Verified with comprehensive testing: ✅ Multi-targeting test (net9.0 + net10.0): Build succeeded with 0 errors The fix works by:
No CS0757 errors occur when multi-targeting with Microsoft.Extensions.Telemetry.Abstractions. |
|
So I reverted most of the changes, and have tested everything as it is with internal build and confirm that it does fix the bug. @joperezr could you please review again? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
...emetry.Abstractions/buildTransitive/net9.0/Microsoft.Extensions.Telemetry.Abstractions.props
Show resolved
Hide resolved
| @@ -1,6 +1,6 @@ | |||
| <Project> | |||
| <!-- This package should replace the "Microsoft.Extensions.Logging.Abstractions" source generator. --> | |||
| <Target Name="_Microsoft_Extensions_Logging_AbstractionsRemoveAnalyzers" | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fact that the two targets had the same name was very intentional, as we want to make sure that the targets are replaced such that only one ever runs in a project evaluation. Do we have a repro project where we can clearly see the issue? I'd like to see that to better understand the problem and validate the RCA made here, as I don't think that target naming colissions was the problem.
| @@ -0,0 +1,36 @@ | |||
| <Project> | |||
| <!-- This package should replace the "Microsoft.Extensions.Logging.Abstractions" source generator. --> | |||
| <Target Name="_Microsoft_Extensions_Telemetry_AbstractionsRemoveAnalyzers" | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you help me understand why this new net9 folder is needed? the way buildTransitive folder works, should make it such that if a net9 folder is not present but the app targets net9, it should just fallback and import the files from net8.
|
I think the best course of action here before we make a change is to be absolutely sure of what is causing the issue and what the right fix is. Once that RCA is done, we should also make sure we add a test to protect against regressions as part of the PR |
|
Here is some historical context about why we added this as is and how it works today: Historical Context SummaryOriginal Design (PR #4238 - Aug 2023) Goal: When Microsoft.Extensions.Telemetry.Abstractions is referenced, disable the Microsoft.Extensions.Logging.Abstractions source generator so the extensions' generator takes precedence. Mechanism: Set DisableMicrosoftExtensionsLoggingSourceGenerator=true in a .props file, which the Logging.Abstractions package checks to disable its generator. Follow-up Fix (PR #4287 - Aug 2023) Problem discovered: For ASP.NET Core web projects, the logging generator also comes from the ASP.NET Core targeting pack (not just the NuGet package). The original fix only disabled the NuGet package's analyzer. Solution: You added a target named _Microsoft_Extensions_Logging_AbstractionsRemoveAnalyzers that explicitly removes the analyzer from the Analyzer item group. Why the Same Target Name Was Intentional (from your own comment in PR #4287): |
Multi-targeting projects using
Microsoft.Extensions.Telemetry.Abstractionswith net9.0 or net10.0 fail with CS0757 ("A partial method may not have multiple implementing declarations") because bothMicrosoft.Gen.LoggingandMicrosoft.Extensions.Logging.Abstractionssource generators execute, producing duplicateLoggerMessageimplementations.Root Cause
Target name collision: MSBuild target
_Microsoft_Extensions_Logging_AbstractionsRemoveAnalyzersin Telemetry.Abstractions was overridden by the identically-named target in Logging.Abstractions, preventing removal of framework pack analyzers.Changes
_Microsoft_Extensions_Telemetry_AbstractionsRemoveAnalyzers*to prevent name collision with Logging.Abstractions targets_AdditionalNETStandardCompatErrorFileContentsfor net462 compatibilityHow It Works
The renamed targets now execute properly without being overridden by Logging.Abstractions targets and remove both:
Framework-specific buildTransitive folders ensure proper NuGet package resolution for each target framework without interference from SDK-generated compatibility files.
Reproduction
Fails before fix with CS0757, succeeds after.
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.
Microsoft Reviewers: Open in CodeFlow