Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uv run --with ... leaves _uv_ephemeral_overlay.pth in ephemeral environment #11117

Closed
daviesian opened this issue Jan 30, 2025 · 4 comments · Fixed by #11141
Closed

uv run --with ... leaves _uv_ephemeral_overlay.pth in ephemeral environment #11117

daviesian opened this issue Jan 30, 2025 · 4 comments · Fixed by #11141
Assignees
Labels
bug Something isn't working cache Caching of packages and metadata great writeup A wonderful example of a quality contribution 💜

Comments

@daviesian
Copy link

daviesian commented Jan 30, 2025

Summary

Inside a project, running uv run --with jupyter jupyter lab correctly adds _uv_ephemeral_overlay.pth to the ephemeral environment created for jupyter. However, the _uv_ephemeral_overlay.pth file is never cleaned up, so subsequent uses of that cached ephemeral environment will see it too. In most cases this doesn't matter because it will be overwritten, but uv tool run (uvx) will not overwrite it. In this example, running uvx jupyter lab elsewhere on the system matches the same cached environment and therefore incorrectly includes the original project virtual env in sys.path.

Example

uv init my-project
cd my-project
uv run --with jupyter jupyter lab

# Print sys.path inside a Jupyter cell. See that the current project venv is correctly on the path
# Ctrl-C to quit Jupyter

cd /some-other-folder
uvx jupyter lab

# Print sys.path inside a Jupyter cell. See that my-project/.venv is incorrectly on the path, 
# despite the fact that this should be an entirely isolated environment

uv cache clean
uvx jupyter lab

# Print sys.path inside a Jupyter cell. See that my-project/.venv is gone, because the
# left-over _uv_ephemeral_overlay.pth is no longer in the ephemeral environment.

This happens with both uvx and uv tool run, and appears to be because uv run --with x and uv tool run x match the same cached ephemeral environment, even though uv run leaves behind the .pth file.

N.B. This problem is not caused by the PR that added _uv_ephemeral_overlay.pth - the previous implemention using sitecustomize.py had the same problem.


I think the solution may be as simple as cleaning up _uv_ephemeral_overlay.pth before using the chosen environment for uv tool run, but I'm unfamiliar with Rust in general and this codebase in particular, so would be hesitant to open a PR directly.

Platform

macOS 14 arm64 and Windows 11 x86_64

Version

uv 0.5.25 (9c07c3f 2025-01-28)

Python version

Python 3.12.0

@daviesian daviesian added the bug Something isn't working label Jan 30, 2025
@daviesian daviesian changed the title uv run --with ... leaves _uv_ephemeral_overlay.pth in ephemeral environment, which is incorrect for subsequent uses of uv tool run ... uv run --with ... leaves _uv_ephemeral_overlay.pth in ephemeral environment Jan 30, 2025
@zanieb
Copy link
Member

zanieb commented Jan 30, 2025

Interesting! Thanks for the report

@zanieb zanieb added cache Caching of packages and metadata great writeup A wonderful example of a quality contribution 💜 labels Jan 30, 2025
@charliermarsh
Copy link
Member

Hmm... I mean, we can just remove it in uv tool run. But it seems not-completely-safe for us to do so, since another process could simultaneously be starting up the same cached environment, but with the overlay applied. I guess that's already a risk today, though? Like, two commands could simultaneously use the same cached environment, and overwrite one another's uv_ephemeral_overlay.pth?

@charliermarsh
Copy link
Member

I wonder if there's a way for us to provide that .pth file to the interpreter without writing it to the cache. Could we augment the PYTHONPATH to look for it, or something?

@charliermarsh
Copy link
Member

Hmm, no, not seeing anything like that. I guess we should just clean it up in uv tool run and live with it until it becomes an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working cache Caching of packages and metadata great writeup A wonderful example of a quality contribution 💜
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants