Skip to content
Open
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
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/forge_fs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ tokio.workspace = true
anyhow.workspace = true
sha2.workspace = true
hex.workspace = true
infer = "0.22.0" # For binary file detection
thiserror = "2.0"
bstr.workspace = true

Expand Down
104 changes: 0 additions & 104 deletions crates/forge_fs/src/is_binary.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/forge_fs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
mod binary_detection;
mod error;
mod file_size;
mod is_binary;
mod meta;
mod read;
mod read_range;
Expand Down
31 changes: 23 additions & 8 deletions crates/forge_fs/src/read_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use bstr::ByteSlice;
use forge_domain::FileInfo;

use crate::error::Error;
use crate::is_binary;

impl crate::ForgeFS {
/// Reads a specific range of lines from a file.
Expand Down Expand Up @@ -33,18 +34,18 @@ impl crate::ForgeFS {
return Err(Error::StartGreaterThanEnd { start: start_line, end: end_line }.into());
}

// Open and check if file is binary
let mut file = tokio::fs::File::open(path_ref)
.await
.with_context(|| format!("Failed to open file {}", path_ref.display()))?;

if start_line == 0 || end_line == 0 {
return Err(Error::IndexStartingWithZero { start: start_line, end: end_line }.into());
}

let (is_text, file_type) = Self::is_binary(&mut file).await?;
if !is_text {
return Err(Error::BinaryFileNotSupported(file_type).into());
// Use the same BOM + zero-byte detector that fs_search and other
// callers use, so binary classification is consistent across all
// code paths. The previous infer-based check could classify a file
// as text here while fs_search skipped it as binary.
if is_binary(path_ref).await? {
return Err(Error::BinaryFileNotSupported(
"binary content detected".into(),
).into());
}

// Read file content
Expand Down Expand Up @@ -332,4 +333,18 @@ mod test {

Ok(())
}

#[tokio::test]
async fn test_stray_zero_byte_classified_as_binary() -> Result<()> {
// Reproduces #3633: a file with a stray 0x00 in the first 512 bytes
// should be rejected as binary by read_range_utf8, matching what
// fs_search sees via the BOM-based detector.
let content = b"KEY=value\0trailing text\n";
let file = tempfile::NamedTempFile::new()?;
fs::write(file.path(), content).await?;

let result = crate::ForgeFS::read_range_utf8(file.path(), 1, 1).await;
assert!(result.is_err(), "file with stray NUL should be rejected as binary");
Ok(())
}
}
Loading