{"record":{"id":"49140fed985d26e3","repo":"mastra-ai/mastra","slug":"multiple-env-files-found-availabledeployenvfile","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":null,"httpStatus":null,"severity":"error","filePath":"packages/cli/src/commands/server/deploy.ts","lineNumber":139,"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":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/cli/src/commands/server/deploy.ts#L121-L157","documentation":"When multiple deployable env files exist and --auto-accept is set, the CLI cannot prompt to choose, so `readEnvVars` throws this error listing all candidates and instructing the use of --env-file. Interactive mode would prompt instead; auto-accept mode must be deterministic.","triggerScenarios":"Running `mastra server deploy --auto-accept` (or a non-interactive pipeline) where getDeployEnvFiles returns 2+ files (.env, .env.production, .env.staging, etc.) and no --env-file is provided.","commonSituations":"CI/CD automation on repos containing several environment files; staging and production env files both present locally.","solutions":["Add `--env-file .env.production` (or the desired file) to the deploy command","Remove or rename the extra env files so only one remains","Drop --auto-accept and choose interactively"],"exampleFix":"// before\nmastra server deploy --auto-accept   # found .env and .env.production\n// after\nmastra server deploy --auto-accept --env-file .env.production","handlingStrategy":"validation","validationCode":"import { readdirSync } from 'node:fs';\nconst envFiles = readdirSync(projectDir).filter(f => f === '.env' || f.startsWith('.env.'));\nif (isCi && envFiles.length > 1 && !opts.envFile) {\n  throw new Error(`Pass --env-file; candidates: ${envFiles.join(', ')}`);\n}","typeGuard":"const hasSingleEnvFile = (dir: string): boolean =>\n  readdirSync(dir).filter(f => f === '.env' || f.startsWith('.env.')).length === 1;","tryCatchPattern":"try {\n  await runServerDeploy(opts);\n} catch (e) {\n  if ((e as Error).message.startsWith('Multiple env files found')) {\n    console.error('Add --env-file <name> to select one deterministically.');\n  } else throw e;\n}","preventionTips":["In non-interactive/CI runs, always pass --env-file explicitly","Keep only one active .env in the project root; archive others","Standardize on .env.production for deploys","Script deploys with an explicit env file argument"],"tags":["env","deploy","cli","ambiguity"],"backgroundTag":"ambiguous-env-file","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}