Skip to content

Commit 048fecb

Browse files
rnorthclaude
andcommitted
Fix block_throw silently returning whole file when token matches without braces
When block_throw=True and the token appeared as a substring on a line without braces (e.g. block="foo" matching "call_foo();"), found_block was set but no lines were selected, causing the entire file to be returned instead of raising ValueError. Check selected_lines instead of found_block to catch this case. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 3452ef2 commit 048fecb

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

codeinclude/resolver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def select(
4646

4747
delim_count -= line.count("}")
4848

49-
if block_throw and not found_block:
49+
if block_throw and not selected_lines:
5050
raise ValueError(f"Block {block} not found to inject from {filename}")
5151

5252
if inside_block:
@@ -79,7 +79,7 @@ def select(
7979
if inside_matching and not first_line_of_block:
8080
selected_lines.append(line_number)
8181

82-
if block_throw and not found_block:
82+
if block_throw and not selected_lines:
8383
raise ValueError(f"Block {inside_block} not found to inject from {filename}")
8484

8585
if from_token and to_token:

tests/codeinclude/test_resolver.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,28 @@ def test_block_throw(self):
4646
result = select(CODE_BLOCK_EXAMPLE, "test_file", inside_block="nonexistent_block", block_throw=True)
4747

4848

49+
def test_block_throw_when_token_matches_but_no_braces(self):
50+
"""block_throw should raise when a token appears on a line without braces,
51+
rather than silently returning the whole file."""
52+
text = textwrap.dedent("""
53+
void call_foo();
54+
int x = 1;
55+
""")
56+
57+
with self.assertRaises(ValueError):
58+
select(text, "test_file", block="foo", block_throw=True)
59+
60+
def test_inside_block_throw_when_token_matches_but_no_braces(self):
61+
"""inside_block with block_throw should raise when the token appears
62+
on a line without braces, rather than silently returning the whole file."""
63+
text = textwrap.dedent("""
64+
void call_foo();
65+
int x = 1;
66+
""")
67+
68+
with self.assertRaises(ValueError):
69+
select(text, "test_file", inside_block="foo", block_throw=True)
70+
4971
def test_block_curly_on_same_line(self):
5072
result = select(
5173
textwrap.dedent(

0 commit comments

Comments
 (0)