Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ability to load from .env files before invoking the command #7357

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently this will just fail silently if the file is malformed or missing - is this what we want?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO missing shouldn't show an error, because the chance that the file is missing means that someone doesn't want to use .env is high.

However, a malformed file should be indicated to the developer. The chance that they have intentionally created a .env file that is malformed, in order for it not to be loaded by uv, is very low. I would suggest a warning rather than an error, because execution can still continue.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think we should show a user-facing warning if there's an error other than "file does not exist". We could also show a warning if it's "file does not exist" and the user provided an explicit path to a dotenv file, but I'm not certain if we should and it's not blocking.

};

// 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
Loading