thedotmack/claude-mem · error

Folder does not exist: ${folderPath}

Error message

Folder does not exist: ${folderPath}

What it means

Thrown by writeClaudeMdToFolder() when it tries to write the managed <claude-mem-context> block into a folder that no longer exists on disk. The function resolves the path, skips any .git directory, then checks existsSync(folderPath) before attempting the write — so this fires only for genuinely absent directories. It guards against a traversal/discovery race where a directory was enumerated earlier but is gone by write time.

Source

Thrown at src/cli/claude-md-commands.ts:248

      }

      lines.push('');
    }
  }

  return lines.join('\n').trim();
}

function writeClaudeMdToFolder(folderPath: string, newContent: string): void {
  const resolvedPath = path.resolve(folderPath);

  if (resolvedPath.includes('/.git/') || resolvedPath.includes('\\.git\\') || resolvedPath.endsWith('/.git') || resolvedPath.endsWith('\\.git')) return;

  const claudeMdPath = path.join(folderPath, 'CLAUDE.md');
  const tempFile = `${claudeMdPath}.tmp`;

  if (!existsSync(folderPath)) {
    throw new Error(`Folder does not exist: ${folderPath}`);
  }

  let existingContent = '';
  if (existsSync(claudeMdPath)) {
    existingContent = readFileSync(claudeMdPath, 'utf-8');
  }

  const startTag = '<claude-mem-context>';
  const endTag = '</claude-mem-context>';

  let finalContent: string;
  if (!existingContent) {
    finalContent = `${startTag}\n${newContent}\n${endTag}`;
  } else {
    const startIdx = existingContent.indexOf(startTag);
    const endIdx = existingContent.indexOf(endTag);

    if (startIdx !== -1 && endIdx !== -1) {

View on GitHub (pinned to d768ba3643)

Solutions

  1. Confirm the folder still exists at the reported path with `ls`/`Get-ChildItem`; if a branch switch removed it, switch back or re-run discovery.
  2. If the directory should exist, create it (`mkdir -p`) and re-run, or skip the path if it is no longer relevant.
  3. Avoid running context injection concurrently with git operations or clean tasks that mutate the tree.
  4. Re-run the claude-md command after the filesystem is stable.

Example fix

// before: write attempted on a deleted folder -> throws
// after: ensure the directory exists before writing
if (!existsSync(folderPath)) mkdirSync(folderPath, { recursive: true });
Defensive patterns

Strategy: validation

Validate before calling

import { existsSync, mkdirSync } from 'node:fs';

function ensureFolderForWrite(folderPath: string): void {
  if (!existsSync(folderPath)) {
    mkdirSync(folderPath, { recursive: true });
  }
}
// call ensureFolderForWrite(folderPath) immediately before writeClaudeMdToFolder

Type guard

function isExistingFolder(p: string): boolean {
  try {
    return existsSync(p) && statSync(p).isDirectory();
  } catch {
    return false;
  }
}

Try / catch

try {
  writeClaudeMdToFolder(folderPath, newContent);
} catch (error) {
  if (/Folder does not exist/.test((error as Error).message)) {
    // directory vanished mid-walk; skip silently or recreate and retry
    return;
  }
  throw error;
}

Prevention

When it happens

Trigger: A directory walk collected folderPath, then it was deleted (git checkout/branch switch, clean operation, or external process) before the write reaches it. A symlink that resolved during discovery but whose target was removed. A stale cached path from an earlier run reused after a cleanup.

Common situations: The user runs claude-md context injection while simultaneously switching git branches that remove directories. An editor or build tool purges output folders mid-operation. The folder is on a network mount that disconnected.

Related errors


AI-assisted analysis of thedotmack/claude-mem@d768ba3643 (2026-08-12). Data as JSON: /api/errors/3810c369021646f9. Report an issue: GitHub.