Skip to content

setup:env crashes on Windows with ENOENT: mkdir '' when writing API keys #3119

Description

@GlaDrancE

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

  1. On a Windows machine, run bun run setup:env
  2. Proceed through the Supabase variable prompts
  3. Enter values at the CSB_API_KEY and OPENROUTER_API_KEY prompts
  4. 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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions