{"record":{"id":"c53c1d0683f275e2","repo":"mastra-ai/mastra","slug":"could-not-read-file-filepath","errorCode":null,"errorMessage":"Could not read file: ${filePath}","messagePattern":"Could not read file: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/cli/src/commands/server/env.ts","lineNumber":119,"sourceCode":"  }\n\n  delete envVars[key];\n  await updateServerProjectEnv(token, orgId, projectId, envVars);\n\n  console.info(`\\n  Removed ${key} successfully.\\n`);\n}\n\n/* ------------------------------------------------------------------ */\n/*  mastra server env import                                           */\n/* ------------------------------------------------------------------ */\n\nexport async function envImportAction(file: string, opts: { config?: string }) {\n  const filePath = resolve(file);\n  let content: string;\n  try {\n    content = await readFile(filePath, 'utf-8');\n  } catch {\n    throw new Error(`Could not read file: ${filePath}`);\n  }\n\n  const newVars = parseEnvFile(content);\n  const newKeys = Object.keys(newVars);\n  if (newKeys.length === 0) {\n    console.info('\\n  No variables found in file.\\n');\n    return;\n  }\n\n  const { token, orgId } = await resolveAuth();\n  const projectId = await resolveProjectId(opts);\n\n  // Merge with existing env vars (new values override existing)\n  const envVars = await getServerProjectEnv(token, orgId, projectId);\n  Object.assign(envVars, newVars);\n  await updateServerProjectEnv(token, orgId, projectId, envVars);\n\n  console.info(`\\n  Imported ${newKeys.length} variable(s) from ${file}:\\n`);","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/cli/src/commands/server/env.ts#L101-L137","documentation":"`mastra server env import <file>` reads the given env file from disk before parsing and uploading it. Any fs read failure (missing file, permission denied, path is a directory, encoding issues) is wrapped into this generic 'Could not read file: <path>' error, so the underlying errno details are hidden.","triggerScenarios":"Passing a relative path from the wrong cwd; filename typo (e.g. .env.local not committed); passing a directory instead of a file; file unreadable due to permissions; file deleted between tab-completion and execution.","commonSituations":"CI runs from a different working directory than the developer assumed; secrets files excluded by .gitignore so a fresh checkout lacks them; quoting issues dropping part of the path from the shell command.","solutions":["Verify the file exists at the printed absolute path (ls -la) and fix the path/typo","Run the command from the directory containing the env file or pass an absolute path","Check file permissions (chmod u+r) and that it is a regular file, not a directory","Confirm the file is committed/present in CI (not gitignored) before the import step"],"exampleFix":"// before\nmastra server env import .env.prod   # run from wrong cwd, ENOENT\n// after\nmastra server env import \"$(pwd)/.env.prod\"","handlingStrategy":"validation","validationCode":"import { stat } from 'node:fs/promises';\nimport { resolve } from 'node:path';\n// Preflight before `mastra server env import <file>`\nexport async function assertReadableEnvFile(file: string) {\n  const p = resolve(file);\n  const s = await stat(p); // throws with real errno if missing\n  if (!s.isFile()) throw new Error(`Not a file: ${p}`);\n  return p;\n}","typeGuard":null,"tryCatchPattern":"try {\n  await exec(`mastra server env import ${envFile}`);\n} catch (err) {\n  if (err instanceof Error && err.message.startsWith('Could not read file:')) {\n    console.error(`Env file missing/unreadable: ${err.message}. Check cwd and path.`);\n    process.exitCode = 1;\n  } else throw err;\n}","preventionTips":["Pass absolute paths in scripts and CI","Verify the env file exists (and isn't gitignored away) before the import step","Set the correct working directory before running the command","Quote paths in shell commands to avoid truncation"],"tags":["cli","filesystem","env-file"],"backgroundTag":"file-not-found","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}