Repository: mitsuhiko/rye
File: rye/src/pyproject.rs (Rust)

Code before:
                        x.operator(),
                        Operator::Equal
                            | Operator::EqualStar
                            | Operator::GreaterThanEqual
                            | Operator::GreaterThan
                    )
                })
                .map(|x| {
                    let mut rv = PythonVersionRequest::from(x.version().clone());
                    // this is pretty shitty, but probably good enough
                    if matches!(x.operator(), Operator::GreaterThan) {
                        if let Some(ref mut patch) = rv.patch {
                            *patch += 1;
                        } else if let Some(ref mut minor) = rv.minor {
                            *minor += 1;
                        }
                    }
                    rv
                })
                .min()
        })
}

pub fn find_project_root_from(path: PathBuf) -> Option<PathBuf> {
    let mut here = if path.is_relative() {
        path.canonicalize().ok()?
    } else {
        path
    };

    loop {
        let project_file = here.join("pyproject.toml");
        if project_file.is_file() {
            return Some(here.to_path_buf());
        }

        if !here.pop() {
            break;
        }
    }

    None
}

fn is_unsafe_script(path: &Path) -> bool {
    #[cfg(windows)]
    {
        let stem = path.file_stem();
        stem == Some(OsStr::new("activate")) || stem == Some(OsStr::new("deactivate"))
    }
    #[cfg(unix)]

Review comment (suggestion):
We don't really like canonicalize and would like to use the unstable std function for absolute path. Well, I'm not sure if symlink resolution and walking the filesystem is good or bad here; symlink resolution definitely changes what it will do when it ascends parents, and maybe resolved is the right way to do it..

Code after:
    let marker_file = venv_path.join("rye-venv.json");
    let contents = fs::read(marker_file).ok()?;
    let marker: VenvMarker = serde_json::from_slice(&contents).ok()?;
    Some(marker.python)
}

/// Give a given python version request, returns the latest available version.
///
/// This can return a version that requires downloading.
pub fn latest_available_python_version(
    requested_version: &PythonVersionRequest,
) -> Option<PythonVersion> {
    let mut all = if let Ok(available) = list_known_toolchains() {
        available
            .into_iter()
            .filter_map(|(ver, _)| {
                if matches_version(requested_version, &ver) {
                    Some(ver)
                } else {
                    None
                }
            })
            .collect()
    } else {
        Vec::new()
    };

    if let Some((latest, _, _)) = get_download_url(requested_version, OS, ARCH) {
        all.push(latest);
    };

    all.sort();
    all.into_iter().rev().next()
}

fn resolve_target_python_version(
    doc: &Document,
    root: &Path,
    venv_path: &Path,
) -> Option<PythonVersionRequest> {
    resolve_lower_bound_python_version(doc)
        .or_else(|| get_current_venv_python_version(venv_path).map(Into::into))
        .or_else(|| get_python_version_request_from_pyenv_pin(root).map(Into::into))
        .or_else(|| Config::current().default_toolchain().ok())
}

fn resolve_intended_venv_python_version(
    doc: &Document,
    root: &Path,
) -> Result<PythonVersion, Error> {
    let requested_version = get_python_version_request_from_pyenv_pin(root)