astral-sh / rye

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

Parsing of ".python-version" file does not respect comments #1032

Closed pbrkr closed 6 months ago

pbrkr commented 7 months ago

Steps to Reproduce

  1. Run rye init

  2. Modify .python-version to start with a comment, e.g.

    # SPDX-License-Identifier: CC0-1.0
    3.12.2
  3. Run python3 --version

Expected Result

❯ python3 --version                  
Python 3.12.2

Actual Result

❯ python3 --version
Python version mismatch (found cpython@3.12.2, expected cpython@3.8.18), recreating.
Downloading cpython@3.8.18
Checking checksum
Unpacking
Downloaded cpython@3.8.18
Initializing new virtualenv in /home/pbarker/Projects/t/.venv
Python version: cpython@3.8.18
Generating production lockfile: /home/pbarker/Projects/t/requirements.lock
Generating dev lockfile: /home/pbarker/Projects/t/requirements-dev.lock
Installing dependencies
   Built file:///home/pbarker/Projects/t                                                                                                                                                                                         Built 1 editable in 104ms
Installed 1 package in 0.16ms
 + t==0.1.0 (from file:///home/pbarker/Projects/t)
Python 3.8.18

Version Info

❯ rye --version
rye 0.32.0
commit: 0.32.0 (e1b4f2a29 2024-03-29)
platform: linux (x86_64)
self-python: cpython@3.12.2
symlink support: true
uv enabled: true

Stacktrace

No response

pbrkr commented 7 months ago

Note that pyenv supports comments in this .python-version: https://github.com/pyenv/pyenv/blob/v2.4.0/libexec/pyenv-version-file-read#L30

  while read -n 1024 -r version _ || [[ $version ]]; do
    if [[ -z "$version" || "$version" == \#* ]]; then
      # Skip empty lines and comments
      continue
    ...
pbrkr commented 6 months ago

@charliermarsh Many thanks for the quick fix here!

charliermarsh commented 6 months ago

No prob!