diff --git a/CHANGELOG.md b/CHANGELOG.md index dd6f5460d814..92ab9f7551c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,13 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom - Fix [1440](https://github.com/biomejs/biome/issues/1440), a case where `extends` and `overrides` weren't correctly emitting the final configuration. Contributed by @arendjr +- Correctly handle `include` when `ignore` is set (#1468). Contributed by @Conaclos + + Previously, Biome ignored `include` if `ignore` was set. + Now, Biome check both `include` and `ignore`. + A file is processed if it is included and not ignored. + If `include` is not set all files are considered included. + ### Editors ### Formatter diff --git a/crates/biome_cli/tests/commands/format.rs b/crates/biome_cli/tests/commands/format.rs index a7b6d8b77c5c..064b1db208ed 100644 --- a/crates/biome_cli/tests/commands/format.rs +++ b/crates/biome_cli/tests/commands/format.rs @@ -1340,6 +1340,98 @@ fn does_not_format_ignored_directories() { )); } +#[test] +fn does_not_format_non_included_and_ignored_files() { + let config = r#"{ + "files": { + "include": ["file1.js", "file2.js", "file3.js"], + "ignore": ["file2.js"] + }, + "formatter": { + "include": ["file2.js"], + "ignore": ["file3.js"] + } + }"#; + let files = [("file1.js", true), ("file2.js", true), ("file3.js", false)]; + + let mut console = BufferConsole::default(); + let mut fs = MemoryFileSystem::default(); + let file_path = Path::new("biome.json"); + fs.insert(file_path.into(), config); + for (file_path, _) in files { + let file_path = Path::new(file_path); + fs.insert(file_path.into(), UNFORMATTED.as_bytes()); + } + + let result = run_cli( + DynRef::Borrowed(&mut fs), + &mut console, + Args::from([("format"), ("."), ("--write")].as_slice()), + ); + assert!(result.is_ok(), "run_cli returned {result:?}"); + + for (file_path, expect_formatted) in files { + let expected = if expect_formatted { + FORMATTED + } else { + UNFORMATTED + }; + assert_file_contents(&fs, Path::new(file_path), expected); + } + + assert_cli_snapshot(SnapshotPayload::new( + module_path!(), + "does_not_format_non_included_and_ignored_files", + fs, + console, + result, + )); +} + +#[test] +fn does_not_format_ignored_file_in_included_directory() { + let config = r#"{ + "formatter": { + "include": ["src"], + "ignore": ["src/file2.js"] + } + }"#; + let files = [("src/file1.js", true), ("src/file2.js", false)]; + + let mut console = BufferConsole::default(); + let mut fs = MemoryFileSystem::default(); + let file_path = Path::new("biome.json"); + fs.insert(file_path.into(), config); + for (file_path, _) in files { + let file_path = Path::new(file_path); + fs.insert(file_path.into(), UNFORMATTED.as_bytes()); + } + + let result = run_cli( + DynRef::Borrowed(&mut fs), + &mut console, + Args::from([("format"), ("."), ("--write")].as_slice()), + ); + assert!(result.is_ok(), "run_cli returned {result:?}"); + + for (file_path, expect_formatted) in files { + let expected = if expect_formatted { + FORMATTED + } else { + UNFORMATTED + }; + assert_file_contents(&fs, Path::new(file_path), expected); + } + + assert_cli_snapshot(SnapshotPayload::new( + module_path!(), + "does_not_format_ignored_file_in_included_directory", + fs, + console, + result, + )); +} + #[test] fn fs_error_read_only() { let mut fs = MemoryFileSystem::new_read_only(); diff --git a/crates/biome_cli/tests/snapshots/main_commands_format/does_not_format_ignored_file_in_included_directory.snap b/crates/biome_cli/tests/snapshots/main_commands_format/does_not_format_ignored_file_in_included_directory.snap new file mode 100644 index 000000000000..224b027e8bec --- /dev/null +++ b/crates/biome_cli/tests/snapshots/main_commands_format/does_not_format_ignored_file_in_included_directory.snap @@ -0,0 +1,35 @@ +--- +source: crates/biome_cli/tests/snap_test.rs +expression: content +--- +## `biome.json` + +```json +{ + "formatter": { + "include": ["src"], + "ignore": ["src/file2.js"] + } +} +``` + +## `src/file1.js` + +```js +statement(); + +``` + +## `src/file2.js` + +```js + statement( ) +``` + +# Emitted Messages + +```block +Formatted 2 file(s) in