Add VSMEF003: flag [Export(typeof(T))] when exporting class doesn't implement T#679
Draft
Add VSMEF003: flag [Export(typeof(T))] when exporting class doesn't implement T#679
Conversation
…tructor, add tests Co-authored-by: AArnott <3548+AArnott@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add analyzer to validate type export implementations
Add VSMEF003: flag [Export(typeof(T))] when exporting class doesn't implement T
Feb 24, 2026
Member
|
@drewnoakes All this added was support for |
Member
|
@AArnott indeed, it seems the issue I originally reported should have been covered. I don't think we can reliably report on subtypes of ExportAttribute. Let's close this out. I had seen an issue around this, but I don't remember the details. I'll keep an eye out and share an example if one arises. |
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.
Adds the
VSMEF003analyzer that warns when[Export(typeof(T))]is applied to a class that doesn't implement or inheritT, catching authoring mistakes that would cause runtime composition failures.Also checks property-level exports where the property type is incompatible with the exported contract type.
Analyzer implementation
NamedTypeandPropertysymbols; checks all[Export(typeof(T))]attributesUtils.IsExportAttribute()(walks attribute inheritance hierarchy) to cover customExportAttributesubclasses, consistent with other analyzers in the projectTryGetExportedType()which handles both:[Export(typeof(T))]— single-arg form (MEF v1 + v2)[Export("contractName", typeof(T))]— two-arg form (MEF v1 only)VSMEF003)Test coverage
Tests added for: unimplemented interface/base class, generic and unbound-generic exports, VB, MEF v1 two-arg contract name form, custom
ExportAttributesubclasses, property exports, and valid cases that produce no diagnostic.Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
h8evsblobprodwus2120.vsblob.vsassets.io/opt/hostedtoolcache/dotnet/dotnet /opt/hostedtoolcache/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/vs-mef/vs-mef/bin/Tests/Microsoft.VisualStudio.Composition.Analyzers.Tests/Release/net8.0/Microsoft.VisualStudio.Composition.Analyzers.Tests.runtimeconfig.json --depsfile /home/REDACTED/work/vs-mef/vs-mef/bin/Tests/Microsoft.VisualStudio.Composition.Analyzers.Tests/Release/net8.0/Microsoft.VisualStudio.Composition.Analyzers.Tests.deps.json /home/REDACTED/work/vs-mef/vs-mef/bin/Tests/Microsoft.VisualStudio.Composition.Analyzers.Tests/Release/net8.0/testhost.dll --port 46727 --endpoint 127.0.0.1:046727 --role client --parentprocessid 5514 --telemetryoptedin false(dns block)/opt/hostedtoolcache/dotnet/dotnet /opt/hostedtoolcache/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/vs-mef/vs-mef/bin/Tests/Microsoft.VisualStudio.Composition.Analyzers.Tests/Release/net8.0/Microsoft.VisualStudio.Composition.Analyzers.Tests.runtimeconfig.json --depsfile /home/REDACTED/work/vs-mef/vs-mef/bin/Tests/Microsoft.VisualStudio.Composition.Analyzers.Tests/Release/net8.0/Microsoft.VisualStudio.Composition.Analyzers.Tests.deps.json /home/REDACTED/work/vs-mef/vs-mef/bin/Tests/Microsoft.VisualStudio.Composition.Analyzers.Tests/Release/net8.0/testhost.dll --port 40403 --endpoint 127.0.0.1:040403 --role client --parentprocessid 12790 --telemetryoptedin false(dns block)/opt/hostedtoolcache/dotnet/dotnet /opt/hostedtoolcache/dotnet/dotnet exec --runtimeconfig /home/REDACTED/work/vs-mef/vs-mef/bin/Tests/Microsoft.VisualStudio.Composition.Analyzers.Tests/Release/net8.0/Microsoft.VisualStudio.Composition.Analyzers.Tests.runtimeconfig.json --depsfile /home/REDACTED/work/vs-mef/vs-mef/bin/Tests/Microsoft.VisualStudio.Composition.Analyzers.Tests/Release/net8.0/Microsoft.VisualStudio.Composition.Analyzers.Tests.deps.json /home/REDACTED/work/vs-mef/vs-mef/bin/Tests/Microsoft.VisualStudio.Composition.Analyzers.Tests/Release/net8.0/testhost.dll --port 34929 --endpoint 127.0.0.1:034929 --role client --parentprocessid 20001 --telemetryoptedin false(dns block)xofvsblobprodwus2114.vsblob.vsassets.io/opt/hostedtoolcache/dotnet/dotnet dotnet build src/Microsoft.VisualStudio.Composition.Analyzers/Microsoft.VisualStudio.Composition.Analyzers.csproj -c Release(dns block)/opt/hostedtoolcache/dotnet/dotnet dotnet build test/Microsoft.VisualStudio.Composition.Analyzers.Tests/Microsoft.VisualStudio.Composition.Analyzers.Tests.csproj -c Release(dns block)If you need me to access, download, or install something from one of these locations, you can either:
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.