{"record":{"id":"4b08bd3a5deca549","repo":"mastra-ai/mastra","slug":"env-file-not-found-options-envfile","errorCode":null,"errorMessage":"Env file not found: ${options.envFile}","messagePattern":"Env file not found: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/cli/src/commands/server/deploy.ts","lineNumber":122,"sourceCode":"        (entry.isFile() || entry.isSymbolicLink()) &&\n        (entry.name === '.env' || entry.name.startsWith('.env.')) &&\n        !entry.name.endsWith('.example'),\n    )\n    .map(entry => entry.name)\n    .sort((a, b) => a.localeCompare(b));\n}\n\nexport async function readEnvVars(\n  projectDir: string,\n  options: { autoAccept?: boolean; envFile?: string } = {},\n): Promise<Record<string, string>> {\n  // When an explicit env file is provided, trust the user — read it directly.\n  if (options.envFile) {\n    const filePath = join(projectDir, options.envFile);\n    try {\n      await access(filePath);\n    } 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.`,","sourceCodeStart":104,"sourceCodeEnd":140,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/cli/src/commands/server/deploy.ts#L104-L140","documentation":"`readEnvVars` validates that an explicitly provided env file exists before parsing it. When --env-file (options.envFile) is given, the CLI checks file accessibility with fs.access and throws this error naming the file if it cannot be reached. Explicitly provided files are trusted as-is, so a missing file is a hard error rather than a silent fallback.","triggerScenarios":"Running `mastra server deploy --env-file <path>` where <path> (resolved against projectDir) does not exist or is not accessible.","commonSituations":"Typo in the --env-file value; running the deploy from a different working directory than expected; env file deleted or gitignored and absent on CI.","solutions":["Create the env file at the specified path relative to projectDir","Correct the --env-file path or typo","Run the command from the project root so the relative path resolves correctly"],"exampleFix":"// before\nmastra server deploy --env-file .env.porduction\n// after\nmastra server deploy --env-file .env.production","handlingStrategy":"validation","validationCode":"import { accessSync, constants } from 'node:fs';\nconst filePath = join(projectDir, options.envFile);\ntry {\n  accessSync(filePath, constants.F_OK);\n} catch {\n  throw new Error(`--env-file '${options.envFile}' not found relative to ${projectDir}`);\n}","typeGuard":"const envFileExists = (dir: string, name: string): boolean => {\n  try { accessSync(join(dir, name), constants.F_OK); return true; } catch { return false; }\n};","tryCatchPattern":"try {\n  await runServerDeploy(opts);\n} catch (e) {\n  if ((e as Error).message.startsWith('Env file not found')) {\n    console.error('Fix --env-file path relative to the project directory.');\n  } else throw e;\n}","preventionTips":["Always pass --env-file relative to the project root","Double-check spelling of env file names","Create the env file before running deploy in CI","Run deploys from the project directory"],"tags":["env","filesystem","cli","deploy"],"backgroundTag":"env-file-not-found","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}