Skip to content
This repository was archived by the owner on Jun 9, 2025. It is now read-only.

Commit 61cfefb

Browse files
Write invalid generated code to a file (#30)
1 parent ef9a3fa commit 61cfefb

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/betterproto2_compiler/plugin/compiler.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,23 @@ def outputfile_compiler(output_file: OutputTemplate) -> str:
4040
code = body_template.render(output_file=output_file)
4141
code = header_template.render(output_file=output_file, version=version) + "\n" + code
4242

43-
# Sort imports, delete unused ones
44-
code = subprocess.check_output(
45-
["ruff", "check", "--select", "I,F401,TCH005", "--fix", "--silent", "-"],
46-
input=code,
47-
encoding="utf-8",
48-
)
43+
try:
44+
# Sort imports, delete unused ones
45+
code = subprocess.check_output(
46+
["ruff", "check", "--select", "I,F401,TCH005", "--fix", "--silent", "-"],
47+
input=code,
48+
encoding="utf-8",
49+
)
50+
51+
# Format the code
52+
code = subprocess.check_output(["ruff", "format", "-"], input=code, encoding="utf-8")
53+
except subprocess.CalledProcessError:
54+
with open("invalid-generated-code.py", "w") as f:
55+
f.write(code)
4956

50-
# Format the code
51-
code = subprocess.check_output(["ruff", "format", "-"], input=code, encoding="utf-8")
57+
raise SyntaxError(
58+
f"Can't format the source code:\nThe invalid generated code has been written in `invalid-generated-code.py`"
59+
)
5260

5361
# Validate the generated code.
5462
validator = ModuleValidator(iter(code.splitlines()))

0 commit comments

Comments
 (0)