mastra-ai/mastra · error

Failed to create directory: ${errorMessage}

Error message

Failed to create directory: ${errorMessage}

What it means

`writeScorer` wraps any failure from fs.mkdirSync when creating the scorers output directory. The underlying OS error (e.g. EACCES, ENOSPC, ENOTDIR) is embedded in the message. It means the CLI could not create the directory where the generated scorer file should live.

Source

Thrown at packages/cli/src/commands/scorers/file-utils.ts:18

import fs from 'node:fs';
import path from 'node:path';
import * as p from '@clack/prompts';

const DEFAULT_SCORERS_DIR = 'src/mastra/scorers';

export function writeScorer(filename: string, content: string, customPath?: string): { ok: true; message: string } {
  const rootDir = process.cwd();
  const scorersPath = customPath || DEFAULT_SCORERS_DIR;
  const fullPath = path.join(rootDir, scorersPath);

  if (!fs.existsSync(fullPath)) {
    try {
      fs.mkdirSync(fullPath, { recursive: true });
      p.log.success(`Created scorers directory at ${scorersPath}`);
    } catch (error) {
      const errorMessage = error instanceof Error ? error.message : String(error);
      throw new Error(`Failed to create directory: ${errorMessage}`);
    }
  }

  const filePath = path.join(fullPath, filename);

  if (fs.existsSync(filePath)) {
    throw new Error(`Skipped: Scorer ${filename} already exists at ${scorersPath}`);
  }

  try {
    fs.writeFileSync(filePath, content);

    return { ok: true, message: `Created scorer at ${path.relative(rootDir, filePath)}` };
  } catch (error) {
    const errorMessage = error instanceof Error ? error.message : String(error);
    throw new Error(`Failed to write scorer: ${errorMessage}`);
  }
}

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Check write permissions on the target directory (ls -la) and fix with chmod/chown
  2. Ensure no regular file exists at the scorers path that blocks directory creation
  3. Free disk space if ENOSPC
  4. Create the directory manually with mkdir -p and rerun

Example fix

// before
# scorers path occupied by a file
scorers  (regular file)
// after
rm scorers && mkdir -p scorers
Defensive patterns

Strategy: try-catch

Validate before calling

import { statSync, existsSync } from 'node:fs';
if (existsSync(scorersPath) && !statSync(scorersPath).isDirectory()) {
  throw new Error(`${scorersPath} exists and is not a directory`);
}
accessSync(dirname(scorersPath), constants.W_OK);

Type guard

const canWrite = (p: string): boolean => {
  try { accessSync(p, constants.W_OK); return true; } catch { return false; }
};

Try / catch

try {
  await writeScorer({ ... });
} catch (e) {
  const msg = (e as Error).message;
  if (msg.startsWith('Failed to create directory')) {
    console.error('Check permissions/disk space for the scorers path:', msg);
  } else throw e;
}

Prevention

When it happens

Trigger: Running the scorer add/generate command when the target path does not exist and mkdirSync fails — typically due to insufficient write permissions, a path component that is a file, or read-only filesystem.

Common situations: Scorers path collides with an existing file named `scorers`; running in a sandbox/container without write access to the project root; disk full.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/61c5cda6257844d6. Report an issue: GitHub.