mirror of
https://github.com/openai/codex.git
synced 2026-04-24 06:35:50 +00:00
Add read_file_text to executor filesystem
This commit is contained in:
@@ -171,7 +171,7 @@ pub async fn maybe_parse_apply_patch_verified(
|
||||
path.as_path(),
|
||||
&effective_cwd,
|
||||
);
|
||||
let original_bytes = match fs.read_file(&path_abs).await {
|
||||
let content = match fs.read_file_text(&path_abs).await {
|
||||
Ok(content) => content,
|
||||
Err(e) => {
|
||||
return MaybeApplyPatchVerified::CorrectnessError(
|
||||
@@ -182,20 +182,6 @@ pub async fn maybe_parse_apply_patch_verified(
|
||||
);
|
||||
}
|
||||
};
|
||||
let content = match String::from_utf8(original_bytes) {
|
||||
Ok(content) => content,
|
||||
Err(e) => {
|
||||
return MaybeApplyPatchVerified::CorrectnessError(
|
||||
ApplyPatchError::IoError(IoError {
|
||||
context: format!("Failed to read {}", path.display()),
|
||||
source: std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidData,
|
||||
e,
|
||||
),
|
||||
}),
|
||||
);
|
||||
}
|
||||
};
|
||||
changes.insert(path, ApplyPatchFileChange::Delete { content });
|
||||
}
|
||||
Hunk::UpdateFile {
|
||||
|
||||
@@ -391,18 +391,12 @@ async fn derive_new_contents_from_chunks(
|
||||
fs: &dyn ExecutorFileSystem,
|
||||
) -> std::result::Result<AppliedPatch, ApplyPatchError> {
|
||||
let path_abs = AbsolutePathBuf::resolve_path_against_base(path, cwd);
|
||||
let original_bytes = fs.read_file(&path_abs).await.map_err(|err| {
|
||||
let original_contents = fs.read_file_text(&path_abs).await.map_err(|err| {
|
||||
ApplyPatchError::IoError(IoError {
|
||||
context: format!("Failed to read file to update {}", path.display()),
|
||||
source: err,
|
||||
})
|
||||
})?;
|
||||
let original_contents = String::from_utf8(original_bytes).map_err(|err| {
|
||||
ApplyPatchError::IoError(IoError {
|
||||
context: format!("Failed to read file to update {}", path.display()),
|
||||
source: io::Error::new(io::ErrorKind::InvalidData, err),
|
||||
})
|
||||
})?;
|
||||
|
||||
let mut original_lines: Vec<String> = original_contents.split('\n').map(String::from).collect();
|
||||
|
||||
|
||||
@@ -39,6 +39,12 @@ pub type FileSystemResult<T> = io::Result<T>;
|
||||
pub trait ExecutorFileSystem: Send + Sync {
|
||||
async fn read_file(&self, path: &AbsolutePathBuf) -> FileSystemResult<Vec<u8>>;
|
||||
|
||||
/// Reads a file and decodes it as UTF-8 text.
|
||||
async fn read_file_text(&self, path: &AbsolutePathBuf) -> FileSystemResult<String> {
|
||||
let bytes = self.read_file(path).await?;
|
||||
String::from_utf8(bytes).map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))
|
||||
}
|
||||
|
||||
async fn write_file(&self, path: &AbsolutePathBuf, contents: Vec<u8>) -> FileSystemResult<()>;
|
||||
|
||||
async fn create_directory(
|
||||
|
||||
@@ -122,6 +122,12 @@ async fn file_system_methods_cover_surface_area(use_remote: bool) -> Result<()>
|
||||
.with_context(|| format!("mode={use_remote}"))?;
|
||||
assert_eq!(nested_file_contents, b"hello from trait");
|
||||
|
||||
let nested_file_text = file_system
|
||||
.read_file_text(&absolute_path(nested_file.clone()))
|
||||
.await
|
||||
.with_context(|| format!("mode={use_remote}"))?;
|
||||
assert_eq!(nested_file_text, "hello from trait");
|
||||
|
||||
file_system
|
||||
.copy(
|
||||
&absolute_path(nested_file),
|
||||
|
||||
Reference in New Issue
Block a user