Skip to content

feat(di): DI002 transitive — a singleton weakly holding a transient that drags in a scoped - #64

Merged
PhysShell merged 2 commits into
mainfrom
claude/di002-transitive
Jun 21, 2026
Merged

PhysShell merged 2 commits into
mainfrom
claude/di002-transitive

Conversation

@PhysShell

@PhysShell PhysShell commented Jun 21, 2026 •

Copy link
Copy Markdown
Owner

The transitive form of DI002

Extends DI002 (shipped direct in #63) to the transitive case: a singleton holding a WeakReference<Transient> whose transient strongly depends on a scoped service.

public sealed class UnitOfWork { public UnitOfWork(AppDbContext db) { } }   // transient -> scoped
public sealed class WeakReport { public WeakReport(WeakReference<UnitOfWork> uow) { } }  // singleton
services.AddTransient<UnitOfWork>();
services.AddSingleton<WeakReport>();   // DI002 — WeakReport -> UnitOfWork -> AppDbContext

The weak edge enters the transient, but the scoped it drags in is still root-resolved and app-lived — a captive one hop below the weak reference. The weak ref keeps the entry edge off the DI001 strong graph, but the captive scoped underneath is the smell → DI002 (warning), with the full path shown.

How

find_weak_captive_dependencies now runs the same strong-edge DFS DI001 does, rooted at each weak dep instead of the strong deps: a scoped reached directly (WeakReference<Scoped>) or through a weakly-held transient's strong deps is reported; transients are followed, singleton edges are another singleton's own pass, cycles guarded. Core-only — the extractor already emits weak_deps and the transient's strong deps.

Pinned in CI

DiCaptiveSample.cs adds WeakReport, so the wpf-extractor job now asserts exactly 3 DI002:

singleton weak path
WeakCache → AppDbContext direct (#63)
WeakCacheOpt → AppDbContext nullable direct (#63)
WeakReport → UnitOfWork → AppDbContext transitive (this PR)

WeakReport carries the path WeakReport -> UnitOfWork -> AppDbContext; the existing exactly-4-DI001 / exactly-1-DI003 counts and WeakClockHolder silence are unchanged. The unit test asserts the set + the transitive path.

Validated: ruff + mypy clean, ownir 98/98, the 3-DI002 bridge with the transitive path.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Rg8kSk1YT14x7A1vo5zgED


Generated by Claude Code

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Improved DI002 detection to surface scoped services that are captured via transitive chains from weakly-held singleton dependencies.
  • Documentation

    • Refreshed the DI002 guidance to clarify direct versus transitive weak-capture behavior and how dependency paths are determined.
  • Tests

    • Expanded DI002 test scenarios to include a transitive case and validate the reported dependency path output.
    • Updated CI self-test expectations to match the enhanced detection results.

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

2 participants