mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-24 14:25:00 +00:00
fix: handle empty original content when creating new files
This commit is contained in:
parent
85f23b3408
commit
c71a92ac84
2 changed files with 44 additions and 1 deletions
|
@ -52,7 +52,10 @@ class EditBlockCoder(Coder):
|
||||||
content = self.io.read_text(full_path)
|
content = self.io.read_text(full_path)
|
||||||
new_content = do_replace(full_path, content, original, updated, self.fence)
|
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
|
# try patching any of the other files in the chat
|
||||||
for full_path in self.abs_fnames:
|
for full_path in self.abs_fnames:
|
||||||
content = self.io.read_text(full_path)
|
content = self.io.read_text(full_path)
|
||||||
|
|
|
@ -10,6 +10,7 @@ from aider.coders import editblock_coder as eb
|
||||||
from aider.dump import dump # noqa: F401
|
from aider.dump import dump # noqa: F401
|
||||||
from aider.io import InputOutput
|
from aider.io import InputOutput
|
||||||
from aider.models import Model
|
from aider.models import Model
|
||||||
|
from aider.utils import GitTemporaryDirectory
|
||||||
|
|
||||||
|
|
||||||
class TestUtils(unittest.TestCase):
|
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)
|
result = eb.replace_most_similar_chunk(whole, part, replace)
|
||||||
self.assertEqual(result, expected_output)
|
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):
|
def test_full_edit(self):
|
||||||
# Create a few temporary files
|
# Create a few temporary files
|
||||||
_, file1 = tempfile.mkstemp()
|
_, file1 = tempfile.mkstemp()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue