microsoft / microsoft/vs-mef

Analyzer to prevent imports of certain types

Open
#666 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

analyzers enhancement
Dominant language
C#
Stars
464
Forks
93
Avg merge
3d 8h
Merged PRs (30d)
13

Description

In VS, some types are available via MEF imports, while others must be sourced via other means (such as IServiceProvider).

It's relatively easy to accidentally attempt to import such types via MEF without realising they will fail at runtime. This is a relatively common bug. Failures are often relatively uneventful -- an error is logged in a place you have to know to look in, and any observed behavioural issues in the product are unlikely to point at the root of the problem.

We should add an analyzer that bans the importing of certain types via MEF. For example:

  • JoinableTaskFactory
  • IComponentModel
  • IVs* interfaces
  • EnvDTE.*

The analyzer might also ban attempting to export these types.

We could ship a ban-list of types to use by default, and allow consumers to override and/or extend that list for their own purposes. Similar mechanisms exist for banning APIs (in Roslyn analyzers) and defining APIs that require/asset the main thread (in vs-threading -- relevant code).

We should support banning type names using wildcards.

A text file that lists banned types should have a descriptive message for each entry that explains how to obtain the service correctly. For example:

[Microsoft.VisualStudio.Threading.JoinableTaskFactory] Import JoinableTaskContext and use its Factory property instead
[Microsoft.Internal.VisualStudio.Shell.Interop.IVs*] Import IVsService<> instead

Any errors in the definition of these rules should trigger their own diagnostic errors.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the linked VSSDK-Analyzers AdditionalFilesHelpers.cs and the MEF import/export behavior described in the issue. Define the scope for wildcard ban rules, default and consumer-supplied lists, descriptive messages, and diagnostics for invalid rule definitions; the work is done when these requirements are supported for the listed service types.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.