Skip to content

Commit 16ab26b

Browse files
Fix IndexError in lit_to_bazel for files without run commands
get_command_without_bazel_prefix crashes with an IndexError when the input .mlir file has no // RUN: lines (or only FileCheck RUN lines), because commands[-1] is accessed on an empty list. Guard both the trailing-pipe pop and the consecutive-pipe dedup against an empty list so these inputs return an empty command instead of crashing.
1 parent eb24873 commit 16ab26b

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

scripts/lit_to_bazel_lib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,11 @@ def get_command_without_bazel_prefix(lit_test_file) -> str:
130130
commands = convert_to_run_commands(run_lines)
131131
commands = [x for x in commands if "FileCheck" not in x]
132132
# remove consecutive and trailing pipes
133-
if commands[-1] == PIPE:
133+
if commands and commands[-1] == PIPE:
134134
commands.pop()
135135
deduped_commands = []
136136
for command in commands:
137-
if command == PIPE and deduped_commands[-1] == PIPE:
137+
if command == PIPE and deduped_commands and deduped_commands[-1] == PIPE:
138138
continue
139139
deduped_commands.append(command)
140140

scripts/test_lit_to_bazel.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
PIPE = lit_to_bazel_lib.PIPE
99
convert_to_run_commands = lit_to_bazel_lib.convert_to_run_commands
1010
normalize_lit_test_file_arg = lit_to_bazel_lib.normalize_lit_test_file_arg
11+
get_command_without_bazel_prefix = (
12+
lit_to_bazel_lib.get_command_without_bazel_prefix
13+
)
1114

1215

1316
class LitToBazelTest(absltest.TestCase):
@@ -186,6 +189,35 @@ def side_effect(path):
186189
"/workspace/tests/foo.mlir",
187190
)
188191

192+
def test_get_command_without_bazel_prefix_no_run_lines(self):
193+
"""A file with no RUN lines should yield an empty command, not crash."""
194+
with absltest.mock.patch(
195+
"builtins.open",
196+
absltest.mock.mock_open(read_data="func.func @foo() -> i8\n"),
197+
):
198+
self.assertEqual(get_command_without_bazel_prefix("fake.mlir"), "")
199+
200+
def test_get_command_without_bazel_prefix_only_filecheck(self):
201+
"""A file whose only RUN line is FileCheck should yield an empty command."""
202+
with absltest.mock.patch(
203+
"builtins.open",
204+
absltest.mock.mock_open(read_data="// RUN: FileCheck %s\n"),
205+
):
206+
self.assertEqual(get_command_without_bazel_prefix("fake.mlir"), "")
207+
208+
def test_get_command_without_bazel_prefix_filecheck_then_pipe(self):
209+
"""A FileCheck command followed by a pipe should not crash."""
210+
with absltest.mock.patch(
211+
"builtins.open",
212+
absltest.mock.mock_open(
213+
read_data="// RUN: FileCheck %s | heir-opt --canonicalize\n"
214+
),
215+
):
216+
self.assertEqual(
217+
get_command_without_bazel_prefix("fake.mlir"),
218+
"| heir-opt --canonicalize",
219+
)
220+
189221

190222
if __name__ == "__main__":
191223
absltest.main()

0 commit comments

Comments
 (0)