Skip to content

Commit 1e3da14

Browse files
authored
Change frontend .env files to be committed and have .env.*.local overrides (Stirling-Tools#6207)
1 parent 276bbd6 commit 1e3da14

11 files changed

Lines changed: 77 additions & 123 deletions

File tree

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,16 @@ __pycache__/
163163
*.pyo
164164

165165
# Virtual environments
166-
.env*
167-
!.env*.example
168-
!engine/.env
169166
.venv*
170167
env*/
171168
venv*/
172169
ENV/
173170
env.bak/
174171
venv.bak/
175172

173+
# Env files (secrets / local overrides). Subproject .gitignore files whitelist any committed defaults.
174+
.env*
175+
176176
# VS Code
177177
/.vscode/**/*
178178
!/.vscode/settings.json

.taskfiles/frontend.yml

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ tasks:
2222
- npx tsx scripts/setup-env.ts
2323
sources:
2424
- scripts/setup-env.ts
25-
- config/.env.example
2625
generates:
27-
- .env
26+
- .env.local
2827

2928
prepare:env:saas:
3029
desc: "Generate .env and .env.saas from examples if missing"
@@ -34,11 +33,9 @@ tasks:
3433
- npx tsx scripts/setup-env.ts --saas
3534
sources:
3635
- scripts/setup-env.ts
37-
- config/.env.example
38-
- config/.env.saas.example
3936
generates:
40-
- .env
41-
- .env.saas
37+
- .env.local
38+
- .env.saas.local
4239

4340
prepare:env:desktop:
4441
desc: "Generate .env and .env.desktop from examples if missing"
@@ -48,11 +45,9 @@ tasks:
4845
- npx tsx scripts/setup-env.ts --desktop
4946
sources:
5047
- scripts/setup-env.ts
51-
- config/.env.example
52-
- config/.env.desktop.example
5348
generates:
54-
- .env
55-
- .env.desktop
49+
- .env.local
50+
- .env.desktop.local
5651

5752
prepare:icons:
5853
desc: "Generate icon bundle from source references"

AGENTS.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,14 @@ The project structure is defined in `engine/pyproject.toml`. Any new dependencie
136136
- **Development**: `task desktop:dev` for desktop dev mode
137137

138138
#### Environment Variables
139-
- All `VITE_*` variables must be declared in the appropriate example file:
140-
- `frontend/config/.env.example` — core, proprietary, and shared vars
141-
- `frontend/config/.env.saas.example` — SaaS-only vars
142-
- `frontend/config/.env.desktop.example` — desktop (Tauri)-only vars
143-
- Never use `|| 'hardcoded-fallback'` inline — put defaults in the example files
144-
- `task frontend:prepare` / `prepare:saas` / `prepare:desktop` auto-create the env files from examples on first run, and error if any required keys are missing
139+
- All `VITE_*` variables must be declared in the appropriate committed env file:
140+
- `frontend/.env` — core, proprietary, and shared vars
141+
- `frontend/.env.saas` — SaaS-only vars (layered on top of `.env` in SaaS mode)
142+
- `frontend/.env.desktop` — desktop (Tauri)-only vars (layered on top of `.env` in desktop mode)
143+
- These files are committed to Git and must not contain private keys
144+
- Local overrides (API keys, machine-specific settings) go in uncommitted sibling `.env.local` / `.env.saas.local` / `.env.desktop.local` files — Vite automatically layers them on top
145+
- Never use `|| 'hardcoded-fallback'` inline — put defaults in the committed env files
146+
- `task frontend:prepare` / `prepare:saas` / `prepare:desktop` create empty `.local` override files on first run
145147
- Prepare runs automatically as a dependency of all `dev*`, `build*`, and `desktop*` tasks
146148
- See `frontend/README.md#environment-variables` for full documentation
147149

engine/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ yarn-error.log*
2121
# Environment
2222
.env.local
2323

24+
# Root .gitignore ignores all .env* - whitelist our committed .env here
25+
!.env
26+
2427
# LaTeX outputs
2528
*.aux
2629
*.log
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
###############################################################################
12
# Frontend environment variables for core and proprietary builds.
3+
# Values can be overridden in the uncommitted sibling `.env.local` file.
4+
# Note: This file is committed to Git, so should not contain any private keys.
5+
###############################################################################
26

37
# API base URL — use / for same-origin (default for web builds)
48
VITE_API_BASE_URL=/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
###############################################################################
12
# Frontend environment variables for desktop (Tauri) builds.
23
# Layered on top of .env when running in desktop mode.
4+
# Values can be overridden in the uncommitted sibling `.env.desktop.local` file.
5+
# Note: This file is committed to Git, so should not contain any private keys.
6+
###############################################################################
37

48
# Desktop backend endpoint — leave blank to use VITE_API_BASE_URL from .env
59
VITE_DESKTOP_BACKEND_URL=
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
###############################################################################
12
# Frontend environment variables for SaaS builds.
23
# Layered on top of .env when running in SaaS mode.
4+
# Values can be overridden in the uncommitted sibling `.env.saas.local` file.
5+
# Note: This file is committed to Git, so should not contain any private keys.
6+
###############################################################################
37

48
# Userback feedback widget — leave blank to disable
59
VITE_USERBACK_TOKEN=

frontend/.gitignore

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@
1414

1515
# misc
1616
.DS_Store
17-
.env
18-
.env.saas
19-
.env.desktop
2017
.env.local
21-
.env.development.local
22-
.env.test.local
23-
.env.production.local
24-
!.env*.example
18+
.env.*.local
19+
20+
# Root .gitignore ignores all .env* - whitelist our committed ones here
21+
!.env
22+
!.env.desktop
23+
!.env.saas
2524

2625
npm-debug.log*
2726
yarn-debug.log*
@@ -32,4 +31,4 @@ test-results
3231

3332
# auto-generated files
3433
/src/assets/material-symbols-icons.json
35-
/src/assets/material-symbols-icons.d.ts
34+
/src/assets/material-symbols-icons.d.ts

frontend/README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@ For desktop app development, see the [Tauri](#tauri) section below.
1515

1616
## Environment Variables
1717

18-
The frontend requires environment variables to be set before running. `task frontend:dev` will create a `.env` file for you automatically on first run using the defaults from `config/.env.example` - for most development work this is all you need.
18+
Environment variables live in committed `.env` files at the frontend root:
1919

20-
If you need to configure specific services (Google Drive, Supabase, Stripe, PostHog), edit your local `.env` file. The values in `config/.env.example` show what each variable does and provides sensible defaults where applicable.
20+
- `.env` — used by all builds (core, proprietary, and as the base for desktop/SaaS)
21+
- `.env.desktop` — additional vars loaded in desktop (Tauri) mode
22+
- `.env.saas` — additional vars loaded in SaaS mode
2123

22-
For desktop (Tauri) development, `task desktop:dev` will additionally create a `.env.desktop` file from `config/.env.desktop.example`.
24+
These files contain non-secret defaults and are checked into Git, so most dev work needs no further setup.
25+
26+
To override values locally (API keys, machine-specific settings), create an uncommitted sibling `.env.local` / `.env.desktop.local` / `.env.saas.local`. Vite automatically layers these on top of the committed files.
2327

2428
## Docker Setup
2529

@@ -72,8 +76,3 @@ task desktop:clean
7276
```
7377

7478
Removes all desktop build artifacts including JLink runtime, bundled JARs, Cargo build, and dist/build directories.
75-
76-
> [!NOTE]
77-
>
78-
> Desktop builds require additional environment variables. See [Environment Variables](#environment-variables)
79-
> above - `task desktop:dev` will set these up automatically from `config/.env.desktop.example` on first run.

frontend/scripts/setup-env.ts

Lines changed: 26 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,44 @@
11
/**
2-
* Copies missing env files from their .example templates, and warns about
3-
* any keys present in the example but not set in the environment.
4-
* Also warns about any VITE_ vars set in the environment that aren't listed
5-
* in any example file.
2+
* Ensures `.env.local` (and mode-specific `.env.desktop.local` / `.env.saas.local`)
3+
* files exist so developers have a place to put overrides (API keys, machine-specific
4+
* settings) without touching the committed `.env` / `.env.desktop` / `.env.saas` files.
5+
*
6+
* Vite automatically layers these `.local` files on top of the committed ones.
67
*
78
* Usage:
8-
* tsx scripts/setup-env.ts # checks .env
9-
* tsx scripts/setup-env.ts --desktop # also checks .env.desktop
10-
* tsx scripts/setup-env.ts --saas # also checks .env.saas
9+
* tsx scripts/setup-env.ts # ensures .env.local
10+
* tsx scripts/setup-env.ts --desktop # also ensures .env.desktop.local
11+
* tsx scripts/setup-env.ts --saas # also ensures .env.saas.local
1112
*/
1213

13-
import { existsSync, copyFileSync, readFileSync } from "fs";
14+
import { existsSync, writeFileSync } from "fs";
1415
import { join } from "path";
15-
import { config, parse } from "dotenv";
1616

1717
// npm scripts run from the directory containing package.json (frontend/)
1818
const root = process.cwd();
1919
const args = process.argv.slice(2);
2020
const isDesktop = args.includes("--desktop");
2121
const isSaas = args.includes("--saas");
2222

23-
console.log(
24-
"setup-env: see frontend/README.md#environment-variables for documentation",
25-
);
26-
27-
function getExampleKeys(exampleFile: string): string[] {
28-
const examplePath = join(root, exampleFile);
29-
if (!existsSync(examplePath)) return [];
30-
return Object.keys(parse(readFileSync(examplePath, "utf-8")));
23+
function template(parent: string): string {
24+
return [
25+
"###############################################################################",
26+
`# Local overrides for \`frontend/${parent}\``,
27+
"# Put API keys and machine-specific settings here. Any variable defined here",
28+
`# takes precedence over the committed \`${parent}\``,
29+
"###############################################################################",
30+
"",
31+
].join("\n");
3132
}
3233

33-
function ensureEnvFile(envFile: string, exampleFile: string): boolean {
34-
const envPath = join(root, envFile);
35-
const examplePath = join(root, exampleFile);
36-
37-
if (!existsSync(examplePath)) {
38-
console.warn(`setup-env: ${exampleFile} not found, skipping ${envFile}`);
39-
return false;
40-
}
41-
42-
if (!existsSync(envPath)) {
43-
copyFileSync(examplePath, envPath);
44-
console.log(`setup-env: created ${envFile} from ${exampleFile}`);
45-
}
46-
47-
config({ path: envPath });
48-
49-
const missing = getExampleKeys(exampleFile).filter(
50-
(k) => !(k in process.env),
51-
);
52-
53-
if (missing.length > 0) {
54-
console.error(
55-
`setup-env: ${envFile} is missing keys from ${exampleFile}:\n` +
56-
missing.map((k) => ` ${k}`).join("\n") +
57-
"\n Add them manually or delete your local file to re-copy from the example.",
58-
);
59-
return true;
34+
function ensureLocalFile(localFile: string, parentFile: string): void {
35+
const localPath = join(root, localFile);
36+
if (!existsSync(localPath)) {
37+
writeFileSync(localPath, template(parentFile));
38+
console.log(`setup-env: created empty ${localFile} for local overrides`);
6039
}
61-
62-
return false;
63-
}
64-
65-
let failed = false;
66-
failed = ensureEnvFile(".env", "config/.env.example") || failed;
67-
68-
if (isDesktop) {
69-
failed =
70-
ensureEnvFile(".env.desktop", "config/.env.desktop.example") || failed;
71-
}
72-
73-
if (isSaas) {
74-
failed = ensureEnvFile(".env.saas", "config/.env.saas.example") || failed;
75-
}
76-
77-
// Warn about any VITE_ vars set in the environment that aren't listed in any example file.
78-
const allExampleKeys = new Set([
79-
...getExampleKeys("config/.env.example"),
80-
...getExampleKeys("config/.env.desktop.example"),
81-
...getExampleKeys("config/.env.saas.example"),
82-
]);
83-
const unknownViteVars = Object.keys(process.env).filter(
84-
(k) => k.startsWith("VITE_") && !allExampleKeys.has(k),
85-
);
86-
if (unknownViteVars.length > 0) {
87-
console.warn(
88-
"setup-env: the following VITE_ vars are set but not listed in any example file:\n" +
89-
unknownViteVars.map((k) => ` ${k}`).join("\n") +
90-
"\n Add them to the appropriate config/.env.*.example file if they are required.",
91-
);
9240
}
9341

94-
if (failed) process.exit(1);
42+
ensureLocalFile(".env.local", ".env");
43+
if (isDesktop) ensureLocalFile(".env.desktop.local", ".env.desktop");
44+
if (isSaas) ensureLocalFile(".env.saas.local", ".env.saas");

0 commit comments

Comments
 (0)