Skip to content

Commit 4e935f7

Browse files
Add a subcommand to generate dependency graphs (#13402)
## Summary This PR adds an experimental Ruff subcommand to generate dependency graphs based on module resolution. A few highlights: - You can generate either dependency or dependent graphs via the `--direction` command-line argument. - Like Pants, we also provide an option to identify imports from string literals (`--detect-string-imports`). - Users can also provide additional dependency data via the `include-dependencies` key under `[tool.ruff.import-map]`. This map uses file paths as keys, and lists of strings as values. Those strings can be file paths or globs. The dependency resolution uses the red-knot module resolver which is intended to be fully spec compliant, so it's also a chance to expose the module resolver in a real-world setting. The CLI is, e.g., `ruff graph build ../autobot`, which will output a JSON map from file to files it depends on for the `autobot` project.
1 parent 260c2ec commit 4e935f7

30 files changed

+1339
-45
lines changed

Cargo.lock

+98
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ ruff_cache = { path = "crates/ruff_cache" }
1717
ruff_db = { path = "crates/ruff_db" }
1818
ruff_diagnostics = { path = "crates/ruff_diagnostics" }
1919
ruff_formatter = { path = "crates/ruff_formatter" }
20+
ruff_graph = { path = "crates/ruff_graph" }
2021
ruff_index = { path = "crates/ruff_index" }
2122
ruff_linter = { path = "crates/ruff_linter" }
2223
ruff_macros = { path = "crates/ruff_macros" }
@@ -42,6 +43,7 @@ red_knot_workspace = { path = "crates/red_knot_workspace" }
4243
aho-corasick = { version = "1.1.3" }
4344
annotate-snippets = { version = "0.9.2", features = ["color"] }
4445
anyhow = { version = "1.0.80" }
46+
assert_fs = { version = "1.1.0" }
4547
argfile = { version = "0.2.0" }
4648
bincode = { version = "1.3.3" }
4749
bitflags = { version = "2.5.0" }
@@ -68,6 +70,7 @@ fern = { version = "0.6.1" }
6870
filetime = { version = "0.2.23" }
6971
glob = { version = "0.3.1" }
7072
globset = { version = "0.4.14" }
73+
globwalk = { version = "0.9.1" }
7174
hashbrown = "0.14.3"
7275
ignore = { version = "0.4.22" }
7376
imara-diff = { version = "0.1.5" }

crates/red_knot_python_semantic/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use rustc_hash::FxHasher;
44

55
pub use db::Db;
66
pub use module_name::ModuleName;
7-
pub use module_resolver::{resolve_module, system_module_search_paths, vendored_typeshed_stubs};
7+
pub use module_resolver::{
8+
resolve_module, system_module_search_paths, vendored_typeshed_stubs, Module,
9+
};
810
pub use program::{Program, ProgramSettings, SearchPathSettings, SitePackages};
911
pub use python_version::PythonVersion;
1012
pub use semantic_model::{HasTy, SemanticModel};

crates/red_knot_python_semantic/src/module_resolver/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::iter::FusedIterator;
22

3-
pub(crate) use module::Module;
3+
pub use module::Module;
44
pub use resolver::resolve_module;
55
pub(crate) use resolver::{file_to_module, SearchPaths};
66
use ruff_db::system::SystemPath;

crates/ruff/Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ default-run = "ruff"
1414

1515
[dependencies]
1616
ruff_cache = { workspace = true }
17+
ruff_db = { workspace = true }
1718
ruff_diagnostics = { workspace = true }
19+
ruff_graph = { workspace = true, features = ["serde", "clap"] }
1820
ruff_linter = { workspace = true, features = ["clap"] }
1921
ruff_macros = { workspace = true }
2022
ruff_notebook = { workspace = true }
@@ -36,6 +38,7 @@ clap_complete_command = { workspace = true }
3638
clearscreen = { workspace = true }
3739
colored = { workspace = true }
3840
filetime = { workspace = true }
41+
globwalk = { workspace = true }
3942
ignore = { workspace = true }
4043
is-macro = { workspace = true }
4144
itertools = { workspace = true }
@@ -59,8 +62,11 @@ wild = { workspace = true }
5962
[dev-dependencies]
6063
# Enable test rules during development
6164
ruff_linter = { workspace = true, features = ["clap", "test-rules"] }
65+
66+
assert_fs = { workspace = true }
6267
# Avoid writing colored snapshots when running tests from the terminal
6368
colored = { workspace = true, features = ["no-color"] }
69+
indoc = { workspace = true }
6470
insta = { workspace = true, features = ["filters", "json"] }
6571
insta-cmd = { workspace = true }
6672
tempfile = { workspace = true }

0 commit comments

Comments
 (0)