Skip to content

Commit

Permalink
chore: unify all env vars used (#8151)
Browse files Browse the repository at this point in the history
## Summary

This PR declares and documents all environment variables that are used
in one way or another in `uv`, either internally, or externally, or
transitively under a common struct.

I think over time as uv has grown there's been many environment
variables introduced. Its harder to know which ones exists, which ones
are missing, what they're used for, or where are they used across the
code. The docs only documents a handful of them, for others you'd have
to dive into the code and inspect across crates to know which crates
they're used on or where they're relevant.

This PR is a starting attempt to unify them, make it easier to discover
which ones we have, and maybe unlock future posibilities in automating
generating documentation for them.

I think we can split out into multiple structs later to better organize,
but given the high influx of PR's and possibly new environment variables
introduced/re-used, it would be hard to try to organize them all now
into their proper namespaced struct while this is all happening given
merge conflicts and/or keeping up to date.

I don't think this has any impact on performance as they all should
still be inlined, although it may affect local build times on changes to
the environment vars as more crates would likely need a rebuild. Lastly,
some of them are declared but not used in the code, for example those in
`build.rs`. I left them declared because I still think it's useful to at
least have a reference.

Did I miss any? Are their initial docs cohesive?

Note, `uv-static` is a terrible name for a new crate, thoughts? Others
considered `uv-vars`, `uv-consts`.

## Test Plan

Existing tests
  • Loading branch information
samypr100 authored Oct 14, 2024
1 parent 0a23be4 commit 01c44af
Show file tree
Hide file tree
Showing 86 changed files with 1,572 additions and 1,104 deletions.
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ uv-scripts = { path = "crates/uv-scripts" }
uv-settings = { path = "crates/uv-settings" }
uv-shell = { path = "crates/uv-shell" }
uv-state = { path = "crates/uv-state" }
uv-static = { path = "crates/uv-static" }
uv-tool = { path = "crates/uv-tool" }
uv-types = { path = "crates/uv-types" }
uv-version = { path = "crates/uv-version" }
Expand Down
1 change: 1 addition & 0 deletions crates/uv-build-frontend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ uv-pep440 = { workspace = true }
uv-pep508 = { workspace = true }
uv-pypi-types = { workspace = true }
uv-python = { workspace = true }
uv-static = { workspace = true }
uv-types = { workspace = true }
uv-virtualenv = { workspace = true }

Expand Down
13 changes: 7 additions & 6 deletions crates/uv-build-frontend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use uv_pep440::Version;
use uv_pep508::PackageName;
use uv_pypi_types::{Requirement, VerbatimParsedUrl};
use uv_python::{Interpreter, PythonEnvironment};
use uv_static::EnvVars;
use uv_types::{BuildContext, BuildIsolation, SourceBuildTrait};

/// The default backend to use when PEP 517 is used without a `build-system` section.
Expand Down Expand Up @@ -318,10 +319,10 @@ impl SourceBuild {

// Figure out what the modified path should be, and remove the PATH variable from the
// environment variables if it's there.
let user_path = environment_variables.remove(&OsString::from("PATH"));
let user_path = environment_variables.remove(&OsString::from(EnvVars::PATH));

// See if there is an OS PATH variable.
let os_path = env::var_os("PATH");
let os_path = env::var_os(EnvVars::PATH);

// Prepend the user supplied PATH to the existing OS PATH
let modified_path = if let Some(user_path) = user_path {
Expand Down Expand Up @@ -944,10 +945,10 @@ impl PythonRunner {
.args(["-c", script])
.current_dir(source_tree.simplified())
.envs(environment_variables)
.env("PATH", modified_path)
.env("VIRTUAL_ENV", venv.root())
.env("CLICOLOR_FORCE", "1")
.env("PYTHONIOENCODING", "utf-8:backslashreplace")
.env(EnvVars::PATH, modified_path)
.env(EnvVars::VIRTUAL_ENV, venv.root())
.env(EnvVars::CLICOLOR_FORCE, "1")
.env(EnvVars::PYTHONIOENCODING, "utf-8:backslashreplace")
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()
Expand Down
1 change: 1 addition & 0 deletions crates/uv-cache/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ uv-distribution-types = { workspace = true }
uv-fs = { workspace = true, features = ["tokio"] }
uv-normalize = { workspace = true }
uv-pypi-types = { workspace = true }
uv-static = { workspace = true }

clap = { workspace = true, features = ["derive", "env"], optional = true }
directories = { workspace = true }
Expand Down
5 changes: 3 additions & 2 deletions crates/uv-cache/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::io;
use std::path::{Path, PathBuf};
use uv_static::EnvVars;

use crate::Cache;
use clap::Parser;
Expand All @@ -17,7 +18,7 @@ pub struct CacheArgs {
long,
short,
alias = "no-cache-dir",
env = "UV_NO_CACHE",
env = EnvVars::UV_NO_CACHE,
value_parser = clap::builder::BoolishValueParser::new(),
)]
pub no_cache: bool,
Expand All @@ -26,7 +27,7 @@ pub struct CacheArgs {
///
/// Defaults to `$HOME/Library/Caches/uv` on macOS, `$XDG_CACHE_HOME/uv` or `$HOME/.cache/uv` on
/// Linux, and `%LOCALAPPDATA%\uv\cache` on Windows.
#[arg(global = true, long, env = "UV_CACHE_DIR")]
#[arg(global = true, long, env = EnvVars::UV_CACHE_DIR)]
pub cache_dir: Option<PathBuf>,
}

Expand Down
1 change: 1 addition & 0 deletions crates/uv-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ uv-pypi-types = { workspace = true }
uv-python = { workspace = true, features = ["clap", "schemars"]}
uv-resolver = { workspace = true, features = ["clap"] }
uv-settings = { workspace = true, features = ["schemars"] }
uv-static = { workspace = true }
uv-version = { workspace = true }
uv-warnings = { workspace = true }

Expand Down
Loading

0 comments on commit 01c44af

Please sign in to comment.