Decode UTF-16 .env files on variable import - #3186
Conversation
File.text() always decodes UTF-8, but .env files written on Windows are often UTF-16 (PowerShell's > redirect defaults to it). Decoded as UTF-8, every character gains an interleaved NUL byte, so a key like VITE_GEMINI_API_KEY was stored as V\u0000I\u0000T\u0000E... - an invalid env var name that broke every subsequent deployment for the resource. readEnvFile() detects UTF-16 by BOM, or by the interleaved-NUL pattern when the BOM is missing, and decodes accordingly; both import modals now use it instead of File.text().
Console (appwrite/console)Project ID: Sites (1)
Tip Deploy functions via zip upload or connect directly to your Git repo |
Greptile SummaryThis PR adds encoding-aware
Confidence Score: 4/5The PR should not merge until BOM-less UTF-16 files with predominantly non-ASCII values are decoded reliably rather than rejected during import. The new detector only recognizes BOM-less UTF-16 when over 70% of all code units exhibit ASCII-style parity NULs, leaving a realistic class of valid Unicode-valued files on the original broken UTF-8 decoding path. Files Needing Attention: src/lib/helpers/envfile.ts, src/lib/helpers/envfile.test.ts Important Files Changed
|
|
Superseded by #3187 (same change with sanitized test fixtures). |

What does this PR do?
File.text()always decodes UTF-8, but.envfiles written on Windows are often UTF-16 -- PowerShell's>redirect defaults to it. Decoded as UTF-8, every character gains an interleaved NUL byte, so importing such a file stored keys with a NUL after every letter -- an invalid environment variable name that invalidated the Kubernetes build job manifest and failed every subsequent deployment for that resource (this is the origin of a production incident on cloud).This adds
readEnvFile()to$lib/helpers/envfile: it detects UTF-16 by BOM (FF FE/FE FF), or by the interleaved-NUL byte pattern when the BOM is missing, and decodes with the rightTextDecoder. Both import modals (importVariablesModal,uploadVariablesModal) now use it instead ofFile.text()-- so a PowerShell-authored.envsimply imports correctly instead of erroring on every key.Test plan
src/lib/helpers/envfile.test.ts-- 7 cases: UTF-8 (with/without BOM), UTF-16LE/BE (with/without BOM), and UTF-8 containing a genuine stray NUL staying UTF-8. All passing; lint clean.Related PRs and Issues