{"record":{"id":"21f15c5aa73ac7f3","repo":"coleam00/Archon","slug":"directory-already-exists-targetpath-no-matchi","errorCode":null,"errorMessage":"Directory already exists: ${targetPath}\n\nNo matching codebase found in database. Remove the directory and re-clone.","messagePattern":"Directory already exists: (.+?)\n\nNo matching codebase found in database\\. Remove the directory and re-clone\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/handlers/clone.ts","lineNumber":350,"sourceCode":"\n    const existingCodebase =\n      (await codebaseDb.findCodebaseByRepoUrl(urlNoGit)) ??\n      (await codebaseDb.findCodebaseByRepoUrl(urlWithGit));\n\n    if (existingCodebase) {\n      return {\n        codebaseId: existingCodebase.id,\n        name: existingCodebase.name,\n        repositoryUrl: existingCodebase.repository_url,\n        defaultCwd: existingCodebase.default_cwd,\n        defaultBranch: existingCodebase.default_branch ?? null,\n        commandCount: 0,\n        alreadyExisted: true,\n      };\n    }\n\n    // Directory exists but no codebase found\n    throw new Error(\n      `Directory already exists: ${targetPath}\\n\\nNo matching codebase found in database. Remove the directory and re-clone.`\n    );\n  }\n\n  // Create project structure (source/, worktrees/, artifacts/, logs/)\n  await ensureProjectStructure(ownerName, repoName);\n\n  getLog().info({ url: workingUrl, targetPath }, 'clone_started');\n\n  // Build clone command with authentication using forge-specific tokens\n  let cloneUrl = workingUrl;\n  const { token: forgeToken, scheme: authScheme } = resolveForgeAuth(workingUrl);\n\n  if (forgeToken) {\n    const parsed = safeParseUrl(workingUrl);\n    if (parsed) {\n      cloneUrl = `https://${authScheme}${forgeToken}@${parsed.hostname}${parsed.pathname}`;\n    } else if (!workingUrl.startsWith('http')) {","sourceCodeStart":332,"sourceCodeEnd":368,"githubUrl":"https://github.com/coleam00/Archon/blob/0773b9745896ef0612e709c80845a0f7db315b19/packages/core/src/handlers/clone.ts#L332-L368","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Remove the existing directory (`rm -rf <targetPath>`) and re-run the clone","If the code is wanted, re-register the existing codebase in the database instead of deleting","Check whether the previous clone attempt left a partial directory and clean it up","Point the clone at a different base path if the existing directory must be kept"],"exampleFix":"// before\nawait cloneRepository({ owner: 'acme', repo: 'api' }); // fails: stale dir\n// after\n// rm -rf /workspace/acme/api\nawait cloneRepository({ owner: 'acme', repo: 'api' });","handlingStrategy":"validation","validationCode":"import { existsSync } from 'node:fs';\nconst targetPath = expectedClonePath(owner, repo);\nif (existsSync(targetPath)) {\n  throw new Error(`Clean up stale clone directory first: ${targetPath}`);\n}","typeGuard":null,"tryCatchPattern":"try { await cloneRepository(args); } catch (e) { if (e.message.includes('Directory already exists')) { await removeStaleDirectory(expectedPath); await cloneRepository(args); } else throw e; }","preventionTips":["Clean up clone directories when resetting or rebuilding the database","Let the tool own the target directory; avoid manually cloning into its workspace","Check for leftover directories after failed clone attempts"],"tags":["git","clone","filesystem","state-conflict"],"backgroundTag":"directory-already-exists","analyzedSha":"0773b9745896ef0612e709c80845a0f7db315b19","analyzedAt":"2026-09-01T02:28:07.064Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}