Skip to content

Conversation

@youknowone
Copy link
Member

@youknowone youknowone commented Dec 26, 2025

Summary by CodeRabbit

  • Bug Fixes

    • Improved environment variable path processing by filtering empty values to prevent empty strings from propagating through path lists.
  • Refactor

    • Adjusted initialization sequence to import site modules before configuring sys.path[0], aligning with standard Python behavior. Path handling now includes run-mode-specific configurations.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 26, 2025

Warning

Rate limit exceeded

@youknowone has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 19 minutes and 7 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 196bca1 and 096ecd0.

⛔ Files ignored due to path filters (1)
  • Lib/test/test_cmd_line_script.py is excluded by !Lib/**
📒 Files selected for processing (2)
  • src/lib.rs
  • src/settings.rs
📝 Walkthrough

Walkthrough

The changes reorder sys.path[0] initialization in lib.rs to align with CPython's behavior: site import now precedes path setup, with per-mode conditional path0 computation. Additionally, settings.rs filters empty environment variable values before path splitting to prevent empty strings from propagating through PATH-like variables.

Changes

Cohort / File(s) Summary
sys.path[0] initialization reordering
src/lib.rs
Reorders initialization sequence so site module imports before sys.path[0] configuration. Adds conditional path0 computation based on run_mode (Command/Repl → empty string; Module → cwd; Script/InstallPip → None). Inserts computed path into sys.path only when path0 is Some(...).
Environment PATH filtering
src/settings.rs
Filters out empty environment variable values in get_paths before path splitting, preventing empty strings from accumulating in PATH-like variables.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • RustPython/RustPython#6461: Modifies sys.path[0] initialization in src/lib.rs with ordering changes and conditional empty string insertion logic.

Poem

🐰 Paths now sorted just like CPython's way,
Site comes first to clean the day,
Empty strings we cast aside,
Initialization flows with pride!
Hop, hop, hop—initialization's right!

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix sys path' is directly related to the main changes in the PR, which reorder sys.path initialization and fix path configuration across different run modes.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@youknowone youknowone marked this pull request as ready for review December 26, 2025 11:52
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c9bf8df and 196bca1.

⛔ Files ignored due to path filters (1)
  • Lib/test/test_cmd_line_script.py is excluded by !Lib/**
📒 Files selected for processing (2)
  • src/lib.rs
  • src/settings.rs
🧰 Additional context used
📓 Path-based instructions (1)
**/*.rs

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

**/*.rs: Follow the default rustfmt code style by running cargo fmt to format Rust code
Always run clippy to lint Rust code (cargo clippy) before completing tasks and fix any warnings or lints introduced by changes
Follow Rust best practices for error handling and memory management
Use the macro system (pyclass, pymodule, pyfunction, etc.) when implementing Python functionality in Rust

Files:

  • src/lib.rs
  • src/settings.rs
🧠 Learnings (2)
📓 Common learnings
Learnt from: CR
Repo: RustPython/RustPython PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-11-29T12:17:28.606Z
Learning: Applies to Lib/**/*.py : Minimize modifications to CPython standard library files in the `Lib/` directory; modifications should be minimal and only to work around RustPython limitations
Learnt from: moreal
Repo: RustPython/RustPython PR: 5847
File: vm/src/stdlib/stat.rs:547-567
Timestamp: 2025-06-27T14:47:28.810Z
Learning: In RustPython's stat module implementation, platform-specific constants like SF_SUPPORTED and SF_SYNTHETIC should be conditionally declared only for the platforms where they're available (e.g., macOS), following CPython's approach of optional declaration rather than providing fallback values for other platforms.
Learnt from: moreal
Repo: RustPython/RustPython PR: 5847
File: vm/src/stdlib/stat.rs:547-567
Timestamp: 2025-06-27T14:47:28.810Z
Learning: In RustPython's stat module implementation, platform-specific constants like SF_SUPPORTED and SF_SYNTHETIC should be conditionally declared only for the platforms where they're available (e.g., macOS), following CPython's approach of optional declaration using #ifdef checks rather than providing fallback values for other platforms.
📚 Learning: 2025-11-29T12:17:28.606Z
Learnt from: CR
Repo: RustPython/RustPython PR: 0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-11-29T12:17:28.606Z
Learning: Applies to Lib/**/*.py : Minimize modifications to CPython standard library files in the `Lib/` directory; modifications should be minimal and only to work around RustPython limitations

Applied to files:

  • src/lib.rs
🔇 Additional comments (2)
src/settings.rs (1)

342-342: LGTM!

The filter correctly prevents empty environment variables from being split and propagated as empty path entries. This improves robustness when handling PATH-like environment variables.

src/lib.rs (1)

172-181: LGTM!

The reordering to import site before configuring sys.path[0] correctly aligns with CPython's behavior. The comment clearly explains the rationale, and error handling is appropriate.

Comment on lines +183 to +197
// _PyPathConfig_ComputeSysPath0 - set sys.path[0] after site import
if !vm.state.config.settings.safe_path {
let path0: Option<String> = match &run_mode {
RunMode::Command(_) => Some(String::new()),
RunMode::Module(_) => env::current_dir()
.ok()
.and_then(|p| p.to_str().map(|s| s.to_owned())),
RunMode::Script(_) | RunMode::InstallPip(_) => None, // handled by run_script
RunMode::Repl => Some(String::new()),
};

if let Some(path) = path0 {
vm.insert_sys_path(vm.new_pyobj(path))?;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Search for current_dir usage patterns and error handling
rg -nP -C3 'current_dir\(\)' --type=rust

Repository: RustPython/RustPython

Length of output: 944


🏁 Script executed:

#!/bin/bash
# Read the context around the reviewed lines to understand the broader context
cat -n src/lib.rs | sed -n '170,210p'

Repository: RustPython/RustPython

Length of output: 1904


🏁 Script executed:

#!/bin/bash
# Search for RunMode enum definition and Module variant usage
rg -nP 'enum RunMode|RunMode::Module' --type=rust -B2 -A2 | head -50

# Search for tests related to this initialization code
rg -nP 'test.*current_dir|test.*path0|test.*Module' --type=rust | head -30

Repository: RustPython/RustPython

Length of output: 1151


Consider logging a warning for Module mode when current directory is unavailable, consistent with site import error handling.

The sys.path[0] initialization for RunMode::Module silently converts env::current_dir() errors to None (lines 187-189), which is inconsistent with the site import error handling above it (lines 177-180). While graceful degradation is acceptable, adding a warning log for Module mode failures would improve debugging and match the established error handling pattern in this initialization sequence.

🤖 Prompt for AI Agents
In src/lib.rs around lines 183 to 197, the RunMode::Module branch silently drops
errors from env::current_dir(); instead of swallowing the error, capture the
Result error and emit a warning using the same warning/logging mechanism used
above for site import failures, then fall back to None; i.e., if
env::current_dir() fails, log a warning that includes the error and context
(Module mode failing to determine current directory) and then proceed with None
so behavior is unchanged but diagnostics are improved.

@youknowone youknowone merged commit 57b4b4a into RustPython:main Dec 26, 2025
12 of 13 checks passed
@youknowone youknowone deleted the sys-path branch December 26, 2025 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant