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

Regression in .env variable expansion when loading additional files #18975

Open
7 tasks done
tionkje opened this issue Dec 16, 2024 · 3 comments · May be fixed by #18981 or #18982
Open
7 tasks done

Regression in .env variable expansion when loading additional files #18975

tionkje opened this issue Dec 16, 2024 · 3 comments · May be fixed by #18981 or #18982
Labels
p2-to-be-discussed Enhancement under consideration (priority)

Comments

@tionkje
Copy link

tionkje commented Dec 16, 2024

Describe the bug

There seems to be a regression in Vite 6 related to loading extra .env files (.env.local or .env.production) related to parameter expansion.

In the extra loaded .env files where in the normal .env file the same variable is defined the expansion does not work.

Given:

# .env
VITE_EXPANDED=ORIGIN
# .env.local
VITE_SOURCE=56789
VITE_EXPANDED="Source: ${VITE_SOURCE}"

The VITE_SOURCE variable does not get expanded into VITE_EXPANDED
Result:
image

Expected:
If i specify a variable with the same name in the .env file it does get expanded correctly.
image

Reproduction

https://stackblitz.com/~/github.com/tionkje/vitejs-vite-hp7equ9q

Steps to reproduce

Run npm install followed by npm run dev

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (7) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.20.3 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.2.3 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    vite: ^6.0.3 => 6.0.3

Used Package Manager

npm

Logs

No response

Validations

@hi-ogawa
Copy link
Collaborator

I assume it's coming from the breaking change of dotenv-expand #18697, but this seems like something unintended.

@hi-ogawa
Copy link
Collaborator

hi-ogawa commented Dec 17, 2024

Oh, this looks tricky. Since the first .env file doesn't have VITE_SOURCE, this flips the order of VITE_SOURCE and VITE_EXPANDED in object keys, which causes VITE_EXPANDED not to expand as per breaking change.

I'm not sure what we can do about this though. Workaround is to add empty VITE_SOURCE= to .env, so object properties order are preserved. https://stackblitz.com/edit/github-prsj2pg4?file=.env

Also here is the repro with only dotenv-expand https://stackblitz.com/edit/github-prsj2pg4?file=repro.mjs

import { expand } from 'dotenv-expand';

const env1 = {
  SECOND: 'foo',
};
const env2 = {
  FIRST: 'ok',
  SECOND: 'expand-first-${FIRST}',
};
const env2Expanded = expand({ parsed: { ...env2 }, processEnv: {} }).parsed;

const merged = Object.fromEntries([
  ...Object.entries(env1),
  ...Object.entries(env2),
]);
const mergedAndExpanded = expand({
  parsed: { ...merged },
  processEnv: {},
}).parsed;

console.log({
  env1,
  env2,
  env2Expanded,
  merged,
  mergedAndExpanded,
});
{
  env1: { SECOND: 'foo' },
  env2: { FIRST: 'ok', SECOND: 'expand-first-${FIRST}' },
  env2Expanded: { FIRST: 'ok', SECOND: 'expand-first-ok' },
  merged: { SECOND: 'expand-first-${FIRST}', FIRST: 'ok' },
  mergedAndExpanded: { SECOND: 'expand-first-', FIRST: 'ok' }
}

@sapphi-red
Copy link
Member

I made two PRs with different approach that resolves this issue: #18981, #18982
We have the third option of not fixing this issue, but I prefer to merge either of the PRs as the current behavior is confusing.

@sapphi-red sapphi-red added the p2-to-be-discussed Enhancement under consideration (priority) label Dec 24, 2024
@github-project-automation github-project-automation bot moved this to Discussing in Team Board Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-to-be-discussed Enhancement under consideration (priority)
Projects
Status: Discussing
3 participants