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

Usage produces fatal error when .env file defines a variable with same name as expanded variable. #98

Closed
atljoseph opened this issue Apr 7, 2023 · 9 comments

Comments

@atljoseph
Copy link

atljoseph commented Apr 7, 2023

When I have a .env file like this, everything works fine:

MY_VAR_1=$MY_VAR

But this breaks:

MY_VAR_1=$MY_VAR
MY_VAR_2=$MY_VAR_2

This line causes the breakage:

MY_VAR_2=$MY_VAR_2

Somehow the library cannot handle this situation.

Error produced:

/Users/<REDACTED>/flourish-node-api/node_modules/dotenv-expand/lib/main.js:6
  return matches.reduce(function (newEnv, match, index) {
                 ^
RangeError: Maximum call stack size exceeded
    at RegExp.exec (<anonymous>)
    at /Users/<REDACTED>/flourish-node-api/node_modules/dotenv-expand/lib/main.js:7:54
    at Array.reduce (<anonymous>)
    at _interpolate (/Users/<REDACTED>/flourish-node-api/node_modules/dotenv-expand/lib/main.js:6:18)
    at /Users/<REDACTED>/flourish-node-api/node_modules/dotenv-expand/lib/main.js:36:15
    at Array.reduce (<anonymous>)
    at _interpolate (/Users/<REDACTED>/flourish-node-api/node_modules/dotenv-expand/lib/main.js:6:18)
    at /Users/<REDACTED>/flourish-node-api/node_modules/dotenv-expand/lib/main.js:36:15
    at Array.reduce (<anonymous>)
    at _interpolate (/Users/<REDACTED>/flourish-node-api/node_modules/dotenv-expand/lib/main.js:6:18)

Someone has already faced this issue and documented it somewhat:
https://stackoverflow.com/questions/49287598/maximum-call-stack-size-exceeded-from-dotenv-expand-on-circleci-using-react

You might ask the question: Why?

  • To that I would respond: Why not? Why does it matter how people use it?
  • And I would add: It is a form of documentation, in case other developers use different variable names to expand than me.
  • Pretty please, fix this.
  • It took so long to stumble onto why this is happening.
  • I was using this lib to save time, and pain.... :/

Using these versions:

    "dotenv": "14.1.0",
    "dotenv-expand": "^9.0.0", <--- And tried v10 (latest), same outcome.
@alfetopito
Copy link

I second that request.

My use case is that I'd like to use the .env files to hold defaults for my env vars, and they have the same name externally as internally to reduce confusion.

So even if the CI sets a env var named MY_VAR to an empty string, the .env default value defined like MY_VAR=${MY_VAR:-DEFAULT} will be respected.

And in the app I don't have to care the source of value, just rely on MY_VAR

@atljoseph
Copy link
Author

atljoseph commented Apr 19, 2023 via email

@andrewmclagan
Copy link

+1 on this request

@JumpIfBelow
Copy link

JumpIfBelow commented Sep 28, 2023

I stumbled on that issue as I was trying to set a default variable if it ever was not available.
For example, I was willing to set the $GID variable if it was not available:

GID="${GID:-${GROUPS:-$UID}}"

Let's admit that my case is a bit overcomplicated and only use the following:

GID="${GID:-$UID}"

I would still get the same error, while it is completely missing the point of the syntax, that is a valid one.
I might not expect the substitution to work well in that specific case, but neither would I expect it to crash that badly either.

Thanks for your time maintaining this awesome package 😄

@celorodovalho
Copy link

No updates about this issue?

@cyango
Copy link

cyango commented Oct 19, 2023

same issue here

@JackZhengSit
Copy link

same issue too.

@motdotla
Copy link
Owner

motdotla commented Feb 6, 2024

looking into this soon. thank you for your patience everyone. know it is frustrating. lots to keep up on!

@motdotla
Copy link
Owner

this is fixed in v11.0.0 (releasing shortly)

here is test demonstrating it is fixed: #111

any recursive call like this will now end up just returning the raw value it was set to.

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

No branches or pull requests

8 participants