Untangling static-web-server: A 25-Module Cycle and a Merged Pull Request

November 29, 2026 · By Jan Arends · 7 min read


Fixing your own code with your own tool proves little. So this time the target was someone else’s: static-web-server, a fast static file server written in Rust, about 15,000 lines, actively maintained. TangleGuard found a circular dependency spanning 25 modules. Two review rounds later the fix was merged upstream, with no change to the public API.

This write-up covers what the tool reported, what the refactoring looked like, and the parts that did not go as planned, because those turned out to be the useful ones.

TangleGuard Studio's Review Changes view of static-web-server, with the dependency graph before the change on the left and after on the right
The change in TangleGuard Studio's Review Changes view: before on the left, after on the right.

The Diagnosis

tangleguard -l rust check-circles

Five cycles. Four were small. The fifth was this (abridged):

- Tangle of 25 modules: access_logs, basic_auth, compression, cors,
  custom_headers, directory_listing, handler, health, mem_cache,
  metrics, redirects, rewrites, security_headers, settings, ...
  - symbols to move into a module below their importers:
    - `RequestHandlerOpts` from handler, imported by access_logs,
      basic_auth, compression, cors, ... — breaks 44 of 50 loops

That last line is the whole story. The request handler calls one module per pipeline step: CORS, compression, basic auth, security headers and so on. Each of those steps reads its settings from one shared options struct, and that struct was defined in the handler. So the handler imported every step, and 21 steps imported the handler back.

Rust compiles this without complaint, and nothing was broken. The cost is of a different kind: you cannot read or change the handler without it being part of everything else, and every new pipeline step joins the knot by default.

Round One: Move the Hub

The plan came straight off the report. One symbol, 44 of 50 loops:

  1. Move RequestHandlerOpts into its own module below the pipeline steps, and re-export it from the handler so the public path stays the same.
  2. Move two small configuration enums out of the directory listing module into the settings module, because the settings parser was importing them from a feature it should not know about.

Both are pure moves. About 20 of the touched files changed a single import line. The tangle went from 25 modules to 9, and the handler and the settings parser were out of it.

I also wrote down a target layering in a TangleGuard config, five layers from entry points down to shared helpers, so that the remaining upward imports would show up as rule violations with a file and line.

What the Maintainer Asked

The first review was positive, and it contained the question that mattered. Paraphrased: some cycles remain, so what is the plan for the three option fields whose types still live in the pipeline modules? That decides whether the new module is the right home for the struct.

He was right, and it was exactly what TangleGuard was still reporting: three violations, one per field. The options struct had moved, but three of its fields used types from modules that imported the struct back. I had moved the hub without making it smaller.

Round Two: Finish the Job

The three types needed three different answers, which is why a ranked list of edges with the import statements behind them is more useful than a cycle count:

  • A plain options struct for the in-memory cache. Moved down next to the main options, re-exported from its old path.
  • The compiled access log format and its parser. Self-contained, so it moved into its own small module, again re-exported.
  • The CORS policy. This one is real logic, not data, so it stayed. The three internal CORS functions now take the policy directly as a parameter and no longer need the whole options struct.

After that, nothing the options module depends on imports it back. The 25-module tangle was gone, and the public API had not changed: every type is still reachable under its old path, and the only changed signatures are crate-internal.

The project’s own checks agreed. All tests passed for both feature sets, and the benchmark bot on the pull request reported no performance change across 21 benchmarks. The pull request was approved and merged.

You can browse the structural diff of the change in TangleGuard.

What Did Not Go as Planned

The config file was not wanted. I had included the TangleGuard layer config in the pull request. The maintainer asked me to drop it: nothing in the project’s CI runs it, and at that point it described a target the code did not yet meet. Fair on both counts. A tool’s config belongs in a repository once the team has decided to use the tool, not before. The link to the structural diff did the explaining with far less friction.

The first round looked better than it was. Going from 25 modules to 9 is a satisfying number, but the coupling was the same as before; only its location had improved. It took a reviewer’s question, and the three violations still on the report, to make the change complete. A smaller cycle count is not the goal. Dependencies that point one way are.

Not every cycle is worth a pull request. Four small cycles remain in the project. Three of them are a parent module and its own children referring to each other, which is ordinary Rust and not worth a reviewer’s time. I left them alone, and I kept one further fix out of the pull request to keep it reviewable.

What This Means for You

  • Start from the symbol list, not the cycle. “Move this one type and 44 of 50 loops disappear” is a plan. “You have a 25-module cycle” is not.
  • Re-run after every round. The second round existed because the report still showed three violations after the first. Without that, the half-finished state would have looked finished.
  • Keep the public API fixed. Re-exports let you move a type to where it belongs without anyone downstream noticing. That is what made this acceptable to a maintainer who did not ask for it.
  • Show the structure, don’t ship the config. A before-and-after view of the dependency graph convinced a reviewer. A config file for a tool he does not use did not.

Want to see what’s tangled in your codebase? Get started with TangleGuard — the scan takes seconds.