fix: Added support for --env for the compile subcommand #24166
+26
−13
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Supported the use of --env flag with the compile subcommand, so that the generated executable/binary file can access the passed env file.
The commit is made to address issue #22105 .
Use-case
deno compile --env=some_env_file file_using_env_vars.js
-> executable/binary file "file_using_env_vars" is created.file_using_env_vars
to run it.The problem that prevented the generated executable from finding the env file's environment variables
CliOptions::new
and so is the env file passed.std::env::set_var
.std::env::set_var
sets the env variables exclusively for the currently running process, and thus the env variables loaded within compile time are exclusive for the operation itself "The compile operation".Solution of the problem
Pass the environment file to the executable/binary file, then load the environment variables whenever the file is ran.
Code changes
A) Added an attribute to hold the environment file name to Metadata struct within deno/cli/standalone/binary.rs, since the metadata is passed to the binary file by
fn write_standalone_binary()
.B) Added a getter for the environment file name to the CliOptions methods, since the Flags attribute that holds env_file attribute is private.
C) Added
fn load_env_variables_from_env_file()
to deno/cli/mainrt.rs which will be responsible for loading the env variables from the environment file at execution time.@dsherret
@littledivy