Skip to content

Analyzer to prevent imports of certain types #666

@drewnoakes

Description

@drewnoakes

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions