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

Conversation

haydn-j-evans
Copy link

@haydn-j-evans haydn-j-evans commented Sep 13, 2024

Summary

Pipenv and other equivalent tools have the ability to load an .env file during execution of a script etc

https://pipenv.pypa.io/en/stable/shell.html#automatic-loading-of-env

The PR adds a new flag --load-dotenv and a new ENV UV_RUN_LOAD_DOTENV that enables this feature for uv run

resolves #1384

Test Plan

Have tested it locally by successfully invoking scripts that require ENV's to be set.

@haydn-j-evans haydn-j-evans force-pushed the feature/1384/support-dotens-in-run-command branch from f13a8eb to 6279982 Compare September 13, 2024 11:25
@eth3lbert
Copy link
Contributor

dotenv is implicitly unmaintained.
Ref: rustsec/advisory-db#1254

@haydn-j-evans
Copy link
Author

@eth3lbert I will switch it to dotenvy if you are happy with the idea of the PR :)

@haydn-j-evans haydn-j-evans force-pushed the feature/1384/support-dotens-in-run-command branch 3 times, most recently from 36af9a3 to 7818e55 Compare September 15, 2024 20:03
@zanieb zanieb self-assigned this Sep 15, 2024
@haydn-j-evans haydn-j-evans force-pushed the feature/1384/support-dotens-in-run-command branch 3 times, most recently from 3bf5307 to 3c49806 Compare September 20, 2024 09:47
@theintz
Copy link

theintz commented Sep 26, 2024

Can't wait for this to land, we're excited to switch to uv, but are currently blocked by this.

@zanieb zanieb assigned charliermarsh and unassigned zanieb Oct 1, 2024
@haydn-j-evans haydn-j-evans force-pushed the feature/1384/support-dotens-in-run-command branch from a35c096 to 2cdbd23 Compare October 9, 2024 12:00
@haydn-j-evans
Copy link
Author

@charliermarsh This PR is currently configured to be opt in, but in light of the discussion in the issue thread, should we instead make it opt out?

i.e.

--no-load-dotenv

UV_RUN_NO_LOAD_DOTENV

And also the naming can be discussed, I kept it scoped to UV_RUN_ as this is a run specific variable, but perhaps if there is a need for full uv configuration with a .env, we just call it UV_NO_LOAD_DOTENV and then just expand its use within uv later.

@PhilipVinc
Copy link

Could this be used to set UV_PROJECT_ENVIRONMENT in the .env file, offering a slightly better workaround to the issues discussed in #7642 and #1495 ?

@haydn-j-evans
Copy link
Author

@PhilipVinc currently it only affects the run command, it would have to be further expanded to support uv global config.

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

@charliermarsh
Copy link
Member

Yeah I think my preference would be:

  • Read .env by default (only in uv run for now; we may expand it later).
  • Allow users to pass --env-file (or UV_ENV_FILE) to override the file.
  • Add --no-env-file and UV_NO_ENV_FILE to disable it.

I think we can omit the UV_RUN_ prefixes.

@haydn-j-evans
Copy link
Author

closing this as it seems to be implemented already

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support .env-files
7 participants