{"record":{"id":"661605c91bffdb48","repo":"mastra-ai/mastra","slug":"multiple-env-files-found-availabledeployenvfile-661605","errorCode":null,"errorMessage":"Multiple env files found: ${availableDeployEnvFiles.join(', ')}. Use --env-file to specify which one to deploy.","messagePattern":"Multiple env files found: (.+?)\\. Use --env-file to specify which one to deploy\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/cli/src/commands/studio/deploy.ts","lineNumber":162,"sourceCode":"    } catch {\n      throw new Error(`Env file not found: ${options.envFile}`);\n    }\n    p.log.step(`Using env file: ${options.envFile}`);\n    return parseEnvFile(await readFile(filePath, 'utf-8'));\n  }\n\n  const availableDeployEnvFiles = await getDeployEnvFiles(projectDir);\n\n  if (availableDeployEnvFiles.length === 0) {\n    throw new Error('No env file found for deploy. Add a .env or .env.* file before deploying.');\n  }\n\n  let selectedEnvFile: string;\n\n  if (availableDeployEnvFiles.length === 1) {\n    selectedEnvFile = availableDeployEnvFiles[0]!;\n  } else if (options.autoAccept) {\n    throw new Error(\n      `Multiple env files found: ${availableDeployEnvFiles.join(', ')}. Use --env-file to specify which one to deploy.`,\n    );\n  } else {\n    const defaultFile =\n      availableDeployEnvFiles.find(envFile => envFile === '.env.production') ?? availableDeployEnvFiles[0]!;\n\n    const selected = await p.select({\n      message: 'Choose env file to deploy',\n      options: availableDeployEnvFiles.map(envFile => ({ value: envFile, label: envFile })),\n      initialValue: defaultFile,\n    });\n\n    if (p.isCancel(selected)) {\n      p.cancel('Deploy cancelled.');\n      process.exit(0);\n    }\n\n    selectedEnvFile = selected as string;","sourceCodeStart":144,"sourceCodeEnd":180,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/cli/src/commands/studio/deploy.ts#L144-L180","documentation":"The `mastra studio deploy` command automatically detects env files (.env, .env.production, etc.) in the target directory. When more than one candidate env file exists, the command refuses to guess which one to upload in non-interactive mode and throws, telling you to disambiguate with --env-file. Interactive runs instead prompt with a default of .env.production.","triggerScenarios":"Running `mastra studio deploy --yes` (or with MASTRA_API_TOKEN set, which forces autoAccept) in a directory where readEnvVars finds two or more deploy env files (e.g. both .env and .env.production).","commonSituations":"Repos that keep a local .env plus a .env.production for staging; CI runners where a prior step generated .env.production alongside a committed .env; switching from dev to prod deploys without cleaning up old env files.","solutions":["Pass --env-file explicitly, e.g. `mastra studio deploy --env-file .env.production`","Remove or rename the env files you do not want deployed so only one candidate remains","Run without --yes and pick the file interactively","In CI, ensure only the intended env file exists in the build directory before deploying"],"exampleFix":"// before\nmastra studio deploy --yes\n// Error: Multiple env files found: .env, .env.production\n// after\nmastra studio deploy --yes --env-file .env.production","handlingStrategy":"validation","validationCode":"import { existsSync } from 'node:fs';\nconst candidates = ['.env', '.env.production', '.env.local'].map(f => join(dir, f)).filter(f => existsSync(f));\nif (candidates.length > 1) {\n  throw new Error(`Ambiguous env files: ${candidates.join(', ')}. Pass --env-file.`);\n}","typeGuard":"function hasSingleEnvFile(files: string[]): files is [string] {\n  return files.length === 1;\n}","tryCatchPattern":"try {\n  await deploy({ envFile: process.env.DEPLOY_ENV_FILE });\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith('Multiple env files found')) {\n    console.error('Disambiguate with --env-file; candidates:', listEnvFiles(dir));\n    process.exitCode = 2;\n  } else throw e;\n}","preventionTips":["Keep exactly one deploy env file in the build directory; generate .env.production only in prod pipelines","Standardize on --env-file in CI scripts instead of relying on auto-detection","Add a pre-deploy lint step that fails on multiple env files","Document which env file is canonical for deploys in the repo README"],"tags":["cli","env-files","configuration"],"backgroundTag":"ambiguous-env-file","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}