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

Merged
merged 1 commit into from
Nov 13, 2024

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
}

@lsmurray
Copy link

lsmurray commented Oct 9, 2024

@motdotla - sorry to bug you but this is causing issues with vite. Is there a chance this could get merged and released?

@motdotla motdotla merged commit 54bcda0 into motdotla:master Nov 13, 2024
@motdotla
Copy link
Owner

thank you @lsmurray !

@motdotla
Copy link
Owner

and thank you @stvvt for the PR!

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.

3 participants