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

Fix self-expanding undefined variable with default value. #126

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

stvvt
Copy link

@stvvt stvvt commented Jun 3, 2024

Currently

const expanded = dotenvExpand.expand({
  parsed: {
    EXPAND_UNDEFINED: '${UNDEFINED}',
    EXPAND_SELF: '${EXPAND_SELF}',
    EXPAND_UNDEFINED_WITH_DEFAULT: '${UNDEFINED:-default}',
    EXPAND_SELF_WITH_DEFAULT: '${EXPAND_SELF_WITH_DEFAULT:-default}'
  }
})

console.log(expanded.parsed)

produces

{
  EXPAND_UNDEFINED: '',          // undefined vars are expanded to an empty string
  EXPAND_SELF: '${EXPAND_SELF}', // but not if undefined is self-referenced!
  EXPAND_UNDEFINED_WITH_DEFAULT: 'default', // default is respected
  EXPAND_SELF_WITH_DEFAULT: '${EXPAND_SELF_WITH_DEFAULT:-default}' // default is ignored
}

In short - shelf-referenced expansions leave the values unchanged.

This pull requests changes the result to

{
  EXPAND_UNDEFINED: '', // empty string, as before
  EXPAND_SELF: '', // empty string, because expanding undefined
  EXPAND_UNDEFINED_WITH_DEFAULT: 'default', // default is respected, as before
  EXPAND_SELF_WITH_DEFAULT: 'default' // default value is now respected
}

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.

1 participant