Skip to content

Commit 9f125fb

Browse files
committed
fix(diagnostics): DI fixes lead with IServiceScopeFactory, qualify Func<T> (CodeRabbit)
The built-in Microsoft.Extensions.DependencyInjection container does not auto-resolve Func<T> (it needs manual registration or a third-party container), so the DI001/DI002/DI003 explanation text no longer presents Func<T> as the default fix. Primary guidance is now IServiceScopeFactory + per-use scope creation; Func<T> is mentioned only as a registered/container-dependent option. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KzA6KGQVRzP52tBEKwJ3SW
1 parent ed83428 commit 9f125fb

1 file changed

Lines changed: 10 additions & 5 deletions

File tree

‎ownlang/diagnostics.py‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,10 @@ class Severity(Enum):
101101
"intended per-scope/per-call semantics are lost and it may leak. DI002-DI005 are the "
102102
"specific shapes (singleton->scoped, singleton->transient, scoped-from-root, "
103103
"disposable-transient-from-a-long-scope).\n"
104-
"Fix: don't capture the shorter-lived service directly — inject a factory "
105-
"(`Func<T>` / `IServiceScopeFactory`) and resolve it per use, or align the lifetimes."
104+
"Fix: don't capture the shorter-lived service directly — inject `IServiceScopeFactory`, "
105+
"create a scope per use, and resolve from it (or align the lifetimes). A `Func<T>` "
106+
"factory also works but the built-in container does not auto-resolve `Func<T>` — you must "
107+
"register it (or use a container that does)."
106108
),
107109
"OWN001": (
108110
"An owned resource is acquired but not released on every path out of its owner — "
@@ -151,13 +153,16 @@ class Severity(Enum):
151153
"A singleton captures a scoped service: the scoped instance is pinned to the singleton "
152154
"for the whole app lifetime, defeating per-scope (e.g. per-request) semantics and often "
153155
"leaking a DbContext-like object.\n"
154-
"Fix: don't inject the scoped service into the singleton — inject a factory "
155-
"(`Func<T>` / `IServiceScopeFactory`) and resolve it per use."
156+
"Fix: don't inject the scoped service into the singleton — inject `IServiceScopeFactory`, "
157+
"create a scope per use, and resolve the scoped service from it. (A registered `Func<T>` "
158+
"factory also works, but the built-in container does not auto-resolve `Func<T>`.)"
156159
),
157160
"DI003": (
158161
"A singleton captures a transient service: the transient is created once and lives for "
159162
"the app lifetime, so its intended short life is lost.\n"
160-
"Fix: inject a factory and create the transient per use instead of holding the instance."
163+
"Fix: resolve the transient per use from a scope (`IServiceScopeFactory`) instead of "
164+
"holding the instance — or inject a `Func<T>` factory you have registered (the built-in "
165+
"container does not auto-resolve `Func<T>`)."
161166
),
162167
"DI004": (
163168
"A scoped service is resolved from the root provider, so it is captured for the whole "

0 commit comments

Comments
 (0)