docs(dev): add script and docs for local API key loading

- Create dev-with-env.sh script to load .env before build
- Add npm run dev:tauri command that uses the script
- Document why .env must be loaded before compilation
- Explain compile-time vs runtime environment variables

The issue: option_env!() reads vars at compile time, but .env
loads at runtime. Solution: source .env before cargo build.

cp
This commit is contained in:
vicrodh committed 2026-01-14 19:43:18 -06:00
1 parent b9c8ecb274
commit 6e21b3ed1f
3 files changed
+56

No files matched your search

+45
View File
@@ -0,0 +1,45 @@
# Development with API Keys
## Problem
The API keys in `.env` are embedded at **compile-time** using Rust's `option_env!()` macro, not at runtime. This means the `.env` file must be loaded into environment variables **before** compilation.
## Solution for Local Development
Use the custom script that loads `.env` before building:
```bash
npm run dev:tauri
```
This script:
1. Sources the `.env` file into the shell environment
2. Runs `npm run tauri dev` with those variables available during compilation
## Alternative: Manual Export
If you prefer to run `npm run tauri dev` directly, export the variables first:
```bash
# Load .env into current shell
set -a
source .env
set +a
# Now run dev
npm run tauri dev
```
## Why This is Needed
- **CI/CD builds**: Variables are injected via GitHub Secrets → environment → `option_env!()` at compile time
- **Local development**: Variables must be in shell environment before `cargo build` runs
- The `dotenvy::dotenv()` in `lib.rs` only loads variables at **runtime**, but `option_env!()` reads them at **compile time**
## Embedded Keys
The following keys are embedded at compile time:
- `LAST_FM_API_KEY` / `LAST_FM_API_SHARED_SECRET`
- `DISCOGS_API_CLIENT_KEY` / `DISCOGS_API_CLIENT_SECRET`
- `SPOTIFY_API_CLIENT_ID` / `SPOTIFY_API_CLIENT_SECRET`
- `TIDAL_API_CLIENT_ID` / `TIDAL_API_CLIENT_SECRET`
+1
View File
@@ -5,6 +5,7 @@
"type": "module", "type": "module",
"scripts": { "scripts": {
"dev": "vite dev", "dev": "vite dev",
"dev:tauri": "./scripts/dev-with-env.sh",
"build": "vite build", "build": "vite build",
"preview": "vite preview", "preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json", "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
+10
View File
@@ -0,0 +1,10 @@
#!/bin/bash
# Development script that loads .env variables before building
set -a
if [ -f .env ]; then
source .env
fi
set +a
npm run tauri dev