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: Added support for --env for the compile subcommand #24166

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

HasanAlrimawi
Copy link

@HasanAlrimawi HasanAlrimawi commented Jun 10, 2024

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

  • Through the compile subcommand flow, CliOptions are made by CliOptions::new and so is the env file passed.
  • Within the same function, environment variables found in the env file were loaded using the dotenvy crate which uses the std::env::set_var.
  • The 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".
  • When you try to run the generated executable file, then a new process will take the responsibility, and this process doesn't have the environment variables loaded.

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

cli/mainrt.rs Outdated
}
}
}
}
Copy link
Member

Choose a reason for hiding this comment

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

The code for this already exists elsewhere. Maybe it should be extracted out to a common function and reused here?

Copy link
Author

Choose a reason for hiding this comment

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

Hello @dsherret ,
Code has been wrapped within function to reuse.
Checks can be triggered.

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.

None yet

2 participants