setup:env crashes on Windows with ENOENT: mkdir '' when writing API keys
Running bun run setup:env on Windows fails at the API Key Configuration step. After entering the Codesandbox and OpenRouter API keys, the script crashes and the keys are never written to the .env files.
Steps to Reproduce
- On a Windows machine, run bun run setup:env
- Proceed through the Supabase variable prompts
- Enter values at the CSB_API_KEY and OPENROUTER_API_KEY prompts
- The script crashes
Actual Behavior
✓ Set CSB_API_KEY key ✓ Set OPENROUTER_API_KEY key Failed to write API keys: Error: ENOENT: no such file or directory, mkdir '' at Object.mkdirSync (node:fs:1343:26) at ensureDirectoryExists (.../packages/scripts/dist/index.js:9290:8) at writeApiKeysToFile (.../packages/scripts/dist/index.js:9245:5) ... errno: -4058, code: 'ENOENT', syscall: 'mkdir', path: ''
Expected Behavior
API keys are written to the web client and db .env files successfully on all platforms.
Root Cause
In packages/scripts/src/api-keys.ts, ensureDirectoryExists() derives the directory using a hardcoded forward-slash search:
const dir = filePath.substring(0, filePath.lastIndexOf('/'));
On Windows, paths use backslashes (e.g. A:...\client.env), so lastIndexOf('/') returns -1, making substring(0, -1) return ''. The subsequent fs.mkdirSync('') then throws ENOENT: mkdir ''.
This is Windows-only — Linux/macOS paths use /, so the bug never triggers there.
Proposed Fix
Use Node's cross-platform path.dirname() instead of manual string slicing:
import path from 'node:path';
const ensureDirectoryExists = (filePath: string): void => {
const dir = path.dirname(filePath);
if (dir && !fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
};This produces identical results on Linux/macOS (no regression) and correctly handles backslash paths on Windows.
Environment
- OS: Windows 11
- Package manager: Bun
- Affected file: packages/scripts/src/api-keys.ts
Want me to also draft the PR title/description for when you raise it?
Source: onlook-dev/onlook