{"record":{"id":"5bb4ac080477ea09","repo":"mastra-ai/mastra","slug":"directory-already-exists-and-is-not-empty-targe","errorCode":null,"errorMessage":"Directory already exists and is not empty: ${targetDir}","messagePattern":"Directory already exists and is not empty: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/sdk/src/plugins/scaffold.ts","lineNumber":35,"sourceCode":"  options: Pick<ScaffoldPluginOptions, 'projectRoot' | 'configDir'> = {},\n): string {\n  if (isBarePluginName(target)) {\n    return path.join(\n      options.projectRoot ?? process.cwd(),\n      options.configDir ?? DEFAULT_CONFIG_DIR,\n      'plugins',\n      'sources',\n      'local',\n      target,\n    );\n  }\n  return path.resolve(options.projectRoot ?? process.cwd(), target);\n}\n\nexport function scaffoldPlugin(targetDir: string, options: ScaffoldPluginOptions = {}): string {\n  const dir = resolveScaffoldTarget(targetDir, options);\n  if (fs.existsSync(dir) && fs.readdirSync(dir).length > 0) {\n    throw new Error(`Directory already exists and is not empty: ${targetDir}`);\n  }\n\n  const packageName =\n    path\n      .basename(dir)\n      .toLowerCase()\n      .replace(/[^a-z0-9_.-]+/g, '-')\n      .replace(/^-|-$/g, '') || 'mastracode-plugin';\n  const pluginId = options.id ?? packageName;\n  const pluginName = options.name ?? humanizeName(packageName);\n\n  fs.mkdirSync(path.join(dir, 'src'), { recursive: true });\n  fs.writeFileSync(\n    path.join(dir, 'package.json'),\n    `${JSON.stringify(\n      {\n        name: packageName,\n        type: 'module',","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/sdk/src/plugins/scaffold.ts#L17-L53","documentation":"scaffoldPlugin refuses to generate a new plugin into an existing, non-empty directory. Because scaffolding writes template files unconditionally, an occupied target directory would be overwritten or mixed with stale content; the guard prevents accidental data loss. The resolved target is projectRoot (default cwd) joined with the given targetDir.","triggerScenarios":"Calling scaffoldPlugin(targetDir) or `mastracode create plugin <dir>` (prepare/createdDir flow) where resolveScaffoldTarget resolves to a path that already exists and fs.readdirSync shows at least one file, including hidden ones like .git or .DS_Store.","commonSituations":"Re-running a scaffold command after a partial/failed first attempt left files behind; scaffolding into a directory that already holds a real plugin; accidentally passing \".\" or the project root as targetDir, which always contains files.","solutions":["Choose a new, unused target directory name and re-run the scaffold command.","If the directory is leftover from a failed scaffold, delete it (verify contents first) and re-run.","If you intended to scaffold there, move the existing files aside, scaffold, then restore what you need.","Pass an explicit projectRoot option if the target resolved relative to the wrong cwd."],"exampleFix":"// before\n$ mastracode plugin create my-plugin\n// Error: Directory already exists and is not empty: my-plugin\n// after — remove leftovers or use a fresh dir\n$ rm -rf my-plugin && mastracode plugin create my-plugin\n// or\n$ mastracode plugin create my-plugin-v2","handlingStrategy":"validation","validationCode":"const target = path.resolve(projectRoot, targetDir);\nif (fs.existsSync(target) && fs.readdirSync(target).length > 0) {\n  throw new Error(`Choose a different scaffold target; ${targetDir} is not empty`);\n}","typeGuard":null,"tryCatchPattern":"try {\n  scaffoldPlugin(targetDir, { projectRoot });\n} catch (err) {\n  if (err instanceof Error && err.message.includes('already exists and is not empty')) {\n    console.error(`Target occupied: ${err.message}. Pick a new name or clear the directory.`);\n  } else throw err;\n}","preventionTips":["Check the target directory is absent or empty before scaffolding (fs.existsSync + readdirSync).","Never pass \".\" or the repository root as targetDir.","Clean up partially scaffolded directories after failed runs before retrying.","Pass an explicit projectRoot so the target resolves where you expect."],"tags":["filesystem","scaffold","conflict"],"backgroundTag":"directory-already-exists","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}