Skip to content

Add analyzer and telemetry support for [DependsOn] #10259

Description

@Evangelink

Follow-up to the [DependsOn] test-dependency feature (RFC 022). The attribute ships without any analyzer or telemetry support, which diverges from the closest precedent — [ResourceLock] (RFC 020) shipped with a WellKnownTypeNames constant, a diagnostic ID, an analyzer and an AnalyzerReleases.Unshipped.md entry.

Nothing here is a correctness problem: the runtime already reports unresolvable references and cycles at run time. These are build-time and observability gaps.

1. Unresolvable / invalid reference analyzer

[DependsOn] references a method by string name. Today a typo or a rename is only discovered at run time, as a warning:

Test 'X' declares a dependency on 'Y', which matches no test in this run. The dependency is ignored.

That warning deliberately does not fail the run, because a reference that matches nothing must stay non-fatal for --filter and single-test runs to keep working (see the RFC's rationale). A build-time analyzer is the right place to catch the cases that are statically decidable:

  • the named method does not exist on the target type;
  • the named method exists but is not a test method;
  • a self-reference written directly on a method;
  • a cycle that is visible within a single compilation.

This is what makes the nameof/typeof shape of the API pay off, and it is the main reason the RFC lists it as future work.

2. [DependsOn] on a non-test method is silently inert

UseAttributeOnTestMethodAnalyzer's RuleTuples is a closed set (Owner / Priority / Description / TestProperty / WorkItem / ConditionBase). [DoNotParallelize], [Parallelize] and [ResourceLock] are all absent from it too, so [DependsOn]'s absence is consistent — but it means putting the attribute on a non-test method produces no diagnostic at all.

Worth deciding whether this family of scheduling attributes should be covered as a group rather than one at a time.

3. WellKnownTypeNames registration

DependsOnAttribute is not registered in src/Analyzers/MSTest.Analyzers/Helpers/WellKnownTypeNames.cs (it would sit near DoNotParallelizeAttribute at :26 / ResourceLockAttribute at :38). Deliberately not added in the feature PR, since an unused constant is dead code until an analyzer consumes it — it should land with item 1.

4. Telemetry

MSTestTelemetryDataCollector (src/Adapter/MSTestAdapter.PlatformServices/Telemetry/MSTestTelemetryDataCollector.cs:124-140) does not track [DependsOn] usage, so there will be no adoption data for a new opt-in feature. [ResourceLock] has the same gap.

5. NativeAOT source generation

src/Analyzers/MSTest.SourceGeneration/Helpers/MSTestAttributeNames.cs:31 does not know about [DependsOn], so dependencies are silently ignored on that path. Pre-existing pattern ([ResourceLock] is missing too), not a regression — but it should be closed for both.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/tech-debtCode health, refactoring, simplification.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions