{"record":{"id":"6aa0890331250b77","repo":"mastra-ai/mastra","slug":"directory-projectname-already-exists","errorCode":null,"errorMessage":"Directory ${projectName} already exists","messagePattern":"Directory (.+?) already exists","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/cli/src/utils/clone-template.ts","lineNumber":54,"sourceCode":"  projectName: string;\n  targetDir?: string;\n  branch?: string;\n  signal?: AbortSignal;\n  silent?: boolean;\n}\n\nexport async function cloneTemplate(options: CloneTemplateOptions): Promise<string> {\n  const { template, projectName, targetDir, branch, signal, silent = false } = options;\n  const projectPath = targetDir ? path.resolve(targetDir, projectName) : path.resolve(projectName);\n\n  const spinner = startSpinner(`Cloning template \"${template.title}\"...`, signal, silent);\n  let ownsProjectPath = false;\n\n  try {\n    // Check if directory already exists\n    if (await directoryExists(projectPath)) {\n      spinner?.error(`Directory ${projectName} already exists`);\n      throw new Error(`Directory ${projectName} already exists`);\n    }\n\n    ownsProjectPath = true;\n\n    // Clone the repository without git history\n    await cloneRepositoryWithoutGit(template.githubUrl, projectPath, branch, signal);\n\n    // Update package.json with new project name\n    await updatePackageJson(projectPath, projectName);\n\n    spinner?.success(`Template \"${template.title}\" cloned successfully to ${projectName}`);\n    return projectPath;\n  } catch (error) {\n    if (ownsProjectPath) {\n      await fs.rm(projectPath, { recursive: true, force: true });\n    }\n    spinner?.error(`Failed to clone template: ${error instanceof Error ? error.message : 'Unknown error'}`);\n    throw error;","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/cli/src/utils/clone-template.ts#L36-L72","documentation":"cloneTemplate refuses to proceed when the destination directory (projectPath/projectName) already exists. It checks directoryExists(projectPath) before cloning and aborts with spinner.error plus this Error to avoid overwriting an existing project. This is a deliberate safety guard so `create-mastra` never clobbers user files.","triggerScenarios":"Running the create-mastra CLI (cloneTemplate) with a --projectName or target path whose directory already exists on disk (including empty ones, since only existence is checked, not emptiness).","commonSituations":"Re-running the scaffolder after a failed/aborted first attempt left a partial directory; choosing a name like 'my-app' that already exists in the cwd; rerunning the command in a repo where the target folder is committed; re-running in a workspace with a pre-created empty folder.","solutions":["Choose a different project name or remove/rename the existing directory before re-running the CLI.","If the existing directory is a leftover partial clone from a failed run, delete it (rm -rf <dir>) and retry.","Run the CLI from a different working directory where the target name is free."],"exampleFix":"// before\n$ npx create-mastra@latest my-app\n// Error: Directory my-app already exists\n\n// after\n$ rm -rf my-app   # or pick a new name\n$ npx create-mastra@latest my-app-v2","handlingStrategy":"validation","validationCode":"import { stat } from 'node:fs/promises';\nasync function directoryExists(p: string): Promise<boolean> {\n  try { return (await stat(p)).isDirectory(); } catch { return false; }\n}\nif (await directoryExists(targetPath)) {\n  throw new Error(`Directory ${projectName} already exists`);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pick a unique project name or check the target path before invoking create-mastra.","Clean up partial directories left by aborted scaffold runs before retrying.","Run the CLI with a fresh directory or pass a distinct --projectName in CI."],"tags":["filesystem","cli","directory-exists","scaffolding"],"backgroundTag":"directory-already-exists","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}