Skip to main content

Module rust_modules

Module rust_modules 

Source
Expand description

Structural Rust module resolution for the project graph (gh-2269).

Rust import edges cannot be derived from a use string alone: the same module is reached through crate::, super::, self::, a workspace crate name, a pub use re-export chain or a glob, and a module’s file depends on foo.rs vs foo/mod.rs and #[path]. This resolver builds the module tree of every Cargo target in the project (no compiler, no build scripts) and resolves each use path and each fully qualified path in code to the file that defines the named item:

  • crate roots come from each Cargo.toml ([lib], bins, tests, examples, benches, build script), workspace crates are named by their library name with - mapped to _, renamed dependencies (package = "…") included;
  • mod x; follows x.rs, x/mod.rs and #[path = "…"]; a #[cfg_attr(…, path = "…")] target is an additional file of the same module (the configuration is not evaluated, so every existing candidate is loaded and the module is their union); inline mod x { … } blocks are modules of their own (so super:: inside an inline mod tests is the file’s module);
  • pub use re-exports and globs are followed up to four hops;
  • strings and comments are skipped, so a module named only in a comment or a string is not a dependency.

Facade modules — crate roots and mod.rs files that declare child files — never receive an import edge: almost every file reaches its imports through them, and an edge there would make every impact the whole workspace. Their mod declarations are still recorded as module edges.

Structs§

RustModuleGraph
Edges and coverage produced by resolve_workspace.

Constants§

IMPORT_EDGE_KIND
Graph edge kind of a resolved Rust use ((from, to) in RustModuleGraph::imports).
IMPORT_EDGE_WEIGHT
Weight of an IMPORT_EDGE_KIND edge in the project graph.
MODULE_EDGE_KIND
Graph edge kind of a mod child; declaration ((parent, child) in RustModuleGraph::modules).
MODULE_EDGE_WEIGHT
Weight of a MODULE_EDGE_KIND edge in the project graph.
UNKNOWN_DEPENDENTS_ADVICE
The advice that follows an unknown-dependents answer.

Functions§

resolve_workspace
Resolve the Rust module graph of every Cargo target below root.
unresolved_dependents_reason
Why the dependents of a Rust file cannot be listed from the resolved graph, or None when they can. declared: some file declares it with mod (it has an incoming MODULE_EDGE_KIND edge); declares_children: it declares child files itself. Every impact answer for a .rs file asks this before it may say that nothing depends on the file.