dotenvx: Variable expansion/interpolation bugs (depth and self-reference)

The parsing/handling of variable expansion doesn’t handle some useful/interesting cases. This is with version 1.22 npm module installed.

There are both self-reference issues, default prefix issues, and depth issues.

For self-reference, I have an very common case in my .env files where I want to set a variable if it’s not already set but this doesn’t work with dotenvx:

TEST_USER=${TEST_USER:-tester} # result: "${TEST_USER:-tester}"

If you have a prefix in your default value, dotenv stops expanding:

TEST_TRIV=prefix-${BAR} # result: "prefix-${BAR}"

And if your expansion is too deep, extraneous stuff appears:

TEST_DEEP=${A:-${B:-${C:-${D:-${E:-}}}}} # result: ":-}}"

I believe at least the depth problems are somewhere in this vicinity: https://github.com/dotenvx/dotenvx/blob/main/src/lib/helpers/dotenvExpand.js#L11 Parameter/variable expansion is a recursive by nature and I don’t think regexp alone has the power to parse a recursive grammars.

For the self-reference problem, my suspicion is that the contents of the env-file are loaded into a map all at once. And so self-reference gets the final version of the variable and loses earlier states that the variable was in when it occurred in order in the file.

My simple driver:

dotenvx = require('@dotenvx/dotenvx')
const res = dotenvx.config({path: ".env-test"})
Object.entries(res.parsed).sort().forEach(([k, v]) => console.log(`${k}=${v}`))

Here is my full .env-test file:

# Compare with correct behavior of shell command:
#     ( . .env-test; set | grep TEST_)

### These work

TEST_SIMPLE=${FOO:-bar}
TEST_DEEP1=${FOO:-${USER:-test}-suffix1}
TEST_DEEP2=${BAZ:-${BAR:-${FOO:-${USER:-test}-suffix1}-suffix2}-suffix3}


### The following are broken

# extraneous end curly braces:
TEST_DEEP3=${QUX:-${BAZ:-${BAR:-${FOO:-${USER:-test}-suffix1}-suffix2}-suffix3}-suffix4}
TEST_DEEP4=${QUXX:-${QUX:-${BAZ:-${BAR:-${FOO:-${USER:-test}-suffix1}-suffix2}-suffix3}-suffix4}-suffix5}

# prefix prevents continuing expansion:
TEST_DEEP5=${FOO:-prefix1-${USER:-test}}
TEST_DEEP6=${FOO:-prefix1-${USER:-test}-suffix1}
TEST_DEEP7=${BAR:-prefix2-${FOO:-prefix1-${USER:-test}-suffix1}-suffix2}

# both extraneous end curl and failure to expand
TEST_DEEP8=${QUXX:-prefix5-${QUX:-prefix4-${BAZ:-prefix3-${BAR:-prefix2-${FOO:-prefix1-${USER:-test}-suffix1}-suffix2}-suffix3}-suffix4}-suffix5}

# expansion is incomplete and incorrect:
TEST_RECUR1="${TEST_RECUR1:-} 234"
TEST_RECUR2=${TEST_RECUR2:-${USER:-test}-bar}
TEST_RECUR3=foo
TEST_RECUR3=prefix2-${TEST_RECUR3:-prefix1-${USER:-test}-suffix2}-suffix2

The correct behavior is this:

$ ( . .env-test; set | grep TEST_)
TEST_DEEP1=joelmartin-suffix1
TEST_DEEP2=joelmartin-suffix1-suffix2-suffix3
TEST_DEEP3=joelmartin-suffix1-suffix2-suffix3-suffix4
TEST_DEEP4=joelmartin-suffix1-suffix2-suffix3-suffix4-suffix5
TEST_DEEP5=prefix1-joelmartin
TEST_DEEP6=prefix1-joelmartin-suffix1
TEST_DEEP7=prefix2-prefix1-joelmartin-suffix1-suffix2
TEST_DEEP8=prefix5-prefix4-prefix3-prefix2-prefix1-joelmartin-suffix1-suffix2-suffix3-suffix4-suffix5
TEST_RECUR1=' 234'
TEST_RECUR2=joelmartin-bar
TEST_RECUR3=prefix2-foo-suffix2
TEST_SIMPLE=bar

Here is the dotenvx behavior:

$ node test.js
[dotenvx@1.22.0] injecting env (1) from .env-test
TEST_DEEP1=joelmartin-suffix1
TEST_DEEP2=joelmartin-suffix1-suffix2-suffix3
TEST_DEEP3=joelmartin-suffix1-suffix2-suffix3-suffix4}
TEST_DEEP4=joelmartin-suffix1-suffix2-suffix3-suffix4}-suffix5}
TEST_DEEP5=prefix1-${USER:-test}
TEST_DEEP6=prefix1-${USER:-test}-suffix1
TEST_DEEP7=prefix2-${FOO:-prefix1-${USER:-test}-suffix1}-suffix2
TEST_DEEP8=prefix5-${QUX:-prefix4-${BAZ:-prefix3-${BAR:-prefix2-${FOO:-prefix1-${USER:-test}-suffix1}-suffix2}-suffix3-suffix4}-suffix5}
TEST_RECUR1=${TEST_RECUR1:-} 234:-} 234
TEST_RECUR2=${TEST_RECUR2:-${USER:-test}-bar}
TEST_RECUR3=prefix2-prefix2-${TEST_RECUR3:-prefix1-${USER:-test}-suffix2}-suffix2-suffix2
TEST_SIMPLE=bar

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 27 (14 by maintainers)

Commits related to this issue

Most upvoted comments

release going out now as v1.38.3. thank you for using dotenvx 🙏

@motdotla I filed a ticket on the append/update capability: https://github.com/dotenvx/dotenvx/issues/441

all this said, it might be time dotenvx built its own expansion engine that properly subscribes to bash’s.