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

Vite fatal error when a variable in .env ends with "$", even if it is not accessible by Vite. #12006

Closed
7 tasks done
Amixx opened this issue Feb 9, 2023 · 4 comments · Fixed by #12031
Closed
7 tasks done
Labels
enhancement New feature or request

Comments

@Amixx
Copy link

Amixx commented Feb 9, 2023

Describe the bug

I have a project where a single .env file is used by both vite and symfony (PHP). There is a variable for symfony that ends with "$".
Might be a regression of this: #6858

Expectation: npm install && npm run dev runs successfully - starts devserver
Reality: npm install && npm run dev fails with the following output:

up to date, audited 16 packages in 528ms

4 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

> vite-bug-error-if-env-file-contains--@0.0.0 dev
> vite

error when starting dev server:
Error: dotenv-expand failed to expand env vars. Maybe you need to escape `$`?
    at loadEnv (file:///home/imants/IssueReproductions/vite-bug-error-if-env-file-contains-$/node_modules/vite/dist/node/chunks/dep-3007b26d.js:44346:19)
    at resolveConfig (file:///home/imants/IssueReproductions/vite-bug-error-if-env-file-contains-$/node_modules/vite/dist/node/chunks/dep-3007b26d.js:61877:9)
    at async createServer (file:///home/imants/IssueReproductions/vite-bug-error-if-env-file-contains-$/node_modules/vite/dist/node/chunks/dep-3007b26d.js:61127:20)
    at async CAC.<anonymous> (file:///home/imants/IssueReproductions/vite-bug-error-if-env-file-contains-$/node_modules/vite/dist/node/cli.js:734:24)

Reproduction

https://github.com/whitedigital-eu/vite-bug-error-if-env-file-contains--

Steps to reproduce

Install packages and run development environment: npm install && npm run dev

System Info

System:
    OS: Linux 5.15 Ubuntu 22.04.1 LTS 22.04.1 LTS (Jammy Jellyfish)
    CPU: (8) x64 AMD Ryzen 7 4700U with Radeon Graphics
    Memory: 11.25 GB / 22.71 GB
    Container: Yes
    Shell: 5.1.16 - /bin/bash
  Binaries:
    Node: 16.17.0 - ~/.nvm/versions/node/v16.17.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v16.17.0/bin/yarn
    npm: 8.15.0 - ~/.nvm/versions/node/v16.17.0/bin/npm
  Browsers:
    Chromium: 109.0.5414.119
    Firefox: 109.0.1
  npmPackages:
    vite: ^4.1.0 => 4.1.1

Used Package Manager

npm

Logs

No response

Validations

@bluwy
Copy link
Member

bluwy commented Feb 9, 2023

This is intentional to how dotenv-expand works. It thinks that the $ is a variable that it tries to expand, however it can't so it reports the error. You can fix it with:

VITE_FOO=foobar\$

@bluwy bluwy closed this as not planned Won't fix, can't repro, duplicate, stale Feb 9, 2023
@acirulis
Copy link

acirulis commented Feb 9, 2023

Hi, Vite documentation also states that "[..] Only VITE_SOME_KEY will be exposed as import.meta.env.VITE_SOME_KEY to your client source code, [..]".

Basically .env file (as single source for project configuration) may contain variables that are completely outside of scope for Vite/javascript source code. Therefore escaping them might not be possible or would require changes in unrelated code (e.g. Symfony framework, as OP suggested.). Wouldn't it be possible to enfore dotenv-expand only on VITE_* variables?

@bluwy
Copy link
Member

bluwy commented Feb 11, 2023

After discussion with @patak-dev, we think it might be worth skipping for private variables. That would mean patching dotenv-expand, but it should be possible.

@bluwy bluwy reopened this Feb 11, 2023
@bluwy bluwy added enhancement New feature or request and removed pending triage labels Feb 11, 2023
@sun0day
Copy link
Member

sun0day commented Feb 12, 2023

After discussion with @patak-dev, we think it might be worth skipping for private variables. That would mean patching dotenv-expand, but it should be possible.

Skipping for private variables will cause an error when VITE_ variables depend on them.

VITE_KEY=$PRIVATE_KEY
PRIVATE_KEY=xxxxx

To avoid impacting irrelevant private variables, I think patching dotenv-expand to be compatible with /^VITE_.*\$$/ is enough.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants