{"record":{"id":"b48f335d893a1805","repo":"mastra-ai/mastra","slug":"mastra-configuration-initialization-created-or-mod","errorCode":null,"errorMessage":"Mastra configuration initialization created or modified unexpected files in the experiment worker artifact: ${unexpectedOutputFiles.join(', ')}","messagePattern":"Mastra configuration initialization created or modified unexpected files in the experiment worker artifact: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/cli/src/commands/experiment/ExperimentBundler.ts","lineNumber":75,"sourceCode":"    const existingOutputFiles = await collectFileSignatures(outputDirectory);\n    const scratchDirectory = await mkdtemp(join(tmpdir(), 'mastra-experiment-config-'));\n    const originalWorkingDirectory = process.cwd();\n    let bundlerOptions: NonNullable<Config['bundler']>;\n    try {\n      process.chdir(scratchDirectory);\n      bundlerOptions = await super.getUserBundlerOptions(mastraEntryFile, outputDirectory);\n    } finally {\n      process.chdir(originalWorkingDirectory);\n      await rm(scratchDirectory, { recursive: true, force: true });\n    }\n\n    const currentOutputFiles = await collectFileSignatures(outputDirectory);\n    const outputPaths = new Set([...existingOutputFiles.keys(), ...currentOutputFiles.keys()]);\n    const unexpectedOutputFiles = [...outputPaths]\n      .filter(path => path !== 'bundler-config.mjs' && existingOutputFiles.get(path) !== currentOutputFiles.get(path))\n      .sort((left, right) => (left < right ? -1 : left > right ? 1 : 0));\n    if (unexpectedOutputFiles.length > 0) {\n      throw new Error(\n        `Mastra configuration initialization created or modified unexpected files in the experiment worker artifact: ${unexpectedOutputFiles.join(', ')}`,\n      );\n    }\n\n    if (!Array.isArray(bundlerOptions.externals)) return bundlerOptions;\n\n    return {\n      ...bundlerOptions,\n      dynamicPackages: [...new Set([...(bundlerOptions.dynamicPackages ?? []), ...bundlerOptions.externals])],\n    };\n  }\n\n  async bundle(\n    entryFile: string,\n    outputDirectory: string,\n    { projectRoot }: { toolsPaths: (string | string[])[]; projectRoot: string },\n  ): Promise<void> {\n    await this._bundle(this.getEntry(), entryFile, { outputDirectory, projectRoot });","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/cli/src/commands/experiment/ExperimentBundler.ts#L57-L93","documentation":"ExperimentBundler.getUserBundlerOptions snapshots the output directory's file signatures before and after invoking the base Bundler's config initialization (which may run user mastra config code), in an isolated scratch cwd. The artifact must contain only deterministic bundler output, so any created, deleted, or modified file other than `bundler-config.mjs` triggers this error. It detects user config code with destructive side effects (e.g. writing to process.cwd() or the output dir) that would corrupt the reproducible worker artifact.","triggerScenarios":"Running an experiment worker build where the user's mastra configuration file (or code it imports) writes, deletes, or modifies files in the bundler output directory during bundler-options initialization — anything other than bundler-config.mjs.","commonSituations":"mastra.config/index.ts with top-level side effects like fs.writeFileSync, log-file creation, DB migrations, or code generators that emit files at import time; plugins that materialize cache files in cwd; a custom bundler option that touches the output dir.","solutions":["Inspect the listed file path(s) in the message and find what in your mastra config or its imports writes them.","Move file-writing side effects out of module top-level / config initialization into lazy functions called at runtime.","Make any unavoidable writes go to a temp directory, not the project/output directory.","If the file is legitimately expected, ensure it is identical before and after (same content digest) so the signature comparison passes."],"exampleFix":"// before (mastra config top level)\nfs.writeFileSync('./gen/schema.json', buildSchema());\nexport const mastra = new Mastra({...});\n// after\nfunction ensureSchema() { fs.writeFileSync('./gen/schema.json', buildSchema()); }\nexport const mastra = new Mastra({...});  // call ensureSchema() lazily at runtime, not on import","handlingStrategy":"validation","validationCode":"// audit your mastra config entry for import-time side effects before building\nconst suspicious = await grepConfigForWrites('./src/mastra'); // no fs.write*/mkdir/unlink at module top level","typeGuard":"null","tryCatchPattern":"try {\n  await buildExperimentWorker({ dir, root, outputDir, debug });\n} catch (e) {\n  if (String(e?.message).includes('unexpected files in the experiment worker artifact')) {\n    console.error('Remove import-time file writes from your mastra config:', e.message);\n  } else throw e;\n}","preventionTips":["Keep mastra config modules free of top-level fs writes, migrations, and codegen","Defer all side effects into functions invoked at request/runtime time","Write any required generated files to a temp directory outside the project/output dirs","Rebuild from a clean output directory when investigating"],"tags":["build","determinism","side-effects"],"backgroundTag":"unexpected-build-artifact","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}