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

fix: load env-file at start of flag parsing #28127

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

devsnek
Copy link
Member

@devsnek devsnek commented Feb 15, 2025

env-file values need to be available before flag parsing runs, since flag parsing uses env vars in some places.

Fixes: #27851

@@ -5727,6 +5750,8 @@ fn env_file_arg_parse(flags: &mut Flags, matches: &mut ArgMatches) {
flags.env_file = matches
.get_many::<String>("env-file")
.map(|values| values.cloned().collect());

load_env_variables_from_env_file(flags.env_file.as_ref());
Copy link
Member

Choose a reason for hiding this comment

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

I'm not 100% sure about that - this change means that any time we need to construct flags again (for whatever reason) we are going to implicitly load the .env file again. @dsherret wdyt about that?

Copy link
Member Author

Choose a reason for hiding this comment

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

well, we read env vars in flag parsing code. so this is kind of unavoidable.

Copy link
Member

Choose a reason for hiding this comment

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

We should probably separate env var loading from flag parsing. Probably it should be:

  1. Flag parsing
  2. Load env file
  3. Resolve the value with the env vars in CliOptions

Copy link
Member Author

Choose a reason for hiding this comment

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

but flag parsing depends on environment variables. another example is DENO_JOBS.

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 separate it so that's not the case.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't really understand what you have in mind.

Copy link
Member

Choose a reason for hiding this comment

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

I think we should leave clap for parsing cli args (not env vars) Three stages: arg parsing with clap, env file loading based on args, then at this point check the value of the env vars (could be backfilled into the flags struct or resolved in CliOptions). Some properties on the flags structs would need to be None to indicate no argument was provided instead of defaulting to a value. I can show an example tomorrow if that would help.

@@ -1398,6 +1399,28 @@ pub fn flags_from_vec(args: Vec<OsString>) -> clap::error::Result<Flags> {
Ok(flags)
}

// use eprintln instead of log::warn because logging hasn't been initialized yet
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't take into account --log-level=none/error anymore unfortunately (we should fix this before landing).

Copy link
Member Author

Choose a reason for hiding this comment

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

i agree it is unfortunate but this isn't the only case of this in this file.

@@ -5727,6 +5750,8 @@ fn env_file_arg_parse(flags: &mut Flags, matches: &mut ArgMatches) {
flags.env_file = matches
.get_many::<String>("env-file")
.map(|values| values.cloned().collect());

load_env_variables_from_env_file(flags.env_file.as_ref());
Copy link
Member

Choose a reason for hiding this comment

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

We should probably separate env var loading from flag parsing. Probably it should be:

  1. Flag parsing
  2. Load env file
  3. Resolve the value with the env vars in CliOptions

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.

OpenTelemetry setup does not work with --env-file
3 participants