Skip to content

Commit 5e210c7

Browse files
committed
fix: Handle filenames starting with fences or triple backticks correctly
1 parent c6ce871 commit 5e210c7

File tree

2 files changed

+16
-37
lines changed

2 files changed

+16
-37
lines changed

aider/coders/editblock_coder.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,11 +414,15 @@ def strip_filename(filename, fence):
414414
start_fence = fence[0]
415415
if filename.startswith(start_fence):
416416
candidate = filename[len(start_fence) :]
417-
if candidate and "." in candidate:
417+
if candidate and ("." in candidate or "/" in candidate):
418418
return candidate
419+
return
419420

420421
if filename.startswith(triple_backticks):
421-
filename = filename[len(triple_backticks) :]
422+
candidate = filename[len(triple_backticks) :]
423+
if candidate and ("." in candidate or "/" in candidate):
424+
return candidate
425+
return
422426

423427
filename = filename.rstrip(":")
424428
filename = filename.lstrip("#")

tests/basic/test_editblock.py

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -108,29 +108,6 @@ def test_find_original_update_blocks(self):
108108
edits = list(eb.find_original_update_blocks(edit))
109109
self.assertEqual(edits, [("foo.txt", "Two\n", "Tooooo\n")])
110110

111-
def test_find_original_update_blocks_mangled_filename_w_source_tag(self):
112-
source = "source"
113-
114-
edit = """
115-
Here's the change:
116-
117-
<%s>foo.txt
118-
<<<<<<< SEARCH
119-
One
120-
=======
121-
Two
122-
>>>>>>> REPLACE
123-
</%s>
124-
125-
Hope you like it!
126-
""" % (source, source)
127-
128-
fence = ("<%s>" % source, "</%s>" % source)
129-
130-
with self.assertRaises(ValueError) as cm:
131-
_edits = list(eb.find_original_update_blocks(edit, fence))
132-
self.assertIn("missing filename", str(cm.exception))
133-
134111
def test_find_original_update_blocks_quote_below_filename(self):
135112
edit = """
136113
Here's the change:
@@ -181,10 +158,11 @@ def test_find_original_update_blocks_missing_filename(self):
181158
182159
183160
oops!
161+
>>>>>>> REPLACE
184162
"""
185163

186164
with self.assertRaises(ValueError) as cm:
187-
list(eb.find_original_update_blocks(edit))
165+
_blocks = list(eb.find_original_update_blocks(edit))
188166
self.assertIn("filename", str(cm.exception))
189167

190168
def test_find_original_update_blocks_no_final_newline(self):
@@ -575,7 +553,7 @@ def test_find_original_update_blocks_quad_backticks_with_triples_in_LLM_reply(se
575553
edits = list(eb.find_original_update_blocks(edit, fence=quad_backticks))
576554
self.assertEqual(edits, [("foo.txt", "", "Tooooo\n")])
577555

578-
#Test for shell script blocks with sh language identifier (issue #3785)
556+
# Test for shell script blocks with sh language identifier (issue #3785)
579557
def test_find_original_update_blocks_with_sh_language_identifier(self):
580558
# https://github.com/Aider-AI/aider/issues/3785
581559
edit = """
@@ -609,13 +587,13 @@ def test_find_original_update_blocks_with_sh_language_identifier(self):
609587
# Check that the content contains the expected shell script elements
610588
result_content = edits[0][2]
611589
self.assertIn("#!/bin/bash", result_content)
612-
self.assertIn("if [ \"$#\" -ne 1 ];", result_content)
613-
self.assertIn("echo \"Usage: $0 <argument>\"", result_content)
590+
self.assertIn('if [ "$#" -ne 1 ];', result_content)
591+
self.assertIn('echo "Usage: $0 <argument>"', result_content)
614592
self.assertIn("exit 1", result_content)
615-
self.assertIn("echo \"$1\"", result_content)
593+
self.assertIn('echo "$1"', result_content)
616594
self.assertIn("exit 0", result_content)
617595

618-
#Test for C# code blocks with csharp language identifier
596+
# Test for C# code blocks with csharp language identifier
619597
def test_find_original_update_blocks_with_csharp_language_identifier(self):
620598
edit = """
621599
Here's a C# code change:
@@ -631,12 +609,9 @@ def test_find_original_update_blocks_with_csharp_language_identifier(self):
631609
"""
632610

633611
edits = list(eb.find_original_update_blocks(edit))
634-
search_text = "Console.WriteLine(\"Hello World!\");\n"
635-
replace_text = "Console.WriteLine(\"Hello, C# World!\");\n"
636-
self.assertEqual(
637-
edits,
638-
[("Program.cs", search_text, replace_text)]
639-
)
612+
search_text = 'Console.WriteLine("Hello World!");\n'
613+
replace_text = 'Console.WriteLine("Hello, C# World!");\n'
614+
self.assertEqual(edits, [("Program.cs", search_text, replace_text)])
640615

641616

642617
if __name__ == "__main__":

0 commit comments

Comments
 (0)