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 script install into .venv overwrites python #5152

Closed
bluss opened this issue Jul 17, 2024 · 5 comments · Fixed by #5165
Closed

Uv script install into .venv overwrites python #5152

bluss opened this issue Jul 17, 2024 · 5 comments · Fixed by #5165
Assignees
Labels
bug Something isn't working

Comments

@bluss
Copy link
Contributor

bluss commented Jul 17, 2024

  • Install python 3.12.3 with uv.
  • Create a new package (pyproject below)
  • uv venv -p 3.12.3
  • Uv creates a virtualenv that has symlink .venv/bin/python -> ~/.local/share/uv/python/cpython-3.12.3-linux-x86_64-gnu/bin/python3
  • Current package is named python. It has a script called python
  • uv sync -v
  • Actual Result: When installing the script to .venv/bin/python it overwrites the target of the symlink. This ruins the installed python version.
  • Expected Result: Don't follow the symlink. Probably throw an error somewhere before.

Based on astral-sh/rye#1235. I think this is could be a bug in Uv?

Tested and reproduced with uv only (then uv 0.2.24, not the latest, I'm sorry).

uv 0.2.24
platform: linux (x86_64)

[project]
name = "python"
version = "0.1.0"
description = "Add your description here"
dependencies = []
requires-python = ">= 3.12"

[project.scripts]
"python" = "python:main"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.metadata]
allow-direct-references = true

[tool.hatch.build.targets.wheel]
packages = ["src/python"]
# src/python/__init__.py
def main() -> int:
    print("Hello from python!")
    return 0
# src/python/__main__.py
import python
import sys

sys.exit(python.main())
@zanieb zanieb added the bug Something isn't working label Jul 17, 2024
@charliermarsh
Copy link
Member

Lol 🤦

@zanieb
Copy link
Member

zanieb commented Jul 17, 2024

Thanks for reproducing over here!

@charliermarsh
Copy link
Member

I wonder what pip does.

@bluss
Copy link
Contributor Author

bluss commented Jul 17, 2024

I tested pip 24.0 now in the same project and it seems like it overwrites the symlink, not the symlink target. so .venv/bin/python is overwritten.

@charliermarsh
Copy link
Member

Thank you.

@charliermarsh charliermarsh self-assigned this Jul 17, 2024
charliermarsh added a commit that referenced this issue Jul 17, 2024
## Summary

It turns out that if `path` is a symlink,
`File::create(path)?.write_all(content.as_ref())?` will overwrite the
_target_ file. That means an entrypoint named `python` would actually
overwrite the user's source Python executable, which is symlinked into
the virtual environment.

This PR replaces that code with our atomic write method.

Closes #5152.

## Test Plan

I ran through the test plan
`https://github.com/astral-sh/uv/issues/5152`, but used an executable
named `bar` linked to `foo.txt` instead...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants