Move diagnostics out of DownlevelLibraryImportGenerator into a separate analyzer#124670
Open
Move diagnostics out of DownlevelLibraryImportGenerator into a separate analyzer#124670
Conversation
…lyzer Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Move diagnostics from DownlevelLibraryImportGenerator to analyzer
Move diagnostics out of DownlevelLibraryImportGenerator into a separate analyzer
Feb 20, 2026
Member
jkoritzinsky
left a comment
There was a problem hiding this comment.
@copilot please address this feedback
...ropServices/gen/DownlevelLibraryImportGenerator/DownlevelLibraryImportDiagnosticsAnalyzer.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request extracts diagnostic reporting from DownlevelLibraryImportGenerator into a dedicated DownlevelLibraryImportDiagnosticsAnalyzer, mirroring the architectural changes made in PR #123780 for LibraryImportGenerator. This separation follows Roslyn team recommendations that generators should focus on code generation while analyzers handle diagnostics.
Changes:
- Created new
DownlevelLibraryImportDiagnosticsAnalyzerthat independently reports all diagnostics previously emitted by the generator - Simplified
DownlevelLibraryImportGeneratorto filter invalid methods using the analyzer's validation logic and generate code using discarding diagnostics bags - Removed
Comparers.csas it's no longer needed after eliminating tuple return types from the pipeline - Updated test to use
DownlevelLibraryImportDiagnosticsAnalyzerinstead ofEmptyDiagnosticAnalyzer
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| DownlevelLibraryImportDiagnosticsAnalyzer.cs | New analyzer that reports all diagnostics for LibraryImport methods in downlevel frameworks, including method signature validation, LCIDConversion unsupported, invalid StringMarshalling configurations, and CannotForwardToDllImport errors |
| DownlevelLibraryImportGenerator.cs | Removed diagnostic reporting; simplified to return MemberDeclarationSyntax directly; filters methods via analyzer's GetDiagnosticIfInvalidMethodForGeneration; uses discarding diagnostics bags internally |
| Comparers.cs | Deleted entire file - no longer needed after removing tuple comparisons from pipeline |
| Diagnostics.cs | Updated StringMarshallingForwardingNotSupported_ReportsDiagnostic test to use DownlevelLibraryImportDiagnosticsAnalyzer |
jkoritzinsky
approved these changes
Feb 20, 2026
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.
Mirrors what PR #123780 did for
LibraryImportGenerator: extracts all diagnostic reporting fromDownlevelLibraryImportGeneratorinto a dedicatedDownlevelLibraryImportDiagnosticsAnalyzer, so generators never emit diagnostics directly.Description
New
DownlevelLibraryImportDiagnosticsAnalyzer—[DiagnosticAnalyzer]that replicates all diagnostic logic previously inline in the generator:LCIDConversionnot supportedInvalidStringMarshallingConfigurationCannotForwardToDllImportfor non-Utf16StringMarshallingin downlevel forwarder stubsRequiresAllowUnsafeBlocks(once per compilation, only registered whenAllowUnsafeis false to avoid unnecessary callback overhead)GetDiagnosticIfInvalidMethodForGenerationasinternal staticfor use by the generatorDownlevelLibraryImportGeneratorsimplifications:Diagnosticsfield fromIncrementalStubGenerationContextIncrementalValuesProvider<MemberDeclarationSyntax>(no tuple)CalculateStubInformationandGenerateSourceuse discarding diagnostics bagsPrintForwarderStubno longer reportsCannotForwardToDllImport; filtered viaDownlevelLibraryImportDiagnosticsAnalyzer.GetDiagnosticIfInvalidMethodForGenerationComparers.cs(tuple comparer no longer needed)Test update:
StringMarshallingForwardingNotSupported_ReportsDiagnosticnow usesDownlevelLibraryImportDiagnosticsAnalyzerinstead ofEmptyDiagnosticAnalyzer💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.