Detect static dependencies

From managedcode/dotnet-skills · View interactive version

Detect static dependencies is an AI agent skill from the GitHub repository managedcode/dotnet-skills. ACTIVATION PREREQUISITE: the request or discovered target must explicitly identify C#, .NET, `.cs`, or `.csproj`; otherwise stay dormant without invoking this skill. USE FOR: locating System.DateTime.Now/UtcNow, System.IO.File/Directory, System.Environment, HttpClient, Console, or Process usage in C

How to use this skill

Skill file

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

Response Guidelines

Execution Contract

available file-listing tools and scan it immediately; do not ask the user to

provide or re-upload files before both discovery and a content search fail.

files. Do not search only for the static keyword: ambient calls inside

LINQ expressions, lambdas, callbacks, and interpolated strings usually have

no static modifier.

classify the failure before retrying. Fall back to another available

mechanism such as rg -n, grep, or a shell file reader only for confirmed

tool availability, transport, or path-normalization failures and only after

verifying the canonical path remains inside the workspace. Stop on

content-exclusion, permission/policy, workspace-boundary, or unknown failures.

Search output can seed the occurrence ledger; open only the surrounding code

needed to verify receiver provenance.

completed audit in the same response. If every fallback genuinely fails,

report the verified partial findings and the exact limitation; do not invent

findings or replace the audit with a request to rerun.

When Not to Use

Inputs

| Input | Required | Description |

|-------|----------|-------------|

| Target path | No | A file, directory, project (.csproj), or solution (.sln) to scan. Defaults to the current workspace. |

| 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:

than asking the user for an absolute path.

pick one project and silently omit its siblings.

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:

Treat pattern matches as candidates, not findings. Before counting an instance call, trace how its

receiver enters the class. A collaborator supplied through a constructor, parameter, property, or

dependency injection (DI) is already a test seam. In particular, an injected HttpClient is

testable with a controlled HttpMessageHandler; do not count its calls or recommend replacing it

merely because the injected type is concrete.

| 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.GetTempPath(, and instance members that hit the disk (new FileInfo(...), new DirectoryInfo(...), .LastWriteTimeUtc, new StreamReader(path)) | IFileSystem (System.IO.Abstractions NuGet) |

| Randomness / identity | new Random(, Random.Shared, Guid.NewGuid( | TimeProvider-style seam: inject Random / an IGuidProvider |

| Culture / serialization | CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture, JsonSerializer.Serialize(, JsonSerializer.Deserialize( | Pass culture/options explicitly, or inject a serializer abstraction |

| Environment | Environment.GetEnvironmentVariable(, Environment.SetEnvironmentVariable(, Environment.MachineName, Environment.UserName, Environment.CurrentDirectory, Environment.Exit( | Custom IEnvironmentProvider |

| Network | new HttpClient(, .GetAsync(, .PostAsync(, .SendAsync( (confirm the receiver is an HttpClient; exclude calls whose receiver is injected or produced by an injected factory) | Inject HttpClient (commonly supplied by IHttpClientFactory) |

| Console | Console.WriteLine(, Console.ReadLine(, Console.Write(, Console.ReadKey( | IConsole wrapper or ILogger |

| Process | Process.Start(, Process.GetCurrentProcess(, Process.GetProcessesByName( | Custom IProcessRunner |

For time calls, inspect use as well as count. Two ambient clock reads in one

logical operation are two call sites and a consistency defect: for example,

separate DateTime.UtcNow reads for CreatedAt and

ExpiresAt = DateTime.UtcNow.AddDays(30) can drift. Recommend one captured

instant. With TimeProvider, retain DateTimeOffset where possible; when the

existing member requires UTC DateTime, use GetUtcNow().UtcDateTime, never

.DateTime, which loses the UTC kind. Treat capturing one instant as an

optional behavior-level follow-up: a mechanical wrapper migration must preserve

the original reads one-for-one unless the user separately approves that

semantic change.

Step 3: Aggregate and rank results

Count each call site across the entire scan scope — including the instance-member call sites covered by the rules below, not only static ones.

Counting rules — inaccurate totals are the main way this report loses to an ad-hoc scan:

site exactly one row containing category, exact pattern, file:line, and

recommended seam. Derive every category, pattern, and per-file count by

grouping that same ledger; never recount independently while writing tables.

eligible source file; affected files includes only files with ledger rows;

call sites is the number of ledger rows. Never substitute one for another.

bin/, generated, and user-excluded files before building the ledger. Do not

include their files or call sites in any reported count. State the exclusions

once rather than mixing excluded candidates into the arithmetic.

patterns when needed for reconciliation. If the user asked only for a top-N

subset, label it as a subset and do not imply that its rows sum to the grand

total.

Produce a summary with:

  1. Category summary — total call sites per category (time, filesystem, env, etc.)
  2. Top patterns — the 10 most frequent individual patterns ranked by count
  3. Most affected files — files with the highest number of static dependencies
  4. Existing abstractions available — for each category, note the recommended .NET abstraction:

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 (highest count, best built-in support). Keep this to a few lines.

Mention generate-testability-wrappers or migrate-static-to-wrapper only when the user's next action clearly needs them — a hand-off note, not a sales pitch. Never end an audit with promotional next-steps that dilute the findings.

Validation

Common Pitfalls

| Pitfall | Solution |

|---------|----------|

| Scanning obj/ or generated code | Always exclude obj/, bin/, and *.Designer.cs |

| Counting calls on injected collaborators | Trace the receiver: an injected HttpClient, TimeProvider, interface, or other caller-supplied dependency already has a seam and needs no replacement |

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

| Under-counting by relegating findings | Real call sites belong in the category totals, not in a trailing "also noticed" paragraph that the totals ignore |

| Calling an instance member a static | new FileInfo(p).LastWriteTimeUtc is an instance call but still a hidden file-system dependency — count it under File System and describe it accurately |

| Recommending a wrapper for Path.Combine | Pure, deterministic helpers need no seam; listing them as blockers makes the recommendations wrong |

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.