fix: preserve ${VAR} interpolation in .env files

Escaping in prepareEnvironmentVariablesForFile escaped every $,
including inside a deliberate ${VAR} reference, breaking Compose's
documented .env variable interpolation. Only escape $ that isn't
part of a ${IDENTIFIER} sequence.

Fixes #5151
This commit is contained in:
Mauricio Siu
2026-08-27 00:47:01 -06:00
parent 3c6a96a30a
commit 452b7aa2f2
2 changed files with 5 additions and 1 deletions
@@ -32,6 +32,8 @@ const cases: Record<string, string> = {
APOSTROPHE: "it's a test",
UNICODE: "héllo wörld 日本語 🚀",
MULTILINE_PEM: "-----BEGIN KEY-----\nabc123\n-----END KEY-----",
APP_URL: "https://example.com",
ASSET_URL: "https://example.com",
};
// How each value must be typed in the UI so the (unchanged) dotenv input
@@ -46,6 +48,8 @@ const inputEncoding: Record<string, string> = {
APOSTROPHE: "it's a test",
UNICODE: "héllo wörld 日本語 🚀",
MULTILINE_PEM: '"-----BEGIN KEY-----\nabc123\n-----END KEY-----"',
APP_URL: "https://example.com",
ASSET_URL: '"${APP_URL}"',
};
describe("getCreateEnvFileCommand", () => {
+1 -1
View File
@@ -548,7 +548,7 @@ export const prepareEnvironmentVariablesForFile = (
const escapedValue = value
.replace(/\\/g, "\\\\")
.replace(/"/g, '\\"')
.replace(/\$/g, "\\$");
.replace(/\$(?!\{[A-Za-z_][A-Za-z0-9_]*\})/g, "\\$");
return `${key}="${escapedValue}"`;
});
};