Skip to content

Commit

Permalink
add ability to load from .env files before invoking the command
Browse files Browse the repository at this point in the history
  • Loading branch information
Haydn Evans committed Sep 13, 2024
1 parent b896657 commit f13a8eb
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 0 deletions.
7 changes: 7 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 @@ -83,6 +83,7 @@ data-encoding = { version = "2.5.0" }
directories = { version = "5.0.1" }
dirs-sys = { version = "0.4.1" }
dunce = { version = "1.0.4" }
dotenv = { version = "0.15.0" }
either = { version = "1.12.0" }
encoding_rs_io = { version = "0.1.7" }
etcetera = { version = "0.8.0" }
Expand Down
6 changes: 6 additions & 0 deletions crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2509,6 +2509,12 @@ pub struct RunArgs {
/// By default, environment modifications are omitted, but enabled under `--verbose`.
#[arg(long, env = "UV_SHOW_RESOLUTION", value_parser = clap::builder::BoolishValueParser::new(), hide = true)]
pub show_resolution: bool,

/// Run the command and load environment variables from the `.env` file in the current project.
///
/// By default, the .env file is not loaded.
#[arg(long, env = "UV_RUN_LOAD_DOTENV")]
pub load_dotenv: bool,
}

#[derive(Args)]
Expand Down
1 change: 1 addition & 0 deletions crates/uv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ unicode-width = { workspace = true }
url = { workspace = true }
which = { workspace = true }
zip = { workspace = true }
dotenv = { workspace = true }

[target.'cfg(target_os = "windows")'.dependencies]
mimalloc = { version = "0.1.39" }
Expand Down
7 changes: 7 additions & 0 deletions crates/uv/src/commands/project/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use crate::commands::reporters::PythonDownloadReporter;
use crate::commands::{project, ExitStatus, SharedState};
use crate::printer::Printer;
use crate::settings::ResolverInstallerSettings;
use dotenv::dotenv;

/// Run a command.
#[allow(clippy::fn_params_excessive_bools)]
Expand All @@ -60,6 +61,7 @@ pub(crate) async fn run(
dev: bool,
python: Option<String>,
settings: ResolverInstallerSettings,
load_dotenv: bool,
python_preference: PythonPreference,
python_downloads: PythonDownloads,
connectivity: Connectivity,
Expand Down Expand Up @@ -686,6 +688,11 @@ pub(crate) async fn run(
debug!("Running `{command}`");
let mut process = command.as_command(interpreter);

// Load the `.env` if necessary.
if load_dotenv {
dotenv().ok();
};

// Construct the `PATH` environment variable.
let new_path = std::env::join_paths(
ephemeral_env
Expand Down
1 change: 1 addition & 0 deletions crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,7 @@ async fn run_project(
args.dev,
args.python,
args.settings,
args.load_dotenv,
globals.python_preference,
globals.python_downloads,
globals.connectivity,
Expand Down
3 changes: 3 additions & 0 deletions crates/uv/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ pub(crate) struct RunSettings {
pub(crate) python: Option<String>,
pub(crate) refresh: Refresh,
pub(crate) settings: ResolverInstallerSettings,
pub(crate) load_dotenv: bool,
}

impl RunSettings {
Expand Down Expand Up @@ -248,6 +249,7 @@ impl RunSettings {
no_project,
python,
show_resolution,
load_dotenv,
} = args;

Self {
Expand Down Expand Up @@ -275,6 +277,7 @@ impl RunSettings {
resolver_installer_options(installer, build),
filesystem,
),
load_dotenv,
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions docs/configuration/environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ uv accepts the following command-line arguments as environment variables:
`--no-python-downloads` option. Whether uv should allow Python downloads.
- `UV_COMPILE_BYTECODE`: Equivalent to the `--compile-bytecode` command-line argument. If set, uv
will compile Python source files to bytecode after installation.
- `UV_RUN_LOAD_DOTENV`: Equivalent to the `--load-dotenv` command-line argument. If set, `uv run`
will load the .env file from the current project directory (if it exists) to load all ENVs into the current
process.

In each case, the corresponding command-line argument takes precedence over an environment variable.

Expand Down

0 comments on commit f13a8eb

Please sign in to comment.