motdotla / dotenv

Loads environment variables from .env for nodejs projects.
https://www.dotenvx.com
BSD 2-Clause "Simplified" License
19.01k stars 853 forks source link

Multiple paths in config: README incorrect? #802

Closed thanosd closed 6 months ago

thanosd commented 7 months ago

Readme.md says this about passing files:

Pass in multiple files as an array, and they will be loaded in order. The first value set for a variable will win.

This implies that all files are loaded, and if a variable is specified in multiple files the first instance wins.

However, it seems the behavior is "Open the first file that exists and stop".

      if (Array.isArray(envPath)) {
        for (const filepath of options.path) {
          if (fs.existsSync(filepath)) {
            envPath = filepath
            break
          }
        }
      }

Two possible fixes:

a) Fix the readme to describe the current behavior. b) Fix the code, to perform what is described in the README. This would cause a change in behavior that may be unexpected for some (though useful for others).

Happy to produce a PR, but thoughts on direction before I do so?

motdotla commented 7 months ago

it should be a. this is work we wrote up in the README before quite finishing.

a PR would be very welcome. thank you @thanosd !

thanosd commented 7 months ago

I'll spin one out later today. If I also implemented (b) in a more backwards compatible way, would there be interest?

motdotla commented 7 months ago

my mistake. i meant to say b. Fix the code, to perform what is described in the README. the behavior should match the README. the README was written ahead of the behavior in this case.

thanosd commented 7 months ago

OK, I can do that too, but that will take me a day :). Any concerns or changing the implementation? If yes, we could mitigate by adding a flag mergeMultipleFiles, defaulting to false, which would be the current behavior.

motdotla commented 7 months ago

this was a very new implementation (https://github.com/motdotla/dotenv/commit/f46336f3efce2ea202f73aaaccc26b727d1e92ee), so no concerns.

filipopo commented 7 months ago

Came here to report this same issue, I'm glad someone took a look at it

thanosd commented 7 months ago

Took a stab at a PR. Feedback welcome.

motdotla commented 6 months ago

released as v16.4.3. big thanks to @thanosd for making dotenv even better for the community!