Skip to content

Nested directory creation when working directory is already in subdirectory #814

@sonyschan

Description

@sonyschan

Description

When the current working directory (cwd) is already inside a subdirectory (e.g., frontend/), and claude-mem receives a relative file path like frontend/src/components/Header.jsx, it creates nested duplicate directories such as:

  • frontend/frontend/src/components/
  • backend/backend/services/
  • trencherrpg/mayor/rig/frontend/ (recreating the entire path structure)

These directories only contain empty CLAUDE.md files with "No recent activity".

Steps to Reproduce

  1. Navigate to a project subdirectory: cd /path/to/project/frontend
  2. Perform file operations (Edit, Write) on files using relative paths that include the current directory name
  3. Observe nested directories being created: frontend/frontend/, frontend/mayor/, etc.

Expected Behavior

claude-mem should either:

  1. Detect and reject paths that would create duplicate directory segments
  2. Normalize paths to prevent duplication before calling mkdirSync

Actual Behavior

mkdirSync(folderPath, { recursive: true }) creates nested duplicate directories because the path validation in isValidPathForClaudeMd() doesn't check for duplicate path segments.

Suggested Fix

Add duplicate segment detection in claude-md-utils.ts:

function hasDuplicatePathSegments(resolvedPath: string): boolean {
  const segments = resolvedPath.split(path.sep).filter(s => s && s !== '.' && s !== '..');
  const seen = new Set<string>();
  for (const segment of segments) {
    if (seen.has(segment)) return true;
    seen.add(segment);
  }
  return false;
}

// In isValidPathForClaudeMd(), add:
if (hasDuplicatePathSegments(resolved)) {
  return false;
}

Alternatively, compare the resolved path against common patterns like /frontend/frontend/, /backend/backend/, /src/src/ etc.

Environment

  • claude-mem version: 9.0.2
  • OS: macOS
  • Claude Code version: 2.1.19

Workaround

Manually delete the orphan directories:

find . -type d \( -path "*/frontend/frontend" -o -path "*/backend/backend" \) -exec rm -rf {} + 2>/dev/null

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions