From 8407041f3a80b35f16230581ad2c5e6968f56ab4 Mon Sep 17 00:00:00 2001 From: Amethyst Reese Date: Wed, 18 Feb 2026 11:06:30 -0800 Subject: [PATCH 1/6] Include configured extensions in file discovery --- crates/ruff_linter/src/settings/types.rs | 11 ++++++++++- crates/ruff_workspace/src/configuration.rs | 17 ++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/crates/ruff_linter/src/settings/types.rs b/crates/ruff_linter/src/settings/types.rs index feb23afc6c64d..6ad43013fc13e 100644 --- a/crates/ruff_linter/src/settings/types.rs +++ b/crates/ruff_linter/src/settings/types.rs @@ -200,6 +200,7 @@ impl Deref for GlobPath { #[derive(Debug, Clone, CacheKey, PartialEq, PartialOrd, Eq, Ord)] pub enum FilePattern { Builtin(&'static str), + Config(String), User(String, GlobPath), } @@ -209,6 +210,9 @@ impl FilePattern { FilePattern::Builtin(pattern) => { builder.add(Glob::from_str(pattern)?); } + FilePattern::Config(pattern) => { + builder.add(Glob::new(&pattern)?); + } FilePattern::User(pattern, absolute) => { // Add the absolute path. builder.add(Glob::new(&absolute.to_string_lossy())?); @@ -230,7 +234,7 @@ impl Display for FilePattern { "{:?}", match self { Self::Builtin(pattern) => pattern, - Self::User(pattern, _) => pattern.as_str(), + Self::User(pattern, _) | Self::Config(pattern) => pattern.as_str(), } ) } @@ -498,6 +502,11 @@ impl From for (String, Language) { pub struct ExtensionMapping(FxHashMap); impl ExtensionMapping { + /// Return the file extensions in the mapping. + pub fn extensions(&self) -> Vec<&String> { + self.0.keys().collect() + } + /// Return the [`Language`] for the given file. pub fn get(&self, path: &Path) -> Option { let ext = path.extension()?.to_str()?; diff --git a/crates/ruff_workspace/src/configuration.rs b/crates/ruff_workspace/src/configuration.rs index dbf31290d17c7..47d4713a10e51 100644 --- a/crates/ruff_workspace/src/configuration.rs +++ b/crates/ruff_workspace/src/configuration.rs @@ -279,9 +279,20 @@ impl Configuration { PreviewMode::Disabled => FilePatternSet::try_from_iter( self.include.unwrap_or_else(|| INCLUDE.to_vec()), )?, - PreviewMode::Enabled => FilePatternSet::try_from_iter( - self.include.unwrap_or_else(|| INCLUDE_PREVIEW.to_vec()), - )?, + PreviewMode::Enabled => { + FilePatternSet::try_from_iter(self.include.unwrap_or_else(|| { + let mut patterns = INCLUDE_PREVIEW.to_vec(); + if let Some(extension_map) = &self.extension { + patterns.extend( + extension_map + .extensions() + .iter() + .map(|ext| FilePattern::Config(format!("*.{ext}"))), + ); + } + patterns + }))? + } }, respect_gitignore: self.respect_gitignore.unwrap_or(true), project_root: project_root.to_path_buf(), From 02cfa931b84bc72d925c167add402a571a4029a0 Mon Sep 17 00:00:00 2001 From: Amethyst Reese Date: Thu, 19 Feb 2026 11:02:30 -0800 Subject: [PATCH 2/6] return iterator directly --- crates/ruff_linter/src/settings/types.rs | 4 ++-- crates/ruff_workspace/src/configuration.rs | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/crates/ruff_linter/src/settings/types.rs b/crates/ruff_linter/src/settings/types.rs index 6ad43013fc13e..c80d0d24415d9 100644 --- a/crates/ruff_linter/src/settings/types.rs +++ b/crates/ruff_linter/src/settings/types.rs @@ -503,8 +503,8 @@ pub struct ExtensionMapping(FxHashMap); impl ExtensionMapping { /// Return the file extensions in the mapping. - pub fn extensions(&self) -> Vec<&String> { - self.0.keys().collect() + pub fn extensions(&self) -> impl Iterator { + self.0.keys() } /// Return the [`Language`] for the given file. diff --git a/crates/ruff_workspace/src/configuration.rs b/crates/ruff_workspace/src/configuration.rs index 47d4713a10e51..c2c6857d70a30 100644 --- a/crates/ruff_workspace/src/configuration.rs +++ b/crates/ruff_workspace/src/configuration.rs @@ -286,7 +286,6 @@ impl Configuration { patterns.extend( extension_map .extensions() - .iter() .map(|ext| FilePattern::Config(format!("*.{ext}"))), ); } From 286c1e46dee429bad12fa35bff059446a9ffd2c2 Mon Sep 17 00:00:00 2001 From: Amethyst Reese Date: Thu, 19 Feb 2026 15:39:41 -0800 Subject: [PATCH 3/6] test case --- crates/ruff/tests/integration_test.rs | 64 +++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/crates/ruff/tests/integration_test.rs b/crates/ruff/tests/integration_test.rs index 1f3320811e6bc..e67096bc61142 100644 --- a/crates/ruff/tests/integration_test.rs +++ b/crates/ruff/tests/integration_test.rs @@ -26,6 +26,10 @@ use ruff::run; const BIN_NAME: &str = "ruff"; +fn tempdir_filter(path: &Path) -> String { + format!(r#"{}\\?/?"#, regex::escape(path.to_str().unwrap())) +} + fn ruff_cmd() -> Command { Command::new(get_cargo_bin(BIN_NAME)) } @@ -201,6 +205,66 @@ import bar # unused import Ok(()) } +#[test] +fn check_mapped_extension_files() -> Result<()> { + let tempdir = TempDir::new()?; + fs::write( + tempdir.path().join("pyproject.toml"), + r#" +[tool.ruff] +extension = {foo="python", bar="markdown"} +"#, + )?; + fs::write( + tempdir.path().join("test.foo"), + r" +print( 'hello' ) +", + )?; + fs::write( + tempdir.path().join("test.bar"), + r" +Text string + +```py +print( 'hello' ) +``` +", + )?; + + insta::with_settings!({filters => vec![ + (&*tempdir_filter(tempdir.path()), "TEMPDIR/"), + ]}, { + assert_cmd_snapshot!( + ruff_cmd() + .args(["format", "--no-cache", "--preview", "--check", &tempdir.path().to_string_lossy()]), + @r#" + success: false + exit_code: 1 + ----- stdout ----- + unformatted: File would be reformatted + --> TEMPDIR/test.bar:1:1 + 2 | Text string + 3 | + 4 | ```py + - print( 'hello' ) + 5 + print("hello") + 6 | ``` + + unformatted: File would be reformatted + --> TEMPDIR/test.foo:1:1 + - + - print( 'hello' ) + 1 + print("hello") + + 2 files would be reformatted + + ----- stderr ----- + "#); + }); + Ok(()) +} + #[test] fn check_warn_stdin_filename_with_files() { let mut cmd = RuffCheck::default() From 292310c8113d9344b85c1d1d734313821ca05d35 Mon Sep 17 00:00:00 2001 From: Amethyst Reese Date: Mon, 23 Feb 2026 12:41:05 -0800 Subject: [PATCH 4/6] move test to cli/format.rs --- crates/ruff/tests/cli/format.rs | 60 +++++++++++++++++++++++++ crates/ruff/tests/integration_test.rs | 64 --------------------------- 2 files changed, 60 insertions(+), 64 deletions(-) diff --git a/crates/ruff/tests/cli/format.rs b/crates/ruff/tests/cli/format.rs index 6e7c8a8117625..5e7dcbe6b2bf3 100644 --- a/crates/ruff/tests/cli/format.rs +++ b/crates/ruff/tests/cli/format.rs @@ -2512,3 +2512,63 @@ fn markdown_formatting_stdin() -> Result<()> { "#); Ok(()) } + +#[test] +fn format_mapped_extension_files() -> Result<()> { + let test = CliTest::with_files([ + ( + "pyproject.toml", + r#" +[tool.ruff] +extension = {foo="python", bar="markdown"} +"#, + ), + ( + "test.foo", + r" +print( 'hello' ) +", + ), + ( + "test.bar", + r" +Text string + +```py +print( 'hello' ) +``` +", + ), + ])?; + + assert_cmd_snapshot!( + test.format_command() + .args(["format", "--preview", "--check", "."]), + @r#" + success: false + exit_code: 2 + ----- stdout ----- + io: [TMP]/format: No such file or directory (os error 2) + --> format:1:1 + + unformatted: File would be reformatted + --> test.bar:1:1 + 2 | Text string + 3 | + 4 | ```py + - print( 'hello' ) + 5 + print("hello") + 6 | ``` + + unformatted: File would be reformatted + --> test.foo:1:1 + - + - print( 'hello' ) + 1 + print("hello") + + 2 files would be reformatted + + ----- stderr ----- + "#); + Ok(()) +} diff --git a/crates/ruff/tests/integration_test.rs b/crates/ruff/tests/integration_test.rs index e67096bc61142..1f3320811e6bc 100644 --- a/crates/ruff/tests/integration_test.rs +++ b/crates/ruff/tests/integration_test.rs @@ -26,10 +26,6 @@ use ruff::run; const BIN_NAME: &str = "ruff"; -fn tempdir_filter(path: &Path) -> String { - format!(r#"{}\\?/?"#, regex::escape(path.to_str().unwrap())) -} - fn ruff_cmd() -> Command { Command::new(get_cargo_bin(BIN_NAME)) } @@ -205,66 +201,6 @@ import bar # unused import Ok(()) } -#[test] -fn check_mapped_extension_files() -> Result<()> { - let tempdir = TempDir::new()?; - fs::write( - tempdir.path().join("pyproject.toml"), - r#" -[tool.ruff] -extension = {foo="python", bar="markdown"} -"#, - )?; - fs::write( - tempdir.path().join("test.foo"), - r" -print( 'hello' ) -", - )?; - fs::write( - tempdir.path().join("test.bar"), - r" -Text string - -```py -print( 'hello' ) -``` -", - )?; - - insta::with_settings!({filters => vec![ - (&*tempdir_filter(tempdir.path()), "TEMPDIR/"), - ]}, { - assert_cmd_snapshot!( - ruff_cmd() - .args(["format", "--no-cache", "--preview", "--check", &tempdir.path().to_string_lossy()]), - @r#" - success: false - exit_code: 1 - ----- stdout ----- - unformatted: File would be reformatted - --> TEMPDIR/test.bar:1:1 - 2 | Text string - 3 | - 4 | ```py - - print( 'hello' ) - 5 + print("hello") - 6 | ``` - - unformatted: File would be reformatted - --> TEMPDIR/test.foo:1:1 - - - - print( 'hello' ) - 1 + print("hello") - - 2 files would be reformatted - - ----- stderr ----- - "#); - }); - Ok(()) -} - #[test] fn check_warn_stdin_filename_with_files() { let mut cmd = RuffCheck::default() From ec93b4a8fa987b51f24bd6f055972d691f73d464 Mon Sep 17 00:00:00 2001 From: Amethyst Reese Date: Mon, 23 Feb 2026 12:51:53 -0800 Subject: [PATCH 5/6] Document this behavior --- crates/ruff_workspace/src/options.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/ruff_workspace/src/options.rs b/crates/ruff_workspace/src/options.rs index 78e35073a1d04..f7b77e286e861 100644 --- a/crates/ruff_workspace/src/options.rs +++ b/crates/ruff_workspace/src/options.rs @@ -286,6 +286,10 @@ pub struct Options { /// by the `--extension` command-line flag). /// /// Supported file types include `python`, `pyi`, `ipynb`, and `markdown`. + /// + /// Any file extensions listed here will be automatically added to the + /// default `include` list as a `*.{ext}` glob, so that they are linted + /// and formatted without needing any additional configuration settings. #[option( default = "{}", value_type = "dict[str, Language]", From 68aa3b98d1d28b8744f079c209e25bd3a92c638c Mon Sep 17 00:00:00 2001 From: Amethyst Reese Date: Mon, 23 Feb 2026 13:08:12 -0800 Subject: [PATCH 6/6] schema --- ruff.schema.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ruff.schema.json b/ruff.schema.json index 0f1e7cf4d60f0..ebcd8c1f39c97 100644 --- a/ruff.schema.json +++ b/ruff.schema.json @@ -178,7 +178,7 @@ } }, "extension": { - "description": "A mapping of custom file extensions to known file types (overridden\nby the `--extension` command-line flag).\n\nSupported file types include `python`, `pyi`, `ipynb`, and `markdown`.", + "description": "A mapping of custom file extensions to known file types (overridden\nby the `--extension` command-line flag).\n\nSupported file types include `python`, `pyi`, `ipynb`, and `markdown`.\n\nAny file extensions listed here will be automatically added to the\ndefault `include` list as a `*.{ext}` glob, so that they are linted\nand formatted without needing any additional configuration settings.", "type": [ "object", "null"