add --compile-only flag for syntax/compile validation#6943
add --compile-only flag for syntax/compile validation#6943Jawfish wants to merge 7 commits intoRustPython:mainfrom
--compile-only flag for syntax/compile validation#6943Conversation
📝 WalkthroughWalkthroughAdds a compile-only execution mode: a new Changes
Sequence Diagram(s)mermaid Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/settings.rs (1)
37-56:⚠️ Potential issue | 🟠 Major
--compile-onlydoesn't actually terminate option parsing, contradicting its documented behavior.The flag only sets a boolean at line 156, allowing lexopt to continue parsing subsequent args as options. This breaks the documented "terminates option list" behavior specified at line 105 of USAGE_STRING (e.g.,
--compile-only -V file.pywill triggerversion()instead of treating-Vas a filename). The fix should return early and collect remaining raw args as files, matching the pattern used by-cand-mflags. This also eliminates the unnecessarycompile_onlyfield and related conditional branches.
🤖 Fix all issues with AI agents
In `@src/lib.rs`:
- Around line 292-295: The current --compile-only path returns Ok(()) when the
files list is empty; change it to signal a usage error so the process exits
non‑zero: in the block that checks files.is_empty() (the --compile-only
handling), replace the Ok(()) return with a non‑zero failure return (e.g.,
return an Err or otherwise cause process exit code 1) and keep the eprintln!
message; update the surrounding function’s error/return handling (the main or
run function that contains files.is_empty()) so the non‑zero exit propagates to
the process.
--compile-only flag for syntax/compile validation
|
@Jawfish Could you tell me more about the motivation of this optimization? Usually compile step is very fast, as you described, only 55ms. Are you compiling massive amount of files before running? |
This isn't meant as a micro-optimization of compile time (55ms is totally fine). The motivation is to have a supported, side-effect-free "compile check" mode for CI/pre-commit: validate that a set of .py files reaches the compiler successfully. This brings CPython's [i for i in (j := [1, 2, 3])]
# SyntaxError: assignment expression cannot be used in a comprehension iterable expressionThis is a compile-phase semantic check that occurs after parsing succeeds. Linters cover many cases with lint rules, but not all compile-phase validations. |
Have you tried to add this to CPython? |
|
Could you share more what If |
@youknowone to be clearer: the motivation is specifically to guarantee compilation with no .pyc file side effects, which
@fanninpm Per the project goals: "Full Python-3 environment entirely in Rust (not CPython bindings)". A --compile-only flag would provide compile validation as a first-class RustPython feature, without side effects. |
|
Edit: I withdraw this comment Thank you for your patient. So what you need is compiling python code and see the error output, but not actually creating Because
|
|
@Jawfish If you don't care about performance, actually |
Adds a
--compile-onlyCLI flag that compiles Python files without executing them.Use case: Catch compile-phase errors (syntax errors, duplicate parameters,
breakoutside loop, etc.) without the overhead of execution. This is similar topython -m py_compilebut leverages RustPython.Usage:
Exits 0 on success, 1 if any file fails to compile. Errors are printed to stderr.
rustpython -m py_compileworks, but there are differences:Implementation:
RunMode::CompileOnly(Vec<String>)variant--compile-onlyflag terminates option parsing (remaining args are files)vm.compile()withMode::Execbreakoutside loopNote: also documents existing
--install-pip [ensurepip|get-pip]flag which was missing from help text.Summary by CodeRabbit
New Features
Tests
✏️ Tip: You can customize this high-level summary in your review settings.