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

The .env injection for the run command need a fix #1285

Closed
jefer94 opened this issue Jul 29, 2022 · 10 comments
Closed

The .env injection for the run command need a fix #1285

jefer94 opened this issue Jul 29, 2022 · 10 comments
Labels
🐛 bug Something isn't working

Comments

@jefer94
Copy link

jefer94 commented Jul 29, 2022

Files to reproduce

# shell
export DATABASE_URL=5 
pdm run python a.py
# .env
DATABASE_URL=postgres://user:pass@localhost:5432/database
# pyproject.toml
[tool.pdm.scripts]
_.env_file = ".env"
# a.py
import os

print('print', os.environ['DATABASE_URL'])

Current behavior

5

Expected behavior

postgres://user:pass@localhost:5432/database

PR #1284

@jefer94 jefer94 added the 🐛 bug Something isn't working label Jul 29, 2022
@frostming
Copy link
Collaborator

Which is the correct behavior depends on how you think about it.

@pgjones has a different opinion on this.

@jefer94
Copy link
Author

jefer94 commented Jul 29, 2022

The standard generally is, load the .env if it exists, and I you are in production, get the environment thought of cloud provider I think, maybe aproach be add a as it

[tool.pdm.scripts.env] # or [tool.pdm.env] 
env_file = ".env"
prefer = "dotenv" # or "system"

The configuration section should be [tool.pdm.scripts.env] or [tool.pdm.env], the second way allow use the same environment to pdm run and pdm shell

Also, pipenv overwrite the existing environment variable with the value of .env

The new aproach question to the user what prefer, and keep the behavior of pipenv to make more easier the migration from it, also, the consecuense of this way ends with if you install pdm in a project was pipenv the setting per default should be set to env_file = ".env" and prefer = "dotenv", else ask to the user what prefer

What do you think? 🤔, the last way to set the env_file should keep their compatibility per a time

@jefer94
Copy link
Author

jefer94 commented Jul 29, 2022

Also if a developer use pdm with gitpod the DATABASE_URL generally is set, if that developer want to work with a custom environment variable, coping the content of .env.example, the way to load their custom variable is more harder than prefer the value of .env if it exists

@pgjones
Copy link
Contributor

pgjones commented Aug 2, 2022

dotenv does provide an override open (defaulting to False) to allow this to be customized. The same could be used here as a solution to both assumptions.

I still believe an env file should not override an environment variable by default, see also "By default, load_dotenv doesn't override existing environment variables."

@jefer94
Copy link
Author

jefer94 commented Aug 3, 2022

Which should be the approach related to set override in the config file? I think also this depends if the command is being executed in development, ci or production in each environment is more convenient to have one preference or other, daphne is prefer run without override, python manage.py runserver is useful in the development environment and is used by junior developers that don't understand how the project works and is prefer setup it with override

Maybe a good option should be considering this is implemented test = {cmd = "pytest", env_file=".env"} implement a

# The way 1, choice the environment using the same style than `test = {cmd = "pytest", env_file=".env"}`
[tool.pdm.scripts]
test1 = {cmd = "pytest", environment="prod"}
test2 = {cmd = "pytest", environment="dev"}

[tool.pdm.environments]
prod = {override=true, env_file=".env"}
dev = {override=false, env_file=".env"}
# The way 2, read first the environments and infer the configuration
[tool.pdm.scripts.prod]
test1 = "pytest"

[tool.pdm.scripts.dev]
test2 = "pytest"

[tool.pdm.environments]
prod = {override="true", env_file=".env"}
dev = {override="false", env_file=".env"}

With this approach PDM should work with multiple environment files, what you think about it? 🤔

@pgjones
Copy link
Contributor

pgjones commented Aug 3, 2022

I think it is unnecessary for multiple environment files and that by definition the variables in the environment that the code runs in should take precedence - to me this is the point of environment variables. In other words the environment variables exist in the environment the code runs in and thereby configure the code.

I would recommend,

[tool.pdm.scripts]
script_cmd = {cmd = "...", env_file="...", override = false}

as a compromise with the default staying as it is now.

@frostming
Copy link
Collaborator

I would recommend,

[tool.pdm.scripts]
script_cmd = {cmd = "...", env_file="...", override = false}

as a compromise with the default staying as it is now.

This is not clear what override is to override, especially when env_file is not given, and that requires more validation logic.

Taking advantage of the TOML syntax, I would recommend:

[tool.pdm.scripts]
script_cmd = {cmd = "...", env_file.override="..."}

@pgjones
Copy link
Contributor

pgjones commented Aug 3, 2022

Makes sense, I'll open a PR

@pgjones
Copy link
Contributor

pgjones commented Aug 3, 2022

See #1299

@frostming
Copy link
Collaborator

Closed by #1299

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants