serverless-dotenv-plugin icon indicating copy to clipboard operation
serverless-dotenv-plugin copied to clipboard

Automatic env file resolution is in reverse order of precedence

Open neverendingqs opened this issue 3 years ago • 7 comments

The README says

The env resolution pattern follows the one used by Rail's dotenv and create-react-app

But the code says https://github.com/neverendingqs/serverless-dotenv-plugin/blob/6fc076c692c2947951966967041e24e10fb4df01/index.js#L64-L72 and https://github.com/neverendingqs/serverless-dotenv-plugin/blob/6fc076c692c2947951966967041e24e10fb4df01/index.js#L84-L91

That last .reduce() call means we're actually resolving the files in reverse order of precedence, since env vars in lower priority files are overwriting the ones in higher priority files.

The fix is potentially a breaking change, and so requires a major version bump.

neverendingqs avatar Feb 16 '21 02:02 neverendingqs

When I implemented this, I used the create-react-app code as a sample. I now see this reduce may be causing issues.

https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/config/env.js

danilofuchs avatar Mar 01 '21 22:03 danilofuchs

Is that why .env.local takes precedence over .env?

feeh27 avatar Mar 25 '21 19:03 feeh27

Is that why .env.local takes precedence over .env?

.local should always have higher priority then their non-local equivalents. This is by design to allow a single dev to override a shared env

danilofuchs avatar Mar 25 '21 20:03 danilofuchs

Is that why .env.local takes precedence over .env?

.local should always have higher priority then their non-local equivalents. This is by design to allow a single dev to override a shared env

I use this plugin in my local environment and when I deploy to the environments, however .env.local is replacing the values of my .env file that is generated at run time in the deploy pipeline, the correct one wouldn't be .env take priority over .env.local?

feeh27 avatar Mar 25 '21 20:03 feeh27

@feeh27 .env.local is intended to override .env. This is by design, not a bug.

You should not commit .env.local to your git repo. If you are running the deployment on an CI, it should not have access to .env.local at all.

If you are running the deployment locally, I suggest you use .env.development and .env.development.local when developing and then .env or .env.production when deploying. You can manage which file is read by using the --stage argument for serverless

danilofuchs avatar Mar 25 '21 21:03 danilofuchs

I understood, really the way the library works and the projects I work on understand the .env.local and .env differently.

I have already managed to adjust this in my pipelines deleting the .env.local.

Thank you for your attention.

feeh27 avatar Mar 25 '21 21:03 feeh27

I'm still seeing .env values override .env..local. In my case .env.test.local.

henrypenny avatar Jan 16 '23 01:01 henrypenny