astral-sh / rye

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

`rye build` should use python version from `.python-version` #1248

Closed b0o closed 2 months ago

b0o commented 2 months ago

Steps to Reproduce

Running rye build uses whatever version of python is configured for rye's global installation.

Expected Result

Rye should use the python version specified in the .python-version file.

Actual Result

$ rye --version
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

$ cat .python-version
3.11

$ python  --version
Python 3.11.9

$ rye build --wheel -v
building myproject-py
* Creating isolated environment: venv+uv...
* Using external uv from /home/myname/.rye/uv/0.2.22/uv
* Installing packages in isolated environment:
  - hatchling
> /home/myname/.rye/uv/0.2.22/uv pip install hatchling
< Resolved 5 packages in 65ms
< Installed 5 packages in 4ms
<  + hatchling==1.25.0
<  + packaging==24.1
<  + pathspec==0.12.1
<  + pluggy==1.5.0
<  + trove-classifiers==2024.7.2
* Getting build dependencies for wheel...
* Installing packages in isolated environment:
  - hatch-mypyc
> /home/myname/.rye/uv/0.2.22/uv pip install hatch-mypyc
< Resolved 10 packages in 69ms
< Installed 5 packages in 66ms
<  + hatch-mypyc==0.16.0
<  + mypy==1.11.0
<  + mypy-extensions==1.0.0
<  + setuptools==71.0.4
<  + typing-extensions==4.12.2
* Building wheel...
Successfully built myproject_py-0.0.1-cp312-cp312-linux_x86_64.whl

Note the cp312 tag instead of cp311.

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

Other Info

I tested the following patch, which "fixes" the issue, but requires adding build as a dev-dependency.

diff --git a/rye/src/cli/build.rs b/rye/src/cli/build.rs
index 324f76b..d98657d 100644
--- a/rye/src/cli/build.rs
+++ b/rye/src/cli/build.rs
@@ -83,7 +83,7 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
             style(project.normalized_name()?).cyan()
         );

-        let mut build_cmd = Command::new(get_venv_python_bin(&self_venv));
+        let mut build_cmd = Command::new(get_venv_python_bin(&project.venv_path()));
         build_cmd
             .arg("-mbuild")
             .env("NO_COLOR", "1")

Related: #1152

charliermarsh commented 2 months ago

Yeah this looks off. I'll take a look.

charliermarsh commented 2 months ago

Yeah this is an interesting problem. We depend on build so we can't use the user's environment.

charliermarsh commented 2 months ago

I think we need to create an ephemeral virtualenv with uv.

charliermarsh commented 2 months ago

I believe I have a fix here: https://github.com/astral-sh/rye/pull/1250