{"record":{"id":"1319d7c13fe7cefd","repo":"mastra-ai/mastra","slug":"output-directory-must-not-be-the-project-root-or-c","errorCode":null,"errorMessage":"Output directory must not be the project root or contain the Mastra source directory","messagePattern":"Output directory must not be the project root or contain the Mastra source directory","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/cli/src/commands/experiment/build.ts","lineNumber":35,"sourceCode":"}) {\n  const rootDir = resolve(root || process.cwd());\n  const mastraDir = dir ? (isAbsolute(dir) ? dir : join(rootDir, dir)) : join(rootDir, 'src', 'mastra');\n  const outputDirectory = outputDir\n    ? isAbsolute(outputDir)\n      ? resolve(outputDir)\n      : resolve(rootDir, outputDir)\n    : join(rootDir, '.mastra', 'experiment-worker');\n  const logger = createLogger(debug ?? false);\n\n  try {\n    const outputFromRoot = relative(resolve(rootDir), outputDirectory);\n    const mastraFromOutput = relative(outputDirectory, resolve(mastraDir));\n    if (\n      outputFromRoot === '' ||\n      mastraFromOutput === '' ||\n      (!mastraFromOutput.startsWith('..') && !isAbsolute(mastraFromOutput))\n    ) {\n      throw new Error('Output directory must not be the project root or contain the Mastra source directory');\n    }\n\n    const fs = new FileService();\n    const mastraEntryFile = fs.getFirstExistingFile([join(mastraDir, 'index.ts'), join(mastraDir, 'index.js')]);\n    const bundler = new ExperimentBundler();\n    bundler.__setLogger(logger);\n    await bundler.prepare(outputDirectory);\n    await bundler.bundle(mastraEntryFile, outputDirectory, { toolsPaths: [], projectRoot: rootDir });\n    await bundler.writeArtifactManifest(outputDirectory, pkgJson.version);\n    logger.info(`Experiment worker build complete: ${outputDirectory}`);\n  } catch (error) {\n    const message = error instanceof Error ? error.message : String(error);\n    logger.error(`Experiment worker build failed: ${message}`, {\n      ...(error instanceof Error ? { stack: error.stack } : {}),\n    });\n    process.exitCode = 1;\n  }\n}","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/cli/src/commands/experiment/build.ts#L17-L53","documentation":"buildExperimentWorker validates the output directory before bundling: it must not be the project root itself, and the Mastra source directory (src/mastra by default, or --dir) must not be inside it. Otherwise the bundler would recursively consume the source it is compiling (and destroy/overwrite files in place), producing a broken or self-clobbering artifact. The build aborts with this error.","triggerScenarios":"Running the experiment worker build with `--output` equal to the project root (relative(outputDirectory, mastraDir) === '' case or outputFromRoot === ''), or with an output directory that contains the mastra `dir` (mastraFromOutput does not start with '..' and is not absolute).","commonSituations":"Passing `--output .` or `--output /path/to/project`; setting outputDir to a parent folder like `--output ./src` which contains src/mastra; a config that computes outputDir = rootDir.","solutions":["Use the default output directory (.mastra/experiment-worker) by omitting --output.","Pass a dedicated output path outside the source tree, e.g. `--output .mastra/experiment-worker` or `--output dist/experiment-worker`.","If --dir points at a custom mastra folder, ensure the output directory does not contain that folder.","Fix scripts/CI that interpolate an empty or root value into --output."],"exampleFix":"// before\nmastra experiment build --output .\n// error: Output directory must not be the project root...\n// after\nmastra experiment build --output .mastra/experiment-worker","handlingStrategy":"validation","validationCode":"import { isAbsolute, join, relative, resolve } from 'node:path';\nfunction validateOutputDir(root: string, mastraDir: string, outputDirectory: string) {\n  const outputFromRoot = relative(resolve(root), outputDirectory);\n  const mastraFromOutput = relative(outputDirectory, resolve(mastraDir));\n  if (outputFromRoot === '' || mastraFromOutput === '' || (!mastraFromOutput.startsWith('..') && !isAbsolute(mastraFromOutput)))\n    throw new Error('Choose an output dir outside the project root and not containing src/mastra');\n}","typeGuard":"null","tryCatchPattern":"try {\n  await buildExperimentWorker({ dir, root, outputDir });\n} catch (e) {\n  if (String(e?.message).includes('must not be the project root')) {\n    console.error('Re-run with --output .mastra/experiment-worker');\n  } else throw e;\n}","preventionTips":["Omit --output and use the default .mastra/experiment-worker","Never set outputDir to the repo root or any ancestor of src/mastra","Validate interpolated --output values in CI scripts","Keep mastra source under src/mastra and outputs under .mastra or dist"],"tags":["build","cli","path-validation"],"backgroundTag":"invalid-output-directory","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}