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 Oct 9, 2024
1 parent 5bc726a commit 2cdbd23
Show file tree
Hide file tree
Showing 9 changed files with 37 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 @@ -92,6 +92,7 @@ directories = { version = "5.0.1" }
dirs-sys = { version = "0.4.1" }
dunce = { version = "1.0.5" }
either = { version = "1.13.0" }
dotenvy = { version = "0.15.7" }
encoding_rs_io = { version = "0.1.7" }
etcetera = { version = "0.8.0" }
flate2 = { version = "1.0.33", default-features = false }
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 @@ -2660,6 +2660,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 @@ -59,6 +59,7 @@ axoupdater = { workspace = true, features = [
], optional = true }
clap = { workspace = true, features = ["derive", "string", "wrap_help"] }
ctrlc = { workspace = true }
dotenvy = { workspace = true }
flate2 = { workspace = true, default-features = false }
fs-err = { workspace = true, features = ["tokio"] }
futures = { workspace = true }
Expand Down
10 changes: 10 additions & 0 deletions crates/uv/src/commands/project/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ use crate::commands::reporters::PythonDownloadReporter;
use crate::commands::{diagnostics, project, ExitStatus, SharedState};
use crate::printer::Printer;
use crate::settings::ResolverInstallerSettings;
use dotenvy::dotenv_override;

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

// Load the `.env` into the environment if the flag or ENV is set.
// This is done before setting the PATH or VIRTUAL_ENV environment variables
// to ensure they are not overwritten
if load_dotenv {
debug!("Loading `.env` file");
dotenv_override().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 @@ -1257,6 +1257,7 @@ async fn run_project(
args.editable,
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 @@ -236,6 +236,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 @@ -267,6 +268,7 @@ impl RunSettings {
no_project,
python,
show_resolution,
load_dotenv,
} = args;

Self {
Expand Down Expand Up @@ -295,6 +297,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 @@ -75,6 +75,9 @@ uv accepts the following command-line arguments as environment variables:
set, uv will use this password for publishing.
- `UV_NO_SYNC`: Equivalent to the `--no-sync` command-line argument. If set, uv will skip updating
the environment.
- `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
5 changes: 5 additions & 0 deletions docs/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ uv run [OPTIONS] [COMMAND]

<li><code>symlink</code>: Symbolically link packages from the wheel into the <code>site-packages</code> directory</li>
</ul>
</dd><dt><code>--load-dotenv</code></dt><dd><p>Run the command and load environment variables from the <code>.env</code> file in the current project.</p>

<p>By default, the .env file is not loaded.</p>

<p>May also be set with the <code>UV_RUN_LOAD_DOTENV</code> environment variable.</p>
</dd><dt><code>--locked</code></dt><dd><p>Assert that the <code>uv.lock</code> will remain unchanged.</p>

<p>Requires that the lockfile is up-to-date. If the lockfile is missing or needs to be updated, uv will exit with an error.</p>
Expand Down

0 comments on commit 2cdbd23

Please sign in to comment.