fix(infra): deterministic env var ordering (#4893)

This change sorts environment variables alphabetically before they're
processed with import-meta-env that injects them into the index.html.
This makes sure consistent hashes across pod restarts when there aren't
any functional changes made to the `.env` file.

The issue was affecting desktop app reconnections after k8s pod restarts.

The cause was non-deterministic env var ordering (a sideeffect of `Map`)
during the import-meta-env injection process. Even when the env vars
themselves didn't change, their order in the injected JSON could change,
ultimately resulting in different file hashes.

This fix ensures that regardless of the order in which env vars are
provided by the runtime, they will be sorted alphabetically before being
written to the build.env file.
This commit is contained in:
Shreyas 2025-03-19 16:08:47 +05:30 committed by GitHub
parent 18c233b9f9
commit 86c0bb619d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 3 additions and 0 deletions

View file

@ -36,6 +36,7 @@ function runChildProcessWithPrefix(command, args, prefix) {
const envFileContent = Object.entries(process.env)
.filter(([env]) => env.startsWith("VITE_"))
.sort(([envA], [envB]) => envA.localeCompare(envB))
.map(([env, val]) => `${env}=${
(val.startsWith("\"") && val.endsWith("\""))
? val

View file

@ -4,6 +4,7 @@ import fs from "fs"
const envFileContent = Object.entries(process.env)
.filter(([env]) => env.startsWith("VITE_"))
.sort(([envA], [envB]) => envA.localeCompare(envB))
.map(
([env, val]) =>
`${env}=${val.startsWith('"') && val.endsWith('"') ? val : `"${val}"`}`

View file

@ -34,6 +34,7 @@ function runChildProcessWithPrefix(command, args, prefix) {
const envFileContent = Object.entries(process.env)
.filter(([env]) => env.startsWith('VITE_'))
.sort(([envA], [envB]) => envA.localeCompare(envB))
.map(
([env, val]) =>
`${env}=${val.startsWith('"') && val.endsWith('"') ? val : `"${val}"`}`