Skip to content
Merged
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
17 changes: 15 additions & 2 deletions crates/forge_fs/src/read_range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ impl crate::ForgeFS {
}

// Read file content
let content = tokio::fs::read_to_string(path_ref)
let content = tokio::fs::read(path_ref)
.await
.with_context(|| format!("Failed to read file content from {}", path_ref.display()))?;
let content = String::from_utf8_lossy(&content);
if start_line < 2 && content.is_empty() {
// If the file is empty, return empty content
return Ok((String::new(), FileInfo::new(start_line, end_line, 0)));
Expand All @@ -74,7 +75,7 @@ impl crate::ForgeFS {

// Extract requested lines
let result_content = if start_pos == 0 && end_pos == total_lines - 1 {
content // Return full content if requesting entire file
content.to_string() // Return full content if requesting entire file
} else {
lines[start_pos as usize..=end_pos as usize].join("\n")
};
Expand Down Expand Up @@ -173,4 +174,16 @@ mod test {

Ok(())
}

#[tokio::test]
async fn test_invalid_utf8_handling() -> Result<()> {
let content = b"Hello world!\nValid line\n\xFF\xFE\xFD Invalid UTF-8\nAnother valid line";
let file = tempfile::NamedTempFile::new()?;
fs::write(file.path(), content).await?;

// Attempt to read the file shouldn't fail with invalid UTF-8 error
let result = crate::ForgeFS::read_range_utf8(&file.path(), 1, 4).await;
assert!(result.is_ok());
Ok(())
}
}
Loading