Fix environment for interactive executors#908
Conversation
for more information, see https://pre-commit.ci
📝 WalkthroughWalkthroughMoved the environment-helper Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 🧹 Recent nitpick comments
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
🤖 Fix all issues with AI agents
In `@src/executorlib/standalone/interactive/spawner.py`:
- Around line 201-210: The function set_current_directory_in_environment
currently checks membership with a substring search against
environment["PYTHONPATH"], which can yield false positives/negatives; change it
to split PYTHONPATH using os.pathsep into individual entries and compare the
absolute/current_path against those entries (prefer os.path.abspath(os.getcwd())
stored in current_path) to determine if it already exists, and when
prepending/appending use os.pathsep to join paths (replace the second
os.getcwd() call with current_path). Reference:
set_current_directory_in_environment, environment, current_path, and
environment["PYTHONPATH"].
| def set_current_directory_in_environment(): | ||
| """ | ||
| Add the current directory to the PYTHONPATH to be able to access local Python modules. | ||
| """ | ||
| environment = os.environ | ||
| current_path = os.getcwd() | ||
| if "PYTHONPATH" in environment and current_path not in environment["PYTHONPATH"]: | ||
| environment["PYTHONPATH"] = os.getcwd() + ":" + environment["PYTHONPATH"] | ||
| elif "PYTHONPATH" not in environment: | ||
| environment["PYTHONPATH"] = os.getcwd() |
There was a problem hiding this comment.
Substring match on PYTHONPATH can produce false positives/negatives.
Line 207 uses current_path not in environment["PYTHONPATH"], which is a substring search on the raw string, not a check against individual path entries. For example, if cwd is /home/user/pro and PYTHONPATH is /home/user/project, the check passes incorrectly (path not added when it should be — or vice versa).
Split on os.pathsep and compare against actual path components instead.
Proposed fix
def set_current_directory_in_environment():
"""
Add the current directory to the PYTHONPATH to be able to access local Python modules.
"""
environment = os.environ
current_path = os.getcwd()
- if "PYTHONPATH" in environment and current_path not in environment["PYTHONPATH"]:
- environment["PYTHONPATH"] = os.getcwd() + ":" + environment["PYTHONPATH"]
+ if "PYTHONPATH" in environment and current_path not in environment["PYTHONPATH"].split(os.pathsep):
+ environment["PYTHONPATH"] = current_path + os.pathsep + environment["PYTHONPATH"]
elif "PYTHONPATH" not in environment:
- environment["PYTHONPATH"] = os.getcwd()
+ environment["PYTHONPATH"] = current_pathThis also replaces the redundant second os.getcwd() call with the already-captured current_path and uses os.pathsep for cross-platform correctness.
🤖 Prompt for AI Agents
In `@src/executorlib/standalone/interactive/spawner.py` around lines 201 - 210,
The function set_current_directory_in_environment currently checks membership
with a substring search against environment["PYTHONPATH"], which can yield false
positives/negatives; change it to split PYTHONPATH using os.pathsep into
individual entries and compare the absolute/current_path against those entries
(prefer os.path.abspath(os.getcwd()) stored in current_path) to determine if it
already exists, and when prepending/appending use os.pathsep to join paths
(replace the second os.getcwd() call with current_path). Reference:
set_current_directory_in_environment, environment, current_path, and
environment["PYTHONPATH"].
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #908 +/- ##
==========================================
+ Coverage 93.59% 93.60% +0.01%
==========================================
Files 38 38
Lines 1888 1891 +3
==========================================
+ Hits 1767 1770 +3
Misses 121 121 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Summary by CodeRabbit
Refactor
Bug Fixes / Reliability
Tests