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

Prefer runtime env values over .env values #711

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

Conversation

NAR8789
Copy link

@NAR8789 NAR8789 commented May 9, 2018

  • inherit environment variables from runtime environment in run and start, but not export
  • prefer variables defined in runtime environment over those in .env or via -e
  • fixes Read environment variables directly from CLI command #707 Read environment variables directly from CLI command
  • partially fixes .env load behaviour at odds with dotenv #702 .env load behaviour at odds with dotenv
    • inherits environment from runtime environment, and prefers runtime environment to .env, similar to dotenv
    • differs from dotenv in that dotenv always prefers earlier-defined variables, even within listed files. Foreman's existing behavior was to prefer later-defined variables in listed files, so I have kept this ordering for now. This would be easy to reverse if preferred

@NAR8789 NAR8789 changed the title Feature/prefer real env vars over dotenv Prefer runtime env values over .env values May 9, 2018
@ddollar
Copy link
Owner

ddollar commented Jun 18, 2018

This looks good, but can you please rebase it with recent merged changes?

@NAR8789 NAR8789 force-pushed the feature/prefer-real-env-vars-over-dotenv branch from c806e6d to 27a017e Compare June 23, 2018 13:44
@NAR8789
Copy link
Author

NAR8789 commented Jun 23, 2018

@ddollar thanks for taking a look! Rebased.

@scalp42
Copy link

scalp42 commented Nov 29, 2018

@ddollar any chance to take a look at this PR? Thank you in advance!

@hez
Copy link

hez commented Feb 12, 2019

This is a great feature, we were just looking for this. Thank you @NAR8789 👍

@NAR8789
Copy link
Author

NAR8789 commented Feb 14, 2019

Thanks @scalp42, thanks @hez!

Reference for anyone else coming to this PR: There is a workaround in the discussion in Issue #561. tl;dr start foreman with foreman start -e /dev/null.

This suppresses foreman's .env-loading behavior altogether (thus letting you pass through whatever env vars you like at foreman invocation time). This works for my application because the application itself uses dotenv, so I get fully-compliant dotenv behavior.

I suspect actually that most applications are like this, in that the right thing to do is suppress foreman's .env behavior entirely.

(also, sorry for the notification spam--I posted with the wrong account earlier)

@scalp42
Copy link

scalp42 commented Mar 18, 2019

@NAR8789 thanks a lot for the workaround.

Can you confirm that if you use -e /dev/null it doesn't just nullify the ENV variables all together? Meaning dotenv won't still work either way?

@NAR8789
Copy link
Author

NAR8789 commented Mar 26, 2019

@scalp42 sure! try the following:

mkdir foreman-nullenv
cd foreman-nullenv
echo 'env: env' > Procfile    # create a procfile that just prints your env
foreman start                 # run it!

here you should get output corresponding to your exported shell env. You can play around with creating a .env to see foreman's usual undesriable override behavior.

Then try with -e /dev/null. This will confirm that -e /dev/null does not just nullify environment

foreman start -e /dev/null

Here you should get your original env again. If this nullified environment, you'd get no output.

Try playing around with exporting additional env vars, or specifying env vars on the same line as foreman start. All these should work more or less intuitively, and should prove foreman start -e /dev/null just disables forman's env munging.


In actual development, I point foreman at a file containing an explanatory comment (instead of /dev/null), to give developers context about the weird foreman invocation. So... invoke foreman as foreman start -e .env.noop, and .env.noop looks like:

# this file purposely empty, to prevent foreman from altering environment

@scalp42
Copy link

scalp42 commented Mar 26, 2019

@NAR8789 thanks a lot for the help! Much appreciated 🙇

@Intrepidd
Copy link

Hi ! Any status on this one ? I chased a bug for hours before finding that it was due to this foreman behaviour :(

Thanks !

@scalp42
Copy link

scalp42 commented Mar 26, 2022

@ddollar any chance to get this merged?

@NAR8789 NAR8789 force-pushed the feature/prefer-real-env-vars-over-dotenv branch from 27a017e to dba8a50 Compare December 7, 2023 20:05
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.

Read environment variables directly from CLI command .env load behaviour at odds with dotenv
5 participants