diff --git a/Cargo.lock b/Cargo.lock index 6264cab4305dba..33f39f5bba33f4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2389,7 +2389,6 @@ version = "0.1.0" dependencies = [ "anyhow", "clap", - "itertools 0.13.0", "red_knot_python_semantic", "ruff_cache", "ruff_db", diff --git a/crates/ruff/src/args.rs b/crates/ruff/src/args.rs index 8aadd064fcc05f..2fb3131b5cfb05 100644 --- a/crates/ruff/src/args.rs +++ b/crates/ruff/src/args.rs @@ -7,7 +7,7 @@ use std::sync::Arc; use anyhow::{anyhow, bail}; use clap::builder::{TypedValueParser, ValueParserFactory}; -use clap::{command, Parser}; +use clap::{command, Parser, Subcommand}; use colored::Colorize; use path_absolutize::path_dedot; use regex::Regex; @@ -132,8 +132,9 @@ pub enum Command { Format(FormatCommand), /// Run the language server. Server(ServerCommand), - /// Generate a map of Python file dependencies. - ImportMap(ImportMapCommand), + /// Analyze the import graph of the given files or directories. + #[clap(subcommand)] + Graph(GraphCommand), /// Display Ruff's version Version { #[arg(long, value_enum, default_value = "text")] @@ -141,8 +142,14 @@ pub enum Command { }, } +#[derive(Debug, Subcommand)] +pub enum GraphCommand { + /// Generate a map of Python file dependencies. + Build(GraphBuildCommand), +} + #[derive(Clone, Debug, clap::Parser)] -pub struct ImportMapCommand { +pub struct GraphBuildCommand { /// List of files or directories to include. #[clap(help = "List of files or directories to include [default: .]")] pub files: Vec, @@ -758,14 +765,14 @@ impl FormatCommand { } } -impl ImportMapCommand { +impl GraphBuildCommand { /// Partition the CLI into command-line arguments and configuration /// overrides. pub fn partition( self, global_options: GlobalConfigArgs, - ) -> anyhow::Result<(ImportMapArgs, ConfigArguments)> { - let format_arguments = ImportMapArgs { + ) -> anyhow::Result<(GraphArgs, ConfigArguments)> { + let format_arguments = GraphArgs { files: self.files, direction: self.direction, }; @@ -1199,7 +1206,7 @@ impl LineColumnParseError { } /// CLI settings that are distinct from configuration (commands, lists of files, etc.). -pub struct ImportMapArgs { +pub struct GraphArgs { pub files: Vec, pub direction: Direction, } diff --git a/crates/ruff/src/commands/import_map.rs b/crates/ruff/src/commands/graph.rs similarity index 97% rename from crates/ruff/src/commands/import_map.rs rename to crates/ruff/src/commands/graph.rs index 7b435356eeb40d..d3d792b70d5f8e 100644 --- a/crates/ruff/src/commands/import_map.rs +++ b/crates/ruff/src/commands/graph.rs @@ -1,4 +1,4 @@ -use crate::args::{ConfigArguments, ImportMapArgs}; +use crate::args::{ConfigArguments, GraphArgs}; use crate::resolve::resolve; use crate::{resolve_default_files, ExitStatus}; use anyhow::Result; @@ -13,10 +13,7 @@ use std::path::Path; use std::sync::Arc; /// Generate an import map. -pub(crate) fn import_map( - args: ImportMapArgs, - config_arguments: &ConfigArguments, -) -> Result { +pub(crate) fn graph(args: GraphArgs, config_arguments: &ConfigArguments) -> Result { // Construct the "default" settings. These are used when no `pyproject.toml` // files are present, or files are injected from outside the hierarchy. let pyproject_config = resolve(config_arguments, None)?; diff --git a/crates/ruff/src/commands/mod.rs b/crates/ruff/src/commands/mod.rs index 3dcb6a37b762e1..d7793098c86242 100644 --- a/crates/ruff/src/commands/mod.rs +++ b/crates/ruff/src/commands/mod.rs @@ -5,7 +5,7 @@ pub(crate) mod clean; pub(crate) mod config; pub(crate) mod format; pub(crate) mod format_stdin; -pub(crate) mod import_map; +pub(crate) mod graph; pub(crate) mod linter; pub(crate) mod rule; pub(crate) mod server; diff --git a/crates/ruff/src/lib.rs b/crates/ruff/src/lib.rs index aaa827b97f6f24..738c037bb643bd 100644 --- a/crates/ruff/src/lib.rs +++ b/crates/ruff/src/lib.rs @@ -20,7 +20,7 @@ use ruff_linter::settings::types::OutputFormat; use ruff_linter::{fs, warn_user, warn_user_once}; use ruff_workspace::Settings; -use crate::args::{Args, CheckCommand, Command, FormatCommand, ImportMapCommand}; +use crate::args::{Args, CheckCommand, Command, FormatCommand, GraphBuildCommand, GraphCommand}; use crate::printer::{Flags as PrinterFlags, Printer}; pub mod args; @@ -186,7 +186,7 @@ pub fn run( Command::Check(args) => check(args, global_options), Command::Format(args) => format(args, global_options), Command::Server(args) => server(args), - Command::ImportMap(args) => import_map(args, global_options), + Command::Graph(GraphCommand::Build(args)) => graph_build(args, global_options), } } @@ -200,10 +200,10 @@ fn format(args: FormatCommand, global_options: GlobalConfigArgs) -> Result Result { +fn graph_build(args: GraphBuildCommand, global_options: GlobalConfigArgs) -> Result { let (cli, config_arguments) = args.partition(global_options)?; - commands::import_map::import_map(cli, &config_arguments) + commands::graph::graph(cli, &config_arguments) } fn server(args: ServerCommand) -> Result { diff --git a/crates/ruff_db/src/system/path.rs b/crates/ruff_db/src/system/path.rs index 04165249ef56dc..df98280c1de96e 100644 --- a/crates/ruff_db/src/system/path.rs +++ b/crates/ruff_db/src/system/path.rs @@ -192,9 +192,9 @@ impl SystemPath { /// [`self.file_name`]: SystemPath::file_name /// /// The stem is: - /// * The entire file name if there is no embedded `.`; /// /// * [`None`], if there is no file name; + /// * The entire file name if there is no embedded `.`; /// * The entire file name if the file name begins with `.` and has no other `.`s within; /// * Otherwise, the portion of the file name before the final `.` /// diff --git a/crates/ruff_graph/Cargo.toml b/crates/ruff_graph/Cargo.toml index 18649a1bfcab91..a8bb3ab6b69d50 100644 --- a/crates/ruff_graph/Cargo.toml +++ b/crates/ruff_graph/Cargo.toml @@ -21,7 +21,6 @@ ruff_python_stdlib = { workspace = true } anyhow = { workspace = true } clap = { workspace = true, optional = true } -itertools = { workspace = true } salsa = { workspace = true } schemars = { workspace = true, optional = true } serde = { workspace = true, optional = true } diff --git a/crates/ruff_graph/src/db.rs b/crates/ruff_graph/src/db.rs index 31960966c6104b..7dcf9fe7897e4d 100644 --- a/crates/ruff_graph/src/db.rs +++ b/crates/ruff_graph/src/db.rs @@ -16,37 +16,32 @@ pub struct ModuleDb { impl Default for ModuleDb { fn default() -> Self { - Self::new() + Self { + storage: salsa::Storage::default(), + system: OsSystem::default(), + vendored: VendoredFileSystem::default(), + files: Files::default(), + } } } impl ModuleDb { /// Initialize a [`ModuleDb`] from the given source root. pub fn from_src_root(src_root: PathBuf) -> Result { - let db = Self::new(); + let db = Self::default(); Program::from_settings( &db, &ProgramSettings { target_version: PythonVersion::default(), search_paths: SearchPathSettings::new( - SystemPathBuf::from_path_buf(src_root).map_err(|path| { - anyhow::anyhow!(format!("Invalid path: {}", path.display())) - })?, + SystemPathBuf::from_path_buf(src_root) + .map_err(|path| anyhow::anyhow!("Invalid path: {}", path.display()))?, ), }, )?; Ok(db) } - pub fn new() -> Self { - Self { - storage: salsa::Storage::default(), - system: OsSystem::default(), - vendored: VendoredFileSystem::default(), - files: Files::default(), - } - } - #[must_use] pub fn snapshot(&self) -> Self { Self { diff --git a/docs/configuration.md b/docs/configuration.md index 055a905fe99512..7940f275d6814a 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -515,16 +515,16 @@ Ruff: An extremely fast Python linter and code formatter. Usage: ruff [OPTIONS] Commands: - check Run Ruff on the given files or directories (default) - rule Explain a rule (or all rules) - config List or describe the available configuration options - linter List all supported upstream linters - clean Clear any caches in the current directory and any subdirectories - format Run the Ruff formatter on the given files or directories - server Run the language server - import-map Generate a map of Python file dependencies - version Display Ruff's version - help Print this message or the help of the given subcommand(s) + check Run Ruff on the given files or directories (default) + rule Explain a rule (or all rules) + config List or describe the available configuration options + linter List all supported upstream linters + clean Clear any caches in the current directory and any subdirectories + format Run the Ruff formatter on the given files or directories + server Run the language server + graph Analyze the import graph of the given files or directories + version Display Ruff's version + help Print this message or the help of the given subcommand(s) Options: -h, --help Print help