Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/smart-zebras-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@biomejs/biome": patch
---

Fixed [#5979](https://github.com/biomejs/biome/issues/5979): `biome search` now correctly skips files that don't match the pattern's target language.
32 changes: 32 additions & 0 deletions crates/biome_cli/src/execute/process_file/search.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use crate::execute::TraversalMode;
use crate::execute::diagnostics::{ResultExt, SearchDiagnostic};
use crate::execute::process_file::workspace_file::WorkspaceFile;
use crate::execute::process_file::{FileResult, FileStatus, Message, SharedTraversalOptions};
use biome_diagnostics::{DiagnosticExt, category};
use biome_fs::{BiomePath, TraversalContext};
use biome_grit_patterns::{GritTargetLanguage, JsTargetLanguage};
use biome_service::diagnostics::FileTooLarge;
use biome_service::file_handlers::DocumentFileSource;
use biome_service::workspace::PatternId;

pub(crate) fn search<'ctx>(
Expand Down Expand Up @@ -31,6 +34,25 @@ pub(crate) fn search_with_guard<'ctx>(
pattern: &PatternId,
) -> FileResult {
let _ = tracing::info_span!("Search ", path =? workspace_file.path).entered();

let file_source = DocumentFileSource::from_path(workspace_file.path.as_path());
let pattern_language = match &_ctx.execution.traversal_mode {
TraversalMode::Search {
language: Some(pattern_language),
..
} => pattern_language,
TraversalMode::Search { language: None, .. } => {
// Default to JavaScript when no language is specified
&GritTargetLanguage::JsTargetLanguage(JsTargetLanguage)
}
_ => return Ok(FileStatus::Ignored), // unreachable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_ => return Ok(FileStatus::Ignored), // unreachable
_ => unreachable!(),

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to keep it as is, as otherwise we'd panic again if it turns out we made a mistake (either now or after a refactor).

};

// Ignore files that don't match the pattern's target language
if !is_file_compatible_with_pattern(&file_source, pattern_language) {
return Ok(FileStatus::Ignored);
}

let result = workspace_file
.guard()
.search_pattern(pattern)
Expand All @@ -53,3 +75,13 @@ pub(crate) fn search_with_guard<'ctx>(

Ok(FileStatus::SearchResult(matches_len, search_results))
}

fn is_file_compatible_with_pattern(
file_source: &DocumentFileSource,
pattern_language: &GritTargetLanguage,
) -> bool {
match pattern_language {
GritTargetLanguage::JsTargetLanguage(_) => matches!(file_source, DocumentFileSource::Js(_)),
GritTargetLanguage::CssTargetLanguage(_) => matches!(file_source, DocumentFileSource::Css(_)),
}
}
69 changes: 69 additions & 0 deletions crates/biome_cli/tests/commands/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,72 @@ fn search_js_pattern() {
result,
));
}

#[test]
fn search_js_pattern_skips_css_files() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let js_file_path = Utf8Path::new("file.js");
let css_file_path = Utf8Path::new("file.css");
fs.insert(js_file_path.into(), JS_FILE_CONTENT.as_bytes());
fs.insert(css_file_path.into(), CSS_FILE_CONTENT.as_bytes());

let (fs, result) = run_cli(
fs,
&mut console,
Args::from(
[
"search",
"`\"foo\"`",
js_file_path.as_str(),
css_file_path.as_str(),
]
.as_slice(),
),
);

assert!(result.is_ok(), "run_cli returned {result:?}");
assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"search_js_pattern_skips_css_files",
fs,
console,
result,
));
}

#[test]
fn search_css_pattern_skips_js_files() {
let mut fs = MemoryFileSystem::default();
let mut console = BufferConsole::default();

let js_file_path = Utf8Path::new("file.js");
let css_file_path = Utf8Path::new("file.css");
fs.insert(js_file_path.into(), JS_FILE_CONTENT.as_bytes());
fs.insert(css_file_path.into(), CSS_FILE_CONTENT.as_bytes());

let (fs, result) = run_cli(
fs,
&mut console,
Args::from(
[
"search",
"--language=css",
"`color: green`",
js_file_path.as_str(),
css_file_path.as_str(),
]
.as_slice(),
),
);

assert!(result.is_ok(), "run_cli returned {result:?}");
assert_cli_snapshot(SnapshotPayload::new(
module_path!(),
"search_css_pattern_skips_js_files",
fs,
console,
result,
));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: redactor(content)
---
## `file.css`

```css
div {
color: green;
}
```

## `file.js`

```js
const a = 'foo';
```

# Emitted Messages

```block
file.css:2:5 search ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

2 │ color: green;

```

```block
Searched 1 file in <TIME>. Found 1 match.
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
source: crates/biome_cli/tests/snap_test.rs
expression: redactor(content)
---
## `file.css`

```css
div {
color: green;
}
```

## `file.js`

```js
const a = 'foo';
```

# Emitted Messages

```block
file.js:1:11 search ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

1 │ const a = 'foo';

```

```block
Searched 1 file in <TIME>. Found 1 match.
```
Loading