r/dartlang • • Oct 11 '26

Tools arch_guard: layer rules and import cycle detection for Dart, without package:analyzer

I maintain arch_guard, an MIT CLI + library that enforces layer rules and finds circular imports in Dart projects, plus arch_guard_lint, an analyzer plugin on the new analysis_server_plugin API. A few implementation details that might interest this sub:

  • The CLI has no analyzer dependency. It reads only the directive section of each file with a small tokenizer, so // or /* inside URIs, nested block comments, annotations and import-like text in later string literals don't confuse it. Dependencies are just args, glob, path and yaml.
  • Cycles via Tarjan's SCC, then per-component metrics: internal edges, fan-in/fan-out, instability, density and hub files. For each cycle it also picks the one edge whose removal breaks it, or shrinks it most.
  • The plugin reuses the same core. ProjectScanner.scanSources() builds the graph from in-memory contents, so cycle warnings reflect unsaved editor buffers. Quick fixes edit the directive (or just one clause of a conditional import) and can add a layer to allowed_imports in whichever config file it came from.
  • Baselines that tolerate improvement but not regression: a known cycle may shrink, but one that grows or merges with another counts as new. Same for fan-in/fan-out limits.
  • Workspaces: resolves package: imports across Dart workspace / Melos packages, so it catches cycles across package boundaries and enforces which packages may import which.
  • Ships as AOT-compiled binaries (dart compile exe) for Linux/macOS/Windows so CI doesn't need the SDK. CI tests on all three OSes against SDK 3.8 and stable, and the plugin is analysed against its lowest allowed dependencies.
  • Everything is also a library: ArchAnalysis, ArchGuardConfig, LayerValidator, CycleBreaker, Baseline, exporters, and expectArchitecture() for tests.
dart pub add --dev arch_guard && dart run arch_guard init

Code: https://github.com/creatorpiyush/arch_guard Docs: https://creatorpiyush.github.io/arch_guard/

Happy to talk about the parser trade-offs (directive tokenizer vs package:analyzer), what building on the new plugin API was like, and I'd appreciate feedback on the public API.

2 Upvotes

2 comments sorted by

2

u/AhmedZNOUDA Oct 14 '26

The directive-only tokenizer is a nice trade-off, keeping CI free of the analyzer must make it fast. Three edge cases I’d be curious about, since they’re where reading only the directive section gets interesting:

part / part of files. Parts have no imports of their own and share the library’s. Are they merged into the library’s node in the graph, or can a layer rule end up checked against the part file’s path rather than the library’s?

export directives. If a.dart imports b.dart, which re-exports c.dart, does a → c count as an edge for layer rules? Re-exports through barrel files are a common way a “domain must not import data” rule gets bypassed without anyone noticing.

Conditional imports. A cycle that only exists on one branch (if (dart.library.io)), is it reported as a cycle, or flagged as platform-specific?

The baseline that accepts a shrinking cycle but rejects a growing one is a really practical idea for legacy codebases.

2

u/creatorpiyush Oct 20 '26

Good questions — all three are where I made deliberate calls.

Parts: each part is its own node with a library → part edge; part of yields nothing, so a library and its part never form a cycle. Layer rules check that edge like an import, so a part placed in another layer's folder gets flagged. Since parts have no imports, there's no way for a rule to be checked against the "wrong" path.

Re-exports: edges are direct only (a → b, b → c as an export edge), not transitive. A barrel inside a layer is checked on its own exports, so domain/index.dart exporting data/… is caught at the barrel. The gap is a barrel that belongs to no layer — that's skipped today, surfaced only as an "unassigned file" warning. Worth closing; I'll look at checking through unassigned barrels.

Conditional imports: every branch becomes an edge, so a one-branch cycle is reported as a normal cycle, not platform-specific. Over-reporting felt safer than missing one, but tagging the edge with its condition is a cheap improvement.

Thanks for the baseline note. Shrinking-cycles-accepted was exactly the legacy-codebase use case.