Detect Static Dependencies
Scan a C# codebase for calls to hard-to-test static APIs and produce a ranked report showing which statics appear most frequently, which files are most affected, and which abstractions already exist in the .NET ecosystem to replace them.
When to Use
- Auditing a project's testability before adding unit tests
- Understanding the scope of static coupling in a legacy codebase
- Prioritizing which statics to wrap first (highest-frequency wins)
- Creating a migration plan for incremental testability improvements
Response Guidelines
- Scale the response to the user's request. A question about a specific category (e.g., "find time statics") should focus on that category with file locations and counts, not produce a full report across all categories.
- When the user provides a specific file or directory path, scan only that scope — do not expand to the entire solution unless asked.
- The full structured report format in Step 4 is for comprehensive audit requests. For focused questions, return only the relevant subset (e.g., category summary + affected files for the requested category).
When Not to Use
- The user wants wrappers generated (hand off to
generate-testability-wrappers)
- The user wants mechanical migration done (hand off to
migrate-static-to-wrapper)
- The statics are already behind interfaces or
TimeProvider
- The code is not C# / .NET
Inputs
| Input |
Required |
Description |
| Target path |
Yes |
A file, directory, project (.csproj), or solution (.sln) to scan |
| Exclusion patterns |
No |
Glob patterns to skip (e.g., **/obj/**, **/Migrations/**) |
| Category filter |
No |
Limit to specific categories: time, filesystem, environment, network, console, process |
Workflow
Step 1: Determine scan scope
Resolve the target to a set of .cs files:
- If a
.cs file, scan that single file.
- If a directory, scan all
.cs files recursively (excluding obj/, bin/).
- If a
.csproj, find its directory and scan .cs files within.
- If a
.sln, parse it, find all project directories, and scan .cs files across all projects.
Always exclude obj/, bin/, and any user-specified exclusion patterns.
Step 2: Search for static dependency patterns
Scan each file for calls matching these categories:
| Category |
Patterns to search for |
Recommended replacement |
| Time |
DateTime.Now, DateTime.UtcNow, DateTime.Today, DateTimeOffset.Now, DateTimeOffset.UtcNow, Task.Delay(, new CancellationTokenSource(TimeSpan |
TimeProvider (.NET 8+) |
| File System |
File.ReadAllText(, File.WriteAllText(, File.Exists(, File.Delete(, File.Copy(, File.Move(, Directory.Exists(, Directory.CreateDirectory(, Directory.GetFiles(, Directory.Delete(, Path.Combine(, Path.GetTempPath( |
IFileSystem (System.IO.Abstractions NuGet) |
| Environment |
Environment.GetEnvironmentVariable(, Environment.SetEnvironmentVariable(, Environment.MachineName, Environment.UserName, Environment.CurrentDirectory, Environment.Exit( |
Custom IEnvironmentProvider |
| Network |
new HttpClient(, HttpClient.GetAsync(, HttpClient.PostAsync(, HttpClient.SendAsync( |
IHttpClientFactory (built-in) |
| Console |
Console.WriteLine(, Console.ReadLine(, Console.Write(, Console.ReadKey( |
IConsole wrapper or ILogger |
| Process |
Process.Start(, Process.GetCurrentProcess(, Process.GetProcessesByName( |
Custom IProcessRunner |
Step 3: Aggregate and rank results
Count each static call pattern across the entire scan scope. Produce a summary with:
- Category summary — total call sites per category (time, filesystem, env, etc.)
- Top patterns — the 10 most frequent individual patterns ranked by count
- Most affected files — files with the highest number of static dependencies
- Existing abstractions available — for each category, note the recommended .NET abstraction:
- Time →
TimeProvider (built-in since .NET 8)
- File system →
System.IO.Abstractions (NuGet package)
- HTTP →
IHttpClientFactory (built-in)
- Environment → custom
IEnvironmentProvider
- Console → custom
IConsole or ILogger
- Process → custom
IProcessRunner
Step 4: Present the report
Format the output as a structured report:
## Static Dependency Report
**Scope**: <project/solution name>
**Files scanned**: <count>
**Total static call sites**: <count>
### Category Summary
| Category | Call Sites | Recommended Abstraction |
|-------------|-----------|------------------------|
| Time | 42 | TimeProvider (.NET 8+) |
| File System | 31 | System.IO.Abstractions |
| Environment | 12 | IEnvironmentProvider |
| ... | ... | ... |
### Top 10 Patterns
| # | Pattern | Count | Files |
|---|---------------------|-------|-------|
| 1 | DateTime.UtcNow | 28 | 14 |
| 2 | File.ReadAllText | 18 | 9 |
| ... |
### Most Affected Files
| File | Static Calls | Categories |
|-------------------------------|-------------|---------------------|
| Services/OrderProcessor.cs | 12 | Time, FileSystem |
| ... |
### Migration Priority
1. **Time** (42 sites) — Use `TimeProvider`, zero NuGet dependencies on .NET 8+
2. **File System** (31 sites) — Use `System.IO.Abstractions` NuGet package
3. ...
Step 5: Suggest next steps
Based on the report, recommend:
- Which category to tackle first (fewest dependencies, best built-in support)
- Whether to use
generate-testability-wrappers for custom wrapper generation
- Whether to use
migrate-static-to-wrapper for mechanical bulk migration
Validation
Common Pitfalls
| Pitfall |
Solution |
Scanning obj/ or generated code |
Always exclude obj/, bin/, and *.Designer.cs |
| Counting wrapped calls as statics |
Check if the call is behind an interface or injected service before counting |
| Missing statics inside lambdas/LINQ |
Search covers all code within .cs files, including lambdas |
Recommending TimeProvider on < .NET 8 |
Check TargetFramework in .csproj — if < net8.0, recommend NodaTime.IClock or custom ISystemClock |
| Ignoring test projects |
Only scan production code — exclude *.Tests.csproj projects from the scan |
1---2name: detect-static-dependencies3description: Scan C# source files for hard-to-test static dependencies — DateTime.Now/UtcNow, File.*, Directory.*, Environment.*, HttpClient, Console.*, Process.*, and other untestable statics. Produces a ranked report of static call sites by frequency. USE FOR: find untestable statics, scan for static dependencies, testability audit, identify hard-to-mock code, find DateTime.Now usage, detect static coupling, testability report, static analysis for testability. DO NOT USE FOR: generating wrappers (use generate-testability-wrappers), migrating code (use migrate-static-to-wrapper), general code review, or finding statics that are already behind abstractions.4license: MIT5---67# Detect Static Dependencies89Scan a C# codebase for calls to hard-to-test static APIs and produce a ranked report showing which statics appear most frequently, which files are most affected, and which abstractions already exist in the .NET ecosystem to replace them.1011## When to Use1213- Auditing a project's testability before adding unit tests14- Understanding the scope of static coupling in a legacy codebase15- Prioritizing which statics to wrap first (highest-frequency wins)16- Creating a migration plan for incremental testability improvements1718## Response Guidelines1920- Scale the response to the user's request. A question about a specific category (e.g., "find time statics") should focus on that category with file locations and counts, not produce a full report across all categories.21- When the user provides a specific file or directory path, scan only that scope — do not expand to the entire solution unless asked.22- The full structured report format in Step 4 is for comprehensive audit requests. For focused questions, return only the relevant subset (e.g., category summary + affected files for the requested category).2324## When Not to Use2526- The user wants wrappers generated (hand off to `generate-testability-wrappers`)27- The user wants mechanical migration done (hand off to `migrate-static-to-wrapper`)28- The statics are already behind interfaces or `TimeProvider`29- The code is not C# / .NET3031## Inputs3233| Input | Required | Description |34|-------|----------|-------------|35| Target path | Yes | A file, directory, project (.csproj), or solution (.sln) to scan |36| Exclusion patterns | No | Glob patterns to skip (e.g., `**/obj/**`, `**/Migrations/**`) |37| Category filter | No | Limit to specific categories: `time`, `filesystem`, `environment`, `network`, `console`, `process` |3839## Workflow4041### Step 1: Determine scan scope4243Resolve the target to a set of `.cs` files:44- If a `.cs` file, scan that single file.45- If a directory, scan all `.cs` files recursively (excluding `obj/`, `bin/`).46- If a `.csproj`, find its directory and scan `.cs` files within.47- If a `.sln`, parse it, find all project directories, and scan `.cs` files across all projects.4849Always exclude `obj/`, `bin/`, and any user-specified exclusion patterns.5051### Step 2: Search for static dependency patterns5253Scan each file for calls matching these categories:5455| Category | Patterns to search for | Recommended replacement |56|----------|----------------------|------------------------|57| **Time** | `DateTime.Now`, `DateTime.UtcNow`, `DateTime.Today`, `DateTimeOffset.Now`, `DateTimeOffset.UtcNow`, `Task.Delay(`, `new CancellationTokenSource(TimeSpan` | `TimeProvider` (.NET 8+) |58| **File System** | `File.ReadAllText(`, `File.WriteAllText(`, `File.Exists(`, `File.Delete(`, `File.Copy(`, `File.Move(`, `Directory.Exists(`, `Directory.CreateDirectory(`, `Directory.GetFiles(`, `Directory.Delete(`, `Path.Combine(`, `Path.GetTempPath(` | `IFileSystem` (System.IO.Abstractions NuGet) |59| **Environment** | `Environment.GetEnvironmentVariable(`, `Environment.SetEnvironmentVariable(`, `Environment.MachineName`, `Environment.UserName`, `Environment.CurrentDirectory`, `Environment.Exit(` | Custom `IEnvironmentProvider` |60| **Network** | `new HttpClient(`, `HttpClient.GetAsync(`, `HttpClient.PostAsync(`, `HttpClient.SendAsync(` | `IHttpClientFactory` (built-in) |61| **Console** | `Console.WriteLine(`, `Console.ReadLine(`, `Console.Write(`, `Console.ReadKey(` | `IConsole` wrapper or `ILogger` |62| **Process** | `Process.Start(`, `Process.GetCurrentProcess(`, `Process.GetProcessesByName(` | Custom `IProcessRunner` |6364### Step 3: Aggregate and rank results6566Count each static call pattern across the entire scan scope. Produce a summary with:67681. **Category summary** — total call sites per category (time, filesystem, env, etc.)692. **Top patterns** — the 10 most frequent individual patterns ranked by count703. **Most affected files** — files with the highest number of static dependencies714. **Existing abstractions available** — for each category, note the recommended .NET abstraction:72 - Time → `TimeProvider` (built-in since .NET 8)73 - File system → `System.IO.Abstractions` (NuGet package)74 - HTTP → `IHttpClientFactory` (built-in)75 - Environment → custom `IEnvironmentProvider`76 - Console → custom `IConsole` or `ILogger`77 - Process → custom `IProcessRunner`7879### Step 4: Present the report8081Format the output as a structured report:8283```84## Static Dependency Report8586**Scope**: <project/solution name>87**Files scanned**: <count>88**Total static call sites**: <count>8990### Category Summary91| Category | Call Sites | Recommended Abstraction |92|-------------|-----------|------------------------|93| Time | 42 | TimeProvider (.NET 8+) |94| File System | 31 | System.IO.Abstractions |95| Environment | 12 | IEnvironmentProvider |96| ... | ... | ... |9798### Top 10 Patterns99| # | Pattern | Count | Files |100|---|---------------------|-------|-------|101| 1 | DateTime.UtcNow | 28 | 14 |102| 2 | File.ReadAllText | 18 | 9 |103| ... |104105### Most Affected Files106| File | Static Calls | Categories |107|-------------------------------|-------------|---------------------|108| Services/OrderProcessor.cs | 12 | Time, FileSystem |109| ... |110111### Migration Priority1121. **Time** (42 sites) — Use `TimeProvider`, zero NuGet dependencies on .NET 8+1132. **File System** (31 sites) — Use `System.IO.Abstractions` NuGet package1143. ...115```116117### Step 5: Suggest next steps118119Based on the report, recommend:120- Which category to tackle first (fewest dependencies, best built-in support)121- Whether to use `generate-testability-wrappers` for custom wrapper generation122- Whether to use `migrate-static-to-wrapper` for mechanical bulk migration123124## Validation125126- [ ] All `.cs` files in scope were scanned (check count)127- [ ] Report includes category totals, top patterns, and affected files128- [ ] Each detected pattern has a recommended replacement listed129- [ ] `obj/` and `bin/` directories were excluded130- [ ] Migration priority is ordered by impact (count × ease of replacement)131132## Common Pitfalls133134| Pitfall | Solution |135|---------|----------|136| Scanning `obj/` or generated code | Always exclude `obj/`, `bin/`, and `*.Designer.cs` |137| Counting wrapped calls as statics | Check if the call is behind an interface or injected service before counting |138| Missing statics inside lambdas/LINQ | Search covers all code within `.cs` files, including lambdas |139| Recommending `TimeProvider` on < .NET 8 | Check `TargetFramework` in `.csproj` — if < net8.0, recommend `NodaTime.IClock` or custom `ISystemClock` |140| Ignoring test projects | Only scan production code — exclude `*.Tests.csproj` projects from the scan |