-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
.env files with UTF16-LE BOM are ignored #14564
Comments
I guess the fix is that we have to try loading the vite/packages/vite/src/node/env.ts Line 37 in 5bb13aa
And that would slightly hurt startup time, so I'm not really sure about fixing this. I think they should be strictly vite/packages/vite/src/node/plugins/loadFallback.ts Lines 8 to 20 in 5bb13aa
|
@bluwy Yes, trying multiple encodings seems unreliable and potentially slow. Enforcing import fsp from 'node:fs/promises'
try {
const r = await fsp.readFile('./.env.development.local', 'utf-8')
console.log(r)
console.log(Array.from(r).map(b => b.charCodeAt(0)))
} catch (e) {
console.log(e)
} Output:
It looks like a fundamental limitation with I think it could be solved if we decode with import fsp from 'node:fs/promises'
import util from 'node:util'
const decoder = new util.TextDecoder('utf-8', {fatal: true})
const buffer = await fsp.readFile('./.env.development.local')
try {
console.log(decoder.decode(buffer))
} catch (e) {
console.log(e)
} The above correctly fails with
|
I'm still not sure if it's worth fixing, wouldn't that also affect perf? There's a lot of tools today that reads files with |
@bluwy I believe it's worth fixing.
I haven't found measurable performance difference. Half of the time
https://stackblitz.com/edit/vitest-dev-vitest-oqhnfg?file=test%2Fencoding.bench.ts |
FWIW Even if we fixed every place in our code base, there'd be plenty of codes that we depend on that assumes the file is encoded in UTF-8. I don't think we can fix this. |
@sapphi-red nice find! However, Vite does not supply I would argue broken env handling on Windows is common enough to warrant a fix for this specific case (not general). I’ve posted some links in my previous comment. I think It would be very nice if Vite would not fail silently and in a hard-to-debug manner. How does Vite contribution flow work? Is it a good idea to get an approval from a maintainer first? |
Describe the bug
Vite does not correctly handle
.env
files with BOM. For example, it happens if the file is created with Windowsecho
command:With this file
import.meta.env.VITE_VAR
expands toundefined
.Reproduction: https://stackblitz.com/edit/vitejs-vite-m3as94?file=main.js
Instead of a silent error I would expect one of two things:
'1'
(best).env.development.local
(better)Debugging
Outside of Windows, the file can be created with the Python script:
Related issue
motdotla/dotenv#445
Reproduction
https://stackblitz.com/edit/vitejs-vite-m3as94?file=main.js
Steps to reproduce
No response
System Info
System: OS: Linux 5.0 undefined CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz Memory: 0 Bytes / 0 Bytes Shell: 1.0 - /bin/jsh Binaries: Node: 18.18.0 - /usr/local/bin/node Yarn: 1.22.19 - /usr/local/bin/yarn npm: 9.4.2 - /usr/local/bin/npm pnpm: 8.6.12 - /usr/local/bin/pnpm npmPackages: vite: ^4.4.8 => 4.4.11
Used Package Manager
npm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: