-
Notifications
You must be signed in to change notification settings - Fork 0
feat(extractor): DI registration graph → DI001 captive dependency on real C# (P-006) #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # DI001 end-to-end — the captive-dependency extractor (P-006) | ||
|
|
||
| The DI001 captive-dependency check has lived in the core (`ownlang/di.py`) for a | ||
| while, validated only on hand-written `services` facts. This slice gives it a | ||
| **C# front end**: the Roslyn extractor now builds the registration + constructor | ||
| graph from real code, so **DI001 fires end-to-end on C#** — no hand-authored | ||
| facts. | ||
|
|
||
| ## The bug it catches | ||
|
|
||
| A **singleton** that depends on a **scoped** service captures that scoped instance | ||
| for the whole application lifetime — the classic ASP.NET Core *"Cannot consume | ||
| scoped service from singleton"* bug (a `DbContext` held by a singleton is the | ||
| canonical case: an open connection / request state promoted to process lifetime). | ||
| It is a deterministic property of the **registration graph**, which is exactly | ||
| OwnLang's lifetime ordering spelled in DI terms (`Transient ≲ Scoped < Singleton`). | ||
|
|
||
| The core rule (`di.py`, unchanged): | ||
|
|
||
| - `singleton -> scoped` — captive (the edge is the bug); | ||
| - `singleton -> transient -> scoped` — captive (a transient resolved by a | ||
| singleton is itself singleton-lived, and drags the scoped along); | ||
| - `singleton -> singleton -> scoped` — **not** reported here (the inner singleton | ||
| is the captor and is flagged on its own pass). | ||
|
|
||
| ## What the extractor now reads | ||
|
|
||
| A purely **syntactic** pass over the same parsed trees (no SemanticModel needed — | ||
| in the spirit of the narrow frontend), in two steps: | ||
|
|
||
| 1. **Constructor graph** — every class's widest **public** constructor's parameter | ||
| types (the dependency edges), including a C# 12 **primary constructor** | ||
| (`class Foo(Dep d)`, whose parameters live on the declaration, not a ctor | ||
| member). DI's default provider resolves through public ctors only, so a wider | ||
| non-public ctor's parameters are deliberately not counted (no false captive). | ||
| 2. **Registrations** — every conventional `IServiceCollection` call: | ||
| `AddSingleton` / `AddScoped` / `AddTransient`, in the generic | ||
| `Add*<TService[, TImpl]>` form or the `Add*(typeof(TService)[, typeof(TImpl)])` | ||
| form. | ||
|
|
||
| Each registration emits one `services` fact — the shape the core already consumes: | ||
|
|
||
| ```json | ||
| {"name": "EmailSender", "lifetime": "singleton", "deps": ["AppDbContext"], | ||
| "file": "DiCaptiveSample.cs", "line": 40} | ||
| ``` | ||
|
|
||
| `name` is the **service** type others inject (so an `AddScoped<IRepo, Repo>` keys | ||
| under `IRepo`, and a consumer injecting `IRepo` resolves to it); `deps` are the | ||
| **implementation**'s constructor parameter types; `file`/`line` anchor the finding | ||
| at the registration site. | ||
|
|
||
| ### Non-goals stay silent, never guessed | ||
|
|
||
| Factory lambdas, reflection/assembly scanning, Scrutor, open generics and | ||
| config-driven wiring defeat a static graph. The extractor records what it *can* | ||
| read and treats the rest as an **unknown-dep node** (`deps: []`) — a node others | ||
| can still capture, but one that contributes no edges of its own. Per the P-006 | ||
| non-goals: report the conventional shape, stay silent (not wrong) on the rest. | ||
|
|
||
| ## Validation | ||
|
|
||
| - **Bridge-side, locally** — the `services` facts the extractor emits, fed through | ||
| the real core, produce exactly the four expected DI001s (direct, transitive, | ||
| interface, primary-ctor) and stay silent on the singleton→singleton edge and the | ||
| public-ctor-only service. (No local .NET SDK, so the C#→facts step itself is | ||
| validated in CI, like the rest of the frontend.) | ||
| - **End-to-end, in CI** — `frontend/roslyn/samples/DiCaptiveSample.cs` is wired | ||
| into the `wpf-extractor` job. The assertions pin: a direct capture | ||
| (`EmailSender -> AppDbContext`), a transitive one through a transient | ||
| (`ReportService -> UnitOfWork -> AppDbContext`), an interface-registration one | ||
| (`CacheService -> IRepo`), a C# 12 primary-constructor one | ||
| (`PrimaryCtorService -> AppDbContext`), **exactly four** findings, and silence on | ||
| `Metrics -> Clock` (singleton→singleton) and `PublicCtorOnly` (DI uses its public | ||
| parameterless ctor; the wider private ctor's scoped dep is never resolved). | ||
|
|
||
| ## Why it matters for differentiation | ||
|
|
||
| The captive dependency is an **ASP.NET-specific lifetime contract** that | ||
| general-purpose analyzers don't model — the same complementary story as the | ||
| subscription-leak class the cross-tool oracle already documented (CodeQL and | ||
| Infer# cover the Dispose/RAII family; neither flags these). It is also a clean | ||
| reuse win: a whole new diagnostic class on real C# with **zero core changes** — | ||
| the frontend just produces the `services` facts the lifetime core already checks. | ||
|
|
||
| ## Next (separate slices) | ||
|
|
||
| - **DI002** — a singleton capturing a scoped dependency *weakly* | ||
| (`WeakReference<Scoped>`): a warning, since a weak reference fixes retention but | ||
| not the lifetime-contract violation. | ||
| - **DI003** — a transient `IDisposable` resolved from the **root** provider, never | ||
| disposed until the app exits (a slow leak). | ||
| - Anchoring the finding at the **consuming constructor** as well as the | ||
| registration site (P-006 open question #1), with the capture path shown. |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a registered service uses C# 12 primary-constructor injection, such as
public sealed class EmailSender(AppDbContext db), there is noConstructorDeclarationSyntaxmember; the parameters live on the class declaration itself. This loop leaveswidestnull and emits an empty dependency list, so DI001 misses singleton-to-scoped captures in current .NET 8/ASP.NET-style code.Useful? React with 👍 / 👎.