dep_graph_rs generates a dependency graph for the internal modules of a Rust crate.
It uses the syn crate to parse Rust source code and
analyzes use crate::... statements to build a directed graph of dependencies between modules or files.
The graph is then output in the DOT language.
Note: This tool visualizes dependencies within a single crate. It does not show dependencies on external crates from Cargo.toml.
One of the main motivations for this tool is to assist refactoring and understand dependencies.
My use case is to analyze the dependencies of a module before extracting it into a separate crate.
In this case, extracting tantivy/src/directory into a new tantivy-directory crate.
In this case we want to see on which other modules the directory module depends:
dep_graph_rs tantivy_folder --source "directory.*" > tantivy_directory_deps.dot- Generates dependency graphs from Rust source code.
- Outputs in DOT format for use with Graphviz.
- Group dependencies by file or by module.
- Filter the graph by source, destination, or item (e.g., function name).
- Use an advanced query language with AND/OR/NOT to filter edges.
- Clusters nodes by their root module for better readability.
You can install the tool using cargo:
cargo install dep_graph_rsTo generate a dependency graph, run the tool with the path to your crate's root (src/lib.rs or src/main.rs):
dep_graph_rs <path-to-your-project|path-to-entry-file> > graph.dotThis will output the graph in DOT format to graph.dot.
You can render the generated graph.dot file in a few ways:
- Online Viewer: Paste the content of
graph.dotinto an online viewer like GraphvizOnline. - Local Installation: If you have Graphviz installed locally, you can use the
dotcommand to render the graph to an image:dot -Tpng graph.dot -o graph.png
--mode <file|module>: Group the graph by file or by module (default:module).--source <regex>: Filter by source module/file.--destination <regex>: Filter by destination module/file.--item <regex>: Filter by the name of the imported item (e.g., a function or struct).--query, -q <expr>: Advanced query. Example:source:*agg* AND destination:*segment*. SupportsAND,OR,NOT, parentheses, and fieldssource|destination|item|any.*is a wildcard; quotes allow spaces. The arrow shorthanda->bexpands tosource:a AND destination:b.
For example, to only show dependencies originating from the graphics module:
dep_graph_rs ./test_proj1 --source "graphics" > graph.dot- Better support
*imports - Plain Output for cli usage
- Go more into
rust_analyzerterritory, e.g. find_references etc.?
