From bc3bd4e18cd08c19855999247bb711644f273e14 Mon Sep 17 00:00:00 2001 From: Victorien Elvinger Date: Sat, 13 Jan 2024 12:23:55 +0100 Subject: [PATCH] fix(config): don't ignore `include` when `ignore` is set (#1548) --- CHANGELOG.md | 7 ++ crates/biome_cli/tests/commands/format.rs | 92 +++++++++++++++++++ ...at_ignored_file_in_included_directory.snap | 35 +++++++ ...format_non_included_and_ignored_files.snap | 46 ++++++++++ crates/biome_service/src/settings.rs | 10 +- crates/biome_service/src/workspace/server.rs | 40 +++----- .../content/docs/guides/how-biome-works.mdx | 19 ++-- .../src/content/docs/internals/changelog.mdx | 7 ++ 8 files changed, 217 insertions(+), 39 deletions(-) create mode 100644 crates/biome_cli/tests/snapshots/main_commands_format/does_not_format_ignored_file_in_included_directory.snap create mode 100644 crates/biome_cli/tests/snapshots/main_commands_format/does_not_format_non_included_and_ignored_files.snap diff --git a/CHANGELOG.md b/CHANGELOG.md index 060b9d77b307..502b3f8d2c23 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