astral-sh / rye

a Hassle-Free Python Experience
https://rye.astral.sh
MIT License
13.6k stars 466 forks source link

Rye init script in a folder named `python` breaks rye causing symbolic loop #1235

Closed 0x00cl closed 2 months ago

0x00cl commented 2 months ago

Steps to Reproduce

  1. Install rye with the default values curl -sSf https://rye.astral.sh/get | bash
    
    Welcome to Rye!

This installer will install rye to /home/tomas/.rye

This path can be changed by exporting the RYE_HOME environment variable.

Details: Rye Version: 0.36.0 Platform: linux (x86_64)

✔ Continue? · yes ✔ Select the preferred package installer · uv (fast, recommended) ✔ What should running python or python3 do when you are not inside a Rye managed project? · Run a Python installed and managed by Rye ✔ Which version of Python should be used as default toolchain? · cpython@3.12 Installed binary to /home/tomas/.rye/shims/rye Bootstrapping rye internals Fetching requested internal toolchain 'cpython@3.12.3' Downloading cpython@3.12.3 Checking checksum Unpacking Downloaded cpython@3.12.3 Updated self-python installation at /home/tomas/.rye/self

The rye directory /home/tomas/.rye/shims was not detected on PATH. It is highly recommended that you add it. ✔ Should the installer add Rye to PATH via .profile? · yes Added to PATH.

2. Create a folder for a project `mkdir -p myproject/python && cd myproject/python`
3. `rye init --script`
4. `rye sync`
5. Run `rye add -d ruff` or any other package. Or any other command where rye uses python.

### Expected Result

Some sort of error or at least warning when running `rye init --script` inside a folder named `python`.

At first it wasn't obvious to me that Rye would end up using **my** package named python to execute commands internally and simply break without any error or warning, specially because at no point I executed explicitly any command to install my python in my system **nor told rye to use that version**.

This isn't a problem when simply running `rye init` inside a folder named `python`.

If you are wondering why I'm running `rye init --script` inside a folder named `python`, that is because I was creating a repository with a problem solved in different programming languages and one of them was python.

Maybe add an error when trying to execute inside a folder named `python` and a flag like `-i` or `--ignore` to ignore errors in case someone really knows what they are doing and wants to install their `python` anyway.

### Actual Result

```bash
[~/myproject/python] $ rye add -d ruff
error: Failed to run uv compile DEBUG uv 0.2.22
DEBUG Starting Python discovery for Python 3.12.3
DEBUG Looking for exact match for request Python 3.12.3
DEBUG Searching for Python 3.12.3 in system path
DEBUG Failed to query Python interpreter at `/home/tomas/myproject/python/.venv/bin/python3`
error: Failed to query Python interpreter at `/home/tomas/myproject/python/.venv/bin/python3`
  Caused by: Too many levels of symbolic links (os error 40)
. uv exited with status: exit status: 2

Even on other projects it will stop working unless I reinstall rye

[~/myproject] $ mkdir -p foobar && cd foobar
[~/myproject/foobar] $ rye init
success: Initialized project in /home/tomas/myproject/foobar/.
  Run `rye sync` to get started
[~/myproject/foobar]$ rye sync
Initializing new virtualenv in /home/tomas/myproject/foobar/.venv
Python version: cpython@3.12.3
DEBUG uv 0.2.22
DEBUG Checking for Python interpreter at path `/home/tomas/.rye/py/cpython@3.12.3/bin/python3`
  × Failed to query Python interpreter at `/home/tomas/.rye/py/cpython@3.12.3/bin/python3`
  ╰─▶ Too many levels of symbolic links (os error 40)
error: failed creating virtualenv ahead of sync

Caused by:
    0: failed to initialize virtualenv
    1: Failed to create self venv using /home/tomas/.rye/py/cpython@3.12.3/bin/python3. uv exited with status: exit status: 1

Version Info

rye 0.36.0
commit: 0.36.0 (12c024c7c 2024-07-07)
platform: linux (x86_64)
self-python: cpython@3.12.3
symlink support: true
uv enabled: true

Stacktrace

No response

bluss commented 2 months ago

It reproduces.

Project python and rye's self-python need to be the same version (I think).

The rye sync command wants to install the script python. It follows the existing symlink which goes to Rye's python install, and overwrites the file ~/.rye/py/cpython@3.12.3/bin/python3.12 (!) with a python script snippet.

I think the overwriting qualifies as a bug in uv? @charliermarsh @zanieb

zanieb commented 2 months ago

Should be fixed by https://github.com/astral-sh/uv/pull/5165 in the next uv release. Thank you!