From 01964ca294d5d25ddf85170818e0330065e2d443 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sat, 31 Aug 2024 08:15:02 -0700 Subject: [PATCH] refactor: improve file handling and error reporting in Coder class --- aider/coders/base_coder.py | 28 ++++++++++++++++------------ aider/utils.py | 1 + 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/aider/coders/base_coder.py b/aider/coders/base_coder.py index 10e848a1f..cedbb70aa 100755 --- a/aider/coders/base_coder.py +++ b/aider/coders/base_coder.py @@ -349,20 +349,23 @@ class Coder: for fname in fnames: fname = Path(fname) - if not fname.exists(): - self.io.tool_output(f"Creating empty file {fname}") - fname.parent.mkdir(parents=True, exist_ok=True) - utils.touch_file(fname) - - if not fname.is_file(): - raise ValueError(f"{fname} is not a file") - - fname = str(fname.resolve()) - if self.repo and self.repo.ignored_file(fname): self.io.tool_error(f"Skipping {fname} that matches aiderignore spec.") continue + if not fname.exists(): + if utils.touch_file(fname): + self.io.tool_output(f"Creating empty file {fname}") + else: + self.io.tool_error(f"Can not create {fname}, skipping.") + continue + + if not fname.is_file(): + self.io.tool_error(f"Skipping {fname} that is not a normal file.") + continue + + fname = str(fname.resolve()) + self.abs_fnames.add(fname) self.check_added_files() @@ -1677,8 +1680,9 @@ class Coder: return if not self.dry_run: - Path(full_path).parent.mkdir(parents=True, exist_ok=True) - utils.touch_file(Path(full_path)) + if not utils.touch_file(full_path): + self.io.tool_error(f"Unable to create {path}, skipping edits.") + return # Seems unlikely that we needed to create the file, but it was # actually already part of the repo. diff --git a/aider/utils.py b/aider/utils.py index 1f3eb0455..48849f567 100644 --- a/aider/utils.py +++ b/aider/utils.py @@ -292,6 +292,7 @@ def format_tokens(count): def touch_file(fname): + fname = Path(fname) try: fname.parent.mkdir(parents=True, exist_ok=True) fname.touch()