coleam00/Archon · error

Directory already exists: ${targetPath} No matching codebas

Error message

Directory already exists: ${targetPath}

No matching codebase found in database. Remove the directory and re-clone.

What it means

cloneRepository refuses to proceed when the target clone directory already exists on disk but the database has no codebase record matching it — the state is inconsistent (stale or foreign directory). It tells you to remove the directory and re-clone.

Source

Thrown at packages/core/src/handlers/clone.ts:350

    const existingCodebase =
      (await codebaseDb.findCodebaseByRepoUrl(urlNoGit)) ??
      (await codebaseDb.findCodebaseByRepoUrl(urlWithGit));

    if (existingCodebase) {
      return {
        codebaseId: existingCodebase.id,
        name: existingCodebase.name,
        repositoryUrl: existingCodebase.repository_url,
        defaultCwd: existingCodebase.default_cwd,
        defaultBranch: existingCodebase.default_branch ?? null,
        commandCount: 0,
        alreadyExisted: true,
      };
    }

    // Directory exists but no codebase found
    throw new Error(
      `Directory already exists: ${targetPath}\n\nNo matching codebase found in database. Remove the directory and re-clone.`
    );
  }

  // Create project structure (source/, worktrees/, artifacts/, logs/)
  await ensureProjectStructure(ownerName, repoName);

  getLog().info({ url: workingUrl, targetPath }, 'clone_started');

  // Build clone command with authentication using forge-specific tokens
  let cloneUrl = workingUrl;
  const { token: forgeToken, scheme: authScheme } = resolveForgeAuth(workingUrl);

  if (forgeToken) {
    const parsed = safeParseUrl(workingUrl);
    if (parsed) {
      cloneUrl = `https://${authScheme}${forgeToken}@${parsed.hostname}${parsed.pathname}`;
    } else if (!workingUrl.startsWith('http')) {

View on GitHub (pinned to 0773b97458)

Solutions

  1. Remove the existing directory (`rm -rf <targetPath>`) and re-run the clone
  2. If the code is wanted, re-register the existing codebase in the database instead of deleting
  3. Check whether the previous clone attempt left a partial directory and clean it up
  4. Point the clone at a different base path if the existing directory must be kept

Example fix

// before
await cloneRepository({ owner: 'acme', repo: 'api' }); // fails: stale dir
// after
// rm -rf /workspace/acme/api
await cloneRepository({ owner: 'acme', repo: 'api' });
Defensive patterns

Strategy: validation

Validate before calling

import { existsSync } from 'node:fs';
const targetPath = expectedClonePath(owner, repo);
if (existsSync(targetPath)) {
  throw new Error(`Clean up stale clone directory first: ${targetPath}`);
}

Try / catch

try { await cloneRepository(args); } catch (e) { if (e.message.includes('Directory already exists')) { await removeStaleDirectory(expectedPath); await cloneRepository(args); } else throw e; }

Prevention

When it happens

Trigger: Calling cloneRepository for a repo whose targetPath directory exists (from a prior partial clone or manual checkout) while the codebase lookup finds no matching row in the database.

Common situations: Database was reset or recreated while old clone directories remained; previous clone failed midway after mkdir; repo cloned manually outside the tool; project renamed/re-registered under a different identity.

Related errors


AI-assisted analysis of coleam00/Archon@0773b97458 (2026-09-01). Data as JSON: /api/errors/21f15c5aa73ac7f3. Report an issue: GitHub.