Repository navigation
fix(core): preserve import precedence in non-editable installs - #4004
Conversation
84a7a82 to
52b8394
Compare
`import nemo_gym` rewrites sys.path via component_search_roots(), moving the install root (PARENT_DIR) behind cwd so a plugin module can shadow a same-named built-in. In a wheel install PARENT_DIR is site-packages, so that reordering put the current working directory ahead of every installed dependency: running from a checkout that contains a package named like a dependency (numpy, pandas, ...) breaks imports of that dependency for the whole process, even under `python -I`. When the install root is a site-packages directory it now keeps its sys.path position and only cwd trails. Source checkouts keep the old order, and file lookup (component_search_roots() without sys_path) is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Lin Jia <[email protected]>
52b8394 to
15008b6
Compare
ananthsub
left a comment
There was a problem hiding this comment.
Thanks for tracking this down; the bug is real. In a wheel install, import nemo_gym moves site-packages behind cwd, so any regular package or module in the working directory with a dependency's name breaks that dependency for the whole process. A numpy checkout does it, and so does a stray openai.py or utils.py in a user's project. This reproduces without any agent involved, so I agree the fix belongs in core and not in the harness.
I'm requesting changes because the current fix introduces a regression and misses some wheel installs. Both look easy to address in the same two files; details are inline.
How I checked. I built real venvs, one with main's nemo_gym and one with this PR's, plus --target, dist-packages, and source-checkout layouts. I ran the same scenarios against both:
| Scenario | main | This PR |
|---|---|---|
Dependency in cwd and in site-packages (python -I, console script, python -S, HOME unset) |
cwd wins | installed copy wins |
Debian dist-packages |
cwd wins | installed copy wins |
pip install --target directory on PYTHONPATH |
cwd wins | cwd wins (not covered) |
| Server process: server directory sibling module vs installed module with the same name | server directory | installed module (regression) |
| cwd copy of a built-in server or benchmark: import | cwd | built-in (see the docs comment) |
| cwd copy of a built-in: file lookup | cwd | cwd |
| New-name plugin server or benchmark in cwd | cwd | cwd |
NEMO_GYM_EXTRA_ROOTS dependency and override |
extra root | extra root |
Source checkout sys.path order |
unchanged | unchanged |
Re-running _augment_sys_path() |
idempotent | idempotent |
test_discovery.py passes on this head, and the new test fails against main's __init__.py, so it guards the fix it targets.
Requested changes.
- In a wheel install, keep existing
sys.pathentries in place and only append cwd and the install root when missing, so server directories onPYTHONPATHare not demoted (inline, with a tested variant). - Detect the install mode with the existing
_is_editable_installinstead of the directory name andsitelookups. - Add tests for the end-to-end failure, the server directory case, and a non-standard install directory.
- Document that wheel users who override a built-in should use
--search-dir.
Context, not for this PR. anyswe_agent reaches this path by installing Gym into its sandbox runtime with a non-editable pip install (_portable_python.sh#L43) and running the runner with cwd set to the task repository (app.py#L539). harness_agent already works around the same import-time sys.path rewrite by re-inserting its mount after import nemo_gym (agent_runner.py#L25-L31). Once this lands, that workaround may be removable, and making the sys.path augmentation an explicit CLI step instead of an import side effect is worth a separate issue.
Keep existing import paths in place for non-editable installations using the existing install-mode flag. Preserve explicit server sibling imports and support target directories without site-module detection. Add subprocess regressions for dependency collisions, server paths, source installs and explicit plugin overrides, and document --search-dir for built-in overrides. Signed-off-by: Lin Jia <[email protected]>
ananthsub
left a comment
There was a problem hiding this comment.
LGTM, thanks for the contribution!
|
/ok to test 30d30d9 |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/38744917067 |
import nemo_gymcan let a dependency checkout in cwd shadow installed packages, even underpython -I.This fix reuses
_is_editable_installto preserve existing import order for wheels and--targetinstalls, including explicit server paths, while retaining source-checkout and file-lookup precedence from #2061 and #1806.Subprocess tests cover dependency shadowing, server sibling imports, nonstandard install layouts, and explicit built-in overrides; the configuration reference directs users to
--search-dir .when overriding both files and modules.Validation passed 378 focused tests, 18 documentation link tests,
pre-commit run --all-files, andmake docs-check; regression checks fail on the prior implementations.This reproduced core import bug needs no separate issue or model rollout, and full mirrored CI plus Ananth's renewed review remain required.