{"record":{"id":"45f96a7e8f2d7058","repo":"mastra-ai/mastra","slug":"env-file-not-found-options-envfile-45f96a","errorCode":null,"errorMessage":"Env file not found: ${options.envFile}","messagePattern":"Env file not found: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/cli/src/commands/studio/deploy.ts","lineNumber":145,"sourceCode":"        (entry.name === '.env' || entry.name.startsWith('.env.')) &&\n        !entry.name.endsWith('.example') &&\n        entry.name !== '.env.schema',\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":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/cli/src/commands/studio/deploy.ts#L127-L163","documentation":"Thrown by readEnvVars in the studio deploy command when an explicit env file is passed via options (e.g., `--env-file <path>`) but the file cannot be accessed (fs.access fails) relative to the project directory. The CLI trusts the user's explicit path and errors immediately rather than falling back to discovery.","triggerScenarios":"Running `mastra studio deploy --env-file <path>` where the path is misspelled, relative to the wrong directory (it's resolved against projectDir, not cwd), points outside the project, or the file was deleted/not committed.","commonSituations":"Using `.env.production` without creating it; passing a path relative to cwd while the CLI resolves it relative to the project root; gitignored env file missing on a CI machine; typo in the filename.","solutions":["Verify the file exists relative to the project directory (not your shell's cwd): `ls <projectDir>/<envFile>`.","Pass a path relative to the project root or use an absolute path for --env-file.","Create the env file or copy it from a template before deploying.","If CI, ensure the env file is generated/uploaded as a build artifact before the deploy step."],"exampleFix":"// before\nmastra studio deploy --env-file .env.prod   // typo\n// after\nmastra studio deploy --env-file .env.production","handlingStrategy":"validation","validationCode":"import { access } from 'node:fs/promises';\nimport { join, isAbsolute } from 'node:path';\nconst p = isAbsolute(envFile) ? envFile : join(projectDir, envFile);\ntry {\n  await access(p);\n} catch {\n  throw new Error(`--env-file not found at ${p} (resolved against project dir ${projectDir})`);\n}","typeGuard":null,"tryCatchPattern":"try {\n  await runStudioDeploy(options);\n} catch (err) {\n  if ((err as Error).message.startsWith('Env file not found:')) {\n    console.error(`${err.message} — note the path is resolved relative to the project directory.`);\n    return;\n  }\n  throw err;\n}","preventionTips":["Remember --env-file paths resolve against the project directory, not your shell cwd.","Keep the env file in the repo root or pass an absolute path.","Generate env files in CI before the deploy step (secrets are not auto-materialized).","Double-check filenames for typos (.env.production vs .env.prod)."],"tags":["cli","filesystem","env","deploy"],"backgroundTag":"env-file-not-found","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}