mastra-ai/mastra · error
Output directory must not be the project root or contain the
Error message
Output directory must not be the project root or contain the Mastra source directory
What it means
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.
Source
Thrown at packages/cli/src/commands/experiment/build.ts:35
}) {
const rootDir = resolve(root || process.cwd());
const mastraDir = dir ? (isAbsolute(dir) ? dir : join(rootDir, dir)) : join(rootDir, 'src', 'mastra');
const outputDirectory = outputDir
? isAbsolute(outputDir)
? resolve(outputDir)
: resolve(rootDir, outputDir)
: join(rootDir, '.mastra', 'experiment-worker');
const logger = createLogger(debug ?? false);
try {
const outputFromRoot = relative(resolve(rootDir), outputDirectory);
const mastraFromOutput = relative(outputDirectory, resolve(mastraDir));
if (
outputFromRoot === '' ||
mastraFromOutput === '' ||
(!mastraFromOutput.startsWith('..') && !isAbsolute(mastraFromOutput))
) {
throw new Error('Output directory must not be the project root or contain the Mastra source directory');
}
const fs = new FileService();
const mastraEntryFile = fs.getFirstExistingFile([join(mastraDir, 'index.ts'), join(mastraDir, 'index.js')]);
const bundler = new ExperimentBundler();
bundler.__setLogger(logger);
await bundler.prepare(outputDirectory);
await bundler.bundle(mastraEntryFile, outputDirectory, { toolsPaths: [], projectRoot: rootDir });
await bundler.writeArtifactManifest(outputDirectory, pkgJson.version);
logger.info(`Experiment worker build complete: ${outputDirectory}`);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
logger.error(`Experiment worker build failed: ${message}`, {
...(error instanceof Error ? { stack: error.stack } : {}),
});
process.exitCode = 1;
}
}View on GitHub (pinned to 75dd419e61)
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.
Example fix
// before mastra experiment build --output . // error: Output directory must not be the project root... // after mastra experiment build --output .mastra/experiment-worker
Defensive patterns
Strategy: validation
Validate before calling
import { isAbsolute, join, relative, resolve } from 'node:path';
function validateOutputDir(root: string, mastraDir: string, outputDirectory: string) {
const outputFromRoot = relative(resolve(root), outputDirectory);
const mastraFromOutput = relative(outputDirectory, resolve(mastraDir));
if (outputFromRoot === '' || mastraFromOutput === '' || (!mastraFromOutput.startsWith('..') && !isAbsolute(mastraFromOutput)))
throw new Error('Choose an output dir outside the project root and not containing src/mastra');
} Type guard
null
Try / catch
try {
await buildExperimentWorker({ dir, root, outputDir });
} catch (e) {
if (String(e?.message).includes('must not be the project root')) {
console.error('Re-run with --output .mastra/experiment-worker');
} else throw e;
} Prevention
- 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
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- .mastra/output/index.mjs not found — did the build succeed?
- .mastra/output/index.mjs not found — did the build succeed?
- Output directory ${outputPath} does not exist
- .mastra/output/index.mjs not found — did the build succeed?
- FAIL_BUILD_SCRIPT
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/1319d7c13fe7cefd.
Report an issue: GitHub.