Skip to content

Commit

Permalink
runner: Don't pass automatic hostenv if --env or --envdir are used
Browse files Browse the repository at this point in the history
These new options are a good place to attach this related backwards
incompatible change, since their usage acts as an opt-in.  Eliminating
automatic forwarding also makes it clearer what's getting passed when
combined with explicit forwarding.

We can (and should) still fully remove automatic hostenv in the future.
  • Loading branch information
tsibley committed Jun 20, 2023
1 parent b56f654 commit fa2633d
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions nextstrain/cli/runner/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ def register_arguments(parser: ArgumentParser, runners: List[RunnerModule], exec
metavar = "<name>[=<value>]",
help = "Set the environment variable <name> to the value in the current environment (i.e. pass it thru) or to the given <value>. "
"May be specified more than once. "
"Overrides any variables of the same name set via --envdir."
"Overrides any variables of the same name set via --envdir. "
"When this option or --envdir is given, the default behaviour of automatically passing thru several \"well-known\" variables is disabled. "
f"The \"well-known\" variables are: {' '.join(hostenv.forwarded_names)}. "
"Pass those variables explicitly via --env or --envdir if you need them in combination with other variables. "
f"{SKIP_AUTO_DEFAULT_IN_HELP}",
action = "append",
default = [])
Expand All @@ -168,6 +171,8 @@ def register_arguments(parser: ArgumentParser, runners: List[RunnerModule], exec
"An envdir is a directory containing files describing environment variables. "
"Each filename is used as the variable name. "
"The first line of the contents of each file is used as the variable value. "
"When this option or --env is given, the default behaviour of automatically passing thru several \"well-known\" variables is disabled. "
"See the description of --env for more details. "
f"{SKIP_AUTO_DEFAULT_IN_HELP}",
type = DirectoryPath,
action = "append",
Expand Down Expand Up @@ -258,15 +263,18 @@ def run(opts: Options, working_volume: NamedVolume = None, extra_env: Env = {},
if opts.__runner__ is singularity and opts.image is docker.DEFAULT_IMAGE: # type: ignore
opts.image = singularity.DEFAULT_IMAGE # type: ignore

# Add env from automatically forwarded vars, from --envdir, and from --env
# Add env from automatically forwarded vars xor from --envdir and --env
# without overriding values explicitly set by our commands' own internals
# (i.e. the callers of this function).
extra_env = {
**dict(hostenv.forwarded_values()),
**dict(env.from_dirs(opts.envdir)),
**dict(env.from_vars(opts.env)),
**extra_env,
}
if opts.envdir or opts.env:
extra_env = {
**dict(env.from_dirs(opts.envdir)),
**dict(env.from_vars(opts.env)),
**extra_env }
else:
extra_env = {
**dict(hostenv.forwarded_values()),
**extra_env }

return opts.__runner__.run(opts, argv, working_volume = working_volume, extra_env = extra_env, cpus = cpus, memory = memory)

Expand Down

0 comments on commit fa2633d

Please sign in to comment.