Skip to content

Commit 38e9aca

Browse files
committed
fix(sim-setup): quote env values containing $ so dotenv, Bun, and Compose don't expand them
1 parent 81c10c9 commit 38e9aca

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

‎packages/sim-setup/src/env-files.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ describe('upsertEnv', () => {
2828
}
2929
)
3030

31+
it('single-quotes a value with $ so dotenv, Bun, and Compose do not expand it', () => {
32+
expect(upsertEnv('', 'SMTP_PASSWORD', 'pa$$word$HOME')).toBe("SMTP_PASSWORD='pa$$word$HOME'\n")
33+
})
34+
3135
it('writes an ordinary value without quotes', () => {
3236
const dsn = 'postgresql://postgres:p%23w@127.0.0.1:5432/simstudio'
3337
expect(upsertEnv('', 'DATABASE_URL', dsn)).toBe(`DATABASE_URL=${dsn}\n`)

‎packages/sim-setup/src/env-files.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,20 @@ export function readEnvFile(target: EnvTarget): EnvFile {
8989
}
9090

9191
/**
92-
* Quotes a value only when `#`, edge whitespace, or a leading quote would change
93-
* it for `parseValue`, Next's dotenv, Bun, or Docker Compose. Single quotes are
94-
* preferred because all four read them literally.
92+
* Quotes a value only when `#`, `$`, edge whitespace, or a leading quote would
93+
* change it for `parseValue`, Next's dotenv, Bun, or Docker Compose (the last three
94+
* expand `$NAME` in unquoted values). Single quotes are preferred because all four
95+
* read them literally.
9596
*/
9697
function formatEnvValue(key: string, value: string): string {
9798
if (/[\r\n]/.test(value)) {
9899
throw new Error(`${key} cannot be written to an environment file: it contains a line break`)
99100
}
100-
if (value === value.trim() && !value.includes('#') && !/^["'`]/.test(value)) return value
101+
if (value === value.trim() && !/[#$]/.test(value) && !/^["'`]/.test(value)) return value
101102
if (!value.includes("'")) return `'${value}'`
102103
if (!/["\\$`]/.test(value)) return `"${value}"`
103104
throw new Error(
104-
`${key} cannot be written to an environment file: it mixes quotes with a # or edge whitespace`
105+
`${key} cannot be written to an environment file: it mixes quotes with a #, $, or edge whitespace`
105106
)
106107
}
107108

0 commit comments

Comments
 (0)