{"record":{"id":"b87a252f6efad3e9","repo":"mastra-ai/mastra","slug":"refusing-to-overwrite-target-re-run-with-for","errorCode":null,"errorMessage":"Refusing to overwrite ${target}. Re-run with --force to replace it.","messagePattern":"Refusing to overwrite (.+?)\\. Re-run with --force to replace it\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/cli/src/commands/env/vars.ts","lineNumber":93,"sourceCode":"  // vars first, project-scoped vars override on conflict.\n  const merged = { ...(environment.envVars ?? {}), ...projectVars };\n\n  const { content, written, skipped } = serializeEnvFile(merged, {\n    header: `Pulled from Mastra environment ${environment.slug} — do not edit manually`,\n    managedVarNames: environment.managedEnvVarNames,\n  });\n\n  const target = options.output ?? '.env';\n  const outputPath = resolve(target);\n  try {\n    await writeFile(outputPath, content, {\n      encoding: 'utf-8',\n      mode: 0o600,\n      flag: options.force ? 'w' : 'wx',\n    });\n  } catch (error) {\n    if (isAlreadyExistsError(error)) {\n      throw new Error(`Refusing to overwrite ${target}. Re-run with --force to replace it.`);\n    }\n    throw error;\n  }\n  await chmod(outputPath, 0o600);\n\n  if (written === 0) {\n    console.info(`\\n  No env vars set on ${environment.slug}. Wrote empty ${target}.\\n`);\n  } else {\n    console.info(\n      `\\n  Pulled ${written} variable(s) from ${environment.slug} to ${target}.${skipped > 0 ? ` Skipped ${skipped} unsafe key(s).` : ''}\\n`,\n    );\n  }\n  const managedCount = environment.managedEnvVarNames?.length ?? 0;\n  if (managedCount > 0) {\n    console.info(\n      `  ${managedCount} managed variable name(s) listed as comments — values are injected at deploy time.\\n`,\n    );\n  }","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/cli/src/commands/env/vars.ts#L75-L111","documentation":"`mastra env vars pull` writes the output file with flag 'wx' (fail if exists) unless --force is given, so an existing `.env` (or -o target) is never silently clobbered. When the write fails with EEXIST, the CLI converts it into this explicit refusal. It is a safety guard against destroying local env files that may contain uncommitted local values.","triggerScenarios":"Running `mastra env vars pull` when the output file (default `.env`, or the --output path) already exists on disk and the `--force` flag was not passed.","commonSituations":"Pulling into a project that already has a hand-maintained `.env`; re-running a pull script twice; CI or scripts re-invoking the command on a checked-out repo that ships a `.env.example`-style committed `.env`.","solutions":["Re-run with `--force` to deliberately replace the file: `mastra env vars pull production --force`.","Use `-o/--output` to write to a different file, e.g. `mastra env vars pull -o .env.pulled`, then diff manually.","Back up the existing file before forcing: `cp .env .env.bak` then re-run with --force.","In automation, check existence first and decide programmatically whether to pass --force."],"exampleFix":"// before\nmastra env vars pull production\n// error: Refusing to overwrite .env ...\n// after\nmastra env vars pull production --force","handlingStrategy":"validation","validationCode":"import { existsSync } from 'node:fs';\nif (existsSync(outputPath) && !options.force) {\n  throw new Error(`${outputPath} exists; pass --force or choose another -o path`);\n}","typeGuard":"null","tryCatchPattern":"try {\n  await envVarsPullAction(envArg, options);\n} catch (e) {\n  if (e instanceof Error && e.message.includes('Refusing to overwrite')) {\n    // prompt user or retry with force\n  } else throw e;\n}","preventionTips":["Treat .env as machine-generated once you start pulling; keep hand-edits in .env.local","Always diff existing output against a backup before using --force","In CI, write to a fresh temp file and move it into place","Decide the overwrite policy upfront in automation scripts"],"tags":["cli","filesystem","safety-guard"],"backgroundTag":"file-already-exists","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}