Skip to content

Monorepo Toolkit

Dependency Graph ​

The graph command visualizes and analyzes dependency relationships across the monorepo workspace. It detects cycles, enforces architectural layering rules, and generates graph visualizations in multiple formats.

dependency graphTwo checks keep packages from reaching sideways or looping back

Step 1 of 6, before: Features talk to each other through *_api contract packages. home_impl depends on auth_api, never on auth_impl.

features/home/home_impl/pubspec.yaml
dependencies:  # …  auth_api:    path: ../../auth/auth_api  # …

Subcommands ​

graph visualize ​

Generates a visualization of the workspace dependency graph:

bash
# Text output to stdout (default)
dart run monorepo_toolkit graph visualize

# DOT format for Graphviz
dart run monorepo_toolkit graph visualize --format dot --output graph.dot

# Mermaid diagram for Markdown
dart run monorepo_toolkit graph visualize --format mermaid --output graph.md

# JSON for programmatic use
dart run monorepo_toolkit graph visualize --format json --output graph.json
FormatUse case
textQuick terminal overview (default)
dotGraphviz rendering (dot -Tpng graph.dot -o graph.png)
mermaidEmbed in GitHub Markdown or docs
jsonProgrammatic analysis or custom tooling

graph stats ​

Displays dependency statistics for the workspace:

bash
dart run monorepo_toolkit graph stats

Reports total package count, total edges, most-depended-on packages, and packages with the most dependencies.

graph cycles ​

Detects circular dependencies in the workspace:

bash
dart run monorepo_toolkit graph cycles

Returns exit code 0 if no cycles are found, or exit code 1 with a list of detected cycles. Pass --no-fail to report cycles without failing (the --fail flag defaults to true).

graph validate ​

Validates dependency rules (architectural layering constraints):

bash
dart run monorepo_toolkit graph validate

# Show detailed output including passing checks
dart run monorepo_toolkit graph validate --verbose   # or -v

Returns exit code 0 if all rules pass, or exit code 1 with a list of violations.

The check reads two sources for each package: the pubspec.yaml dependencies and the package: imports in lib/. With resolution: workspace, a package can import another workspace package without declaring it, and it still compiles. A violation found only through an import ends with [detected via import statement only ...].

Architectural rules enforced ​

The validate subcommand enforces these layering constraints:

RuleDescription
api-restricted-deps*_api packages depend ONLY on shared/, utilities/, and other *_api
no-impl-cross-dep*_impl packages cannot depend on another feature's *_impl or feature packages; use its *_api. A feature that is a single package (one package in its features/<name>/ directory) may be depended on
shared-no-upward-depshared/ cannot depend on features/, infrastructure/, packages/, or apps/
utility-no-upward-deputilities/ cannot depend on features/, infrastructure/, packages/, or apps/
no-upward-deppackages/ cannot depend on feature impl or app packages
no-devtools-depApp packages cannot depend on devtools packages

Dependencies must flow downward:

apps/ -> features/ -> infrastructure/ -> packages/ -> shared/ -> utilities/

Package categories ​

The validator automatically categorizes packages based on their path:

CategoryPath patternExample
appapps/template_app
sharedshared/feature_sdk, app_config
utilityutilities/app_utilities, app_lints
api*_api in infrastructure/ or features/auth_api, monitoring_api
impl*_impl in infrastructure/ or features/auth_impl, monitoring_impl
featurefeatures/ without _api/_impl suffixintroduction
infrastructureinfrastructure/ without suffixstandalone infra packages
packagepackages/app_asset, app_ui_kit
devtoolsdevtools/monorepo_toolkit

CI integration ​

Add graph validation to your CI pipeline to catch architectural violations early:

yaml
- name: Validate dependency graph
  run: dart run monorepo_toolkit graph validate

- name: Check for cycles
  run: dart run monorepo_toolkit graph cycles

Both commands return non-zero exit codes on failure, which will fail the CI step.

Melos shortcuts ​

bash
melos run graph:visualize   # Visualize dependency graph
melos run graph:stats       # Show dependency statistics
melos run graph:cycles      # Detect circular dependencies
melos run graph:validate    # Validate architectural rules