Skip to content

fix(core): preserve import precedence in non-editable installs - #4004

Merged
ananthsub merged 2 commits into
NVIDIA-NeMo:mainfrom
linj-glitch:linj/search-roots-site-packages
Oct 15, 2026
Merged

ananthsub merged 2 commits into
NVIDIA-NeMo:mainfrom
linj-glitch:linj/search-roots-site-packages

Conversation

@linj-glitch

@linj-glitch linj-glitch commented Oct 4, 2026 •

Copy link
Copy Markdown
Collaborator

import nemo_gym can let a dependency checkout in cwd shadow installed packages, even under python -I.
This fix reuses _is_editable_install to preserve existing import order for wheels and --target installs, 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, and make 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.

@copy-pr-bot

copy-pr-bot Bot commented Oct 4, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@yaoyu-33 yaoyu-33 added area:core Shared APIs, servers, telemetry, health, and registries bug Something isn't working complexity:low Localized change in one scope with a small, straightforward review surface needs-review PR is ready for code review and waiting on a reviewer labels Oct 5, 2026
@linj-glitch
linj-glitch marked this pull request as draft October 5, 2026 06:12
@linj-glitch
linj-glitch force-pushed the linj/search-roots-site-packages branch from 84a7a82 to 52b8394 Compare October 8, 2026 00:15
`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]>

@ananthsub ananthsub left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

  1. In a wheel install, keep existing sys.path entries in place and only append cwd and the install root when missing, so server directories on PYTHONPATH are not demoted (inline, with a tested variant).
  2. Detect the install mode with the existing _is_editable_install instead of the directory name and site lookups.
  3. Add tests for the end-to-end failure, the server directory case, and a non-standard install directory.
  4. 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.

Comment thread nemo_gym/__init__.py Resolved
Comment thread nemo_gym/__init__.py Resolved
Comment thread nemo_gym/__init__.py Resolved
Comment thread tests/unit_tests/test_discovery.py Resolved
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]>
@linj-glitch linj-glitch changed the title fix(core): keep site-packages ahead of cwd in component_search_roots fix(core): preserve import precedence in non-editable installs Oct 15, 2026

@ananthsub ananthsub left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, thanks for the contribution!

@ananthsub

Copy link
Copy Markdown
Contributor

/ok to test 30d30d9

@github-actions

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/38744917067

Merged via the queue into NVIDIA-NeMo:main with commit 0ef51ca Oct 15, 2026
42 of 43 checks passed

This branch was previously deployed

1 inactive deployment
public — 30d30d93 Deployed Oct 15, 2026 by copy-pr-bot[bot] via release / finalize / notify #5479
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core Shared APIs, servers, telemetry, health, and registries bug Something isn't working complexity:low Localized change in one scope with a small, straightforward review surface needs-review PR is ready for code review and waiting on a reviewer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants