Skip to content

Add VSMEF003 analyzer to detect export type mismatches#601

Merged
AArnott merged 5 commits intomainfrom
copilot/fix-79
Jul 29, 2025
Merged

Add VSMEF003 analyzer to detect export type mismatches#601
AArnott merged 5 commits intomainfrom
copilot/fix-79

Conversation

Copy link
Contributor

Copilot AI commented Jul 25, 2025

This PR implements a new Roslyn analyzer VSMEF003 that detects when MEF [Export(typeof(T))] attributes specify a type that is not implemented by the exporting class.

Problem

Previously, MEF parts could declare exports for types they don't actually implement, leading to potential runtime composition failures:

interface ICalculator
{
    int Add(int a, int b);
}

[Export(typeof(ICalculator))]  // ❌ Should warn - TextProcessor doesn't implement ICalculator
public class TextProcessor
{
    public string ProcessText(string input) => input.ToUpper();
}

Solution

The new analyzer warns when:

  • A class exports an interface it doesn't implement
  • A class exports a base class it doesn't inherit from
  • A class exports an unrelated type

The analyzer correctly handles:

  • Both MEF v1 (System.ComponentModel.Composition) and MEF v2 (System.Composition) attributes
  • Interface implementation checking via AllInterfaces
  • Class inheritance checking via base type traversal
  • Self-exports (exporting the class's own type)

Implementation Details

  • Analyzer ID: VSMEF003
  • Severity: Warning (not error, as this may be intentional in some advanced scenarios)
  • Category: Usage
  • Targets: Classes with [Export(typeof(T))] attributes
  • Type Compatibility: Uses Roslyn's INamedTypeSymbol and SymbolEqualityComparer for robust type checking

Testing

Added comprehensive test cases covering:

  • Valid scenarios (no diagnostics): Interface implementation, class inheritance, self-exports
  • Invalid scenarios (diagnostics): Unimplemented interfaces, unrelated classes
  • Edge cases: Multiple exports, non-class types, contract name exports
  • Both C# and VB.NET support

The core type compatibility logic was validated with a standalone test program confirming correct behavior for interface implementation and class inheritance detection.

Documentation

  • Updated doc/analyzers/index.md with new analyzer entry
  • Created detailed doc/analyzers/VSMEF003.md with examples and guidance
  • Added entry to AnalyzerReleases.Unshipped.md
  • Updated README with analyzer description

Fixes #79.


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

Copilot AI and others added 2 commits July 25, 2025 20:54
Co-authored-by: AArnott <3548+AArnott@users.noreply.github.com>
Co-authored-by: AArnott <3548+AArnott@users.noreply.github.com>
Copilot AI changed the title [WIP] Analyze MEF exports for correctness Add VSMEF003 analyzer to detect export type mismatches Jul 25, 2025
Copilot AI requested a review from AArnott July 25, 2025 21:02
@AArnott AArnott marked this pull request as ready for review July 26, 2025 04:27
@AArnott
Copy link
Member

AArnott commented Jul 26, 2025

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@AArnott AArnott enabled auto-merge July 26, 2025 04:47
@AArnott AArnott merged commit 06b1548 into main Jul 29, 2025
6 checks passed
@AArnott AArnott deleted the copilot/fix-79 branch July 29, 2025 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Analyze MEF exports for correctness

3 participants