iOfficeAI/OfficeCLI · warning · CliException
file_exists
file_exists
Error message
Output file already exists: {outputPath}. Use --force to overwrite. What it means
Thrown by TemplateMerger.Merge when the output file already exists and force is false. This guard mirrors the create command's overwrite guard and prevents silently clobbering an existing file. Reported as code file_exists with a suggestion to add --force or remove the file.
Source
Thrown at src/officecli/Core/TemplateMerger.cs:169
}
/// <summary>
/// Merge a template document with data. Copies template to output, then replaces placeholders.
/// Refuses to overwrite an existing output unless <paramref name="force"/> is set.
/// </summary>
public static MergeResult Merge(string templatePath, string outputPath, Dictionary<string, string> data, bool force = false)
{
if (!File.Exists(templatePath))
throw new CliException($"Template file not found: {templatePath}")
{
Code = "file_not_found",
Suggestion = "Check the template file path."
};
// Refuse to silently overwrite an existing output unless force is set,
// mirroring the `create` command's guard (CommandBuilder.Import.cs:185).
if (File.Exists(outputPath) && !force)
throw new CliException($"Output file already exists: {outputPath}. Use --force to overwrite.")
{
Code = "file_exists",
Suggestion = "Add --force flag or remove the file first."
};
File.Copy(templatePath, outputPath, overwrite: true);
var ext = Path.GetExtension(outputPath).ToLowerInvariant();
return ext switch
{
".docx" => MergeDocx(outputPath, data),
".xlsx" => MergeXlsx(outputPath, data),
".pptx" => MergePptx(outputPath, data),
_ => throw new CliException($"Unsupported file type for merge: {ext}")
{
Code = "unsupported_type",
ValidValues = [".docx", ".xlsx", ".pptx"]
}View on GitHub (pinned to 1ced45e900)
Solutions
- Pass force=true (CLI --force) to overwrite intentionally.
- Delete or rename the existing output before merging.
- Write to a unique output path per run.
Example fix
// before var r = TemplateMerger.Merge(tpl, out, data, force: false); // out already exists // after var r = TemplateMerger.Merge(tpl, out, data, force: true); // explicit overwrite
Defensive patterns
Strategy: validation
Validate before calling
bool force = overwriteAllowed; // from CLI flag
if (System.IO.File.Exists(outputPath) && !force)
throw new InvalidOperationException($"output exists: {outputPath}; pass --force to overwrite"); Try / catch
try { result = TemplateMerger.Merge(tpl, out, data, force); }
catch (CliException ex) when (ex.Code == "file_exists")
{ /* ask user/agent whether to overwrite, then retry with force=true */ } Prevention
- Use unique output filenames per run to avoid the guard entirely.
- Surface a confirm step in interactive flows before setting force.
When it happens
Trigger: Calling Merge (or a merge command) where outputPath already exists and --force was not passed; re-running a merge into the same output location.
Common situations: Iterating on a template and re-running merge repeatedly; an automation pipeline that reuses the same output filename.
Related errors
AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13).
Data as JSON: /api/errors/d4a94d157b80aa88.
Report an issue: GitHub.