Repository: astral-sh/ruff
File: crates/ty_site_packages/src/lib.rs (Rust)

Code before:

/// Read `CONDA_PREFIX` and confirm that it has the expected kind
pub(crate) fn conda_environment_from_env(
    system: &dyn System,
    kind: CondaEnvironmentKind,
) -> Option<SystemPathBuf> {
    let dir = system
        .env_var(EnvVars::CONDA_PREFIX)
        .ok()
        .filter(|value| !value.is_empty())?;
    let path = SystemPathBuf::from(dir);

    if kind != CondaEnvironmentKind::from_prefix_path(system, &path) {
        return None;
    }

    Some(path)
}

#[cfg(not(target_family = "wasm"))]
pub(crate) fn binary_from_path(
    system: &dyn System,
    binary: &str,
    paths: &str,
) -> Option<SystemPathBuf> {
    let binary = which::which_in_global(binary, Some(&paths)).ok()?.next()?;
    let binary = SystemPathBuf::from_path_buf(binary).ok()?;

    // sanity check to filter out shims
    let sys_prefix = SysPrefixPath::new(&binary, SysPrefixPathOrigin::PythonBinary, system).ok()?;
    site_packages_directories_from_sys_prefix(
        &sys_prefix,
        None,
        PythonImplementation::Unknown,
        system,
    )
    .ok()?;

    Some(binary)
}

/// A parser for `pyvenv.cfg` files: metadata files for virtual environments.
///
/// Note that a `pyvenv.cfg` file *looks* like a `.ini` file, but actually isn't valid `.ini` syntax!
///
/// See also: <https://snarky.ca/how-virtual-environments-work/>
#[derive(Debug)]
struct PyvenvCfgParser<'s> {
    source: &'s str,
    cursor: Cursor<'s>,
    line_number: NonZeroUsize,

Review comment (refactor):
Using `which` here still breaks our `System` abstraction, in a couple ways:

1. `System` is also supposed to wrap all filesystem access.
2. `which` will access other env vars internally (e.g. `HOME` for tilde expansion), which could again make our tests accidentally dependent on the system they are run on.

Some options I can see:

1) It looks like `which` also has its own system abstraction that is exposed, so we could probably build a `which::Sys` that wraps a ty `System`?
2) We could not use `which` and just implement the search ourselves. This would at least require implementing tilde expansion ourselves.

I'm not sure which of these is better. I wish @MichaReiser were around to consult on this :) Maybe @BurntSushi has opinions.

I guess we could also add a TODO comment and land this as-is, with `HOME` also added to the set of env vars that are cleared for e2e server tests.

Code after:

        // For other environment names, use the path-based logic
        let Some(name) = path.file_name() else {
            return Self::Child;
        };

        // If the environment is in a directory matching the name of the environment, it's not
        // usually a base environment.
        if name == current_env {
            Self::Child
        } else {
            Self::Base
        }
    }
}

/// Read `CONDA_PREFIX` and confirm that it has the expected kind
pub(crate) fn conda_environment_from_env(
    system: &dyn System,
    kind: CondaEnvironmentKind,
) -> Option<SystemPathBuf> {
    let dir = system
        .env_var(EnvVars::CONDA_PREFIX)
        .ok()
        .filter(|value| !value.is_empty())?;
    let path = SystemPathBuf::from(dir);

    if kind != CondaEnvironmentKind::from_prefix_path(system, &path) {
        return None;
    }

    Some(path)
}

#[cfg(target_family = "wasm")]
pub(crate) fn environment_from_binary(
    _system: &dyn System,
    _binary: &str,
) -> Option<PythonEnvironment> {
    None
}

#[cfg(not(target_family = "wasm"))]
pub(crate) fn environment_from_binary(
    system: &dyn System,
    binary: &str,
) -> Option<PythonEnvironment> {
    let binary = which::WhichConfig::new_with_sys(system)
        .binary_name(binary.into())
        .first_result()
        .ok()?;