diff --git a/aider/coders/editblock_coder.py b/aider/coders/editblock_coder.py index 316205e72..ecd94e47a 100644 --- a/aider/coders/editblock_coder.py +++ b/aider/coders/editblock_coder.py @@ -52,7 +52,10 @@ class EditBlockCoder(Coder): content = self.io.read_text(full_path) new_content = do_replace(full_path, content, original, updated, self.fence) - if not new_content: + # If the edit failed, and + # this is not a "create a new file" with an empty original... + # https://github.com/Aider-AI/aider/issues/2258 + if not new_content and original.strip(): # try patching any of the other files in the chat for full_path in self.abs_fnames: content = self.io.read_text(full_path) diff --git a/tests/basic/test_editblock.py b/tests/basic/test_editblock.py index 4b3817b1c..e018c12b4 100644 --- a/tests/basic/test_editblock.py +++ b/tests/basic/test_editblock.py @@ -10,6 +10,7 @@ from aider.coders import editblock_coder as eb from aider.dump import dump # noqa: F401 from aider.io import InputOutput from aider.models import Model +from aider.utils import GitTemporaryDirectory class TestUtils(unittest.TestCase): @@ -341,6 +342,45 @@ These changes replace the `subprocess.run` patches with `subprocess.check_output result = eb.replace_most_similar_chunk(whole, part, replace) self.assertEqual(result, expected_output) + def test_create_new_file_with_other_file_in_chat(self): + # https://github.com/Aider-AI/aider/issues/2258 + with GitTemporaryDirectory(): + # Create a few temporary files + file1 = "file.txt" + + with open(file1, "w", encoding="utf-8") as f: + f.write("one\ntwo\nthree\n") + + files = [file1] + + # Initialize the Coder object with the mocked IO and mocked repo + coder = Coder.create(self.GPT35, "diff", io=InputOutput(yes=True), fnames=files) + + def mock_send(*args, **kwargs): + coder.partial_response_content = f""" +Do this: + +newfile.txt +<<<<<<< SEARCH +======= +creating a new file +>>>>>>> REPLACE + +""" + coder.partial_response_function_call = dict() + return [] + + coder.send = mock_send + + # Call the run method with a message + coder.run(with_message="hi") + + content = Path(file1).read_text(encoding="utf-8") + self.assertEqual(content, "one\ntwo\nthree\n") + + content = Path("newfile.txt").read_text(encoding="utf-8") + self.assertEqual(content, "creating a new file\n") + def test_full_edit(self): # Create a few temporary files _, file1 = tempfile.mkstemp()