iOfficeAI/OfficeCLI · error · CliException

file_not_found

file_not_found

Error message

Template file not found: {templatePath}

What it means

Thrown by TemplateMerger.Merge when the template file does not exist on disk (File.Exists false). The merger copies the template to the output path before substituting placeholders, so a missing template cannot proceed. Reported as code file_not_found with a suggestion to check the path.

Source

Thrown at src/officecli/Core/TemplateMerger.cs:160

            else if (kvp.Value is JsonArray nestedArr)
            {
                FlattenArray(nestedArr, path, data);
            }
            else if (!data.ContainsKey(path))
            {
                data[path] = kvp.Value?.ToString() ?? "";
            }
        }
    }

    /// <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

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Verify the template path exists (File.Exists) and use an absolute path.
  2. Check the working directory the command runs from.
  3. Confirm the file was not deleted/moved before the merge.

Example fix

// before
var result = TemplateMerger.Merge("tmpl/letter.docx", out, data, false); // missing
// after
var tpath = System.IO.Path.GetFullPath("tmpl/letter.docx");
if (!System.IO.File.Exists(tpath)) throw new FileNotFoundException(tpath);
var result = TemplateMerger.Merge(tpath, out, data, false);
Defensive patterns

Strategy: validation

Validate before calling

if (!System.IO.File.Exists(templatePath))
    throw new System.IO.FileNotFoundException("template not found", templatePath);

Try / catch

try { result = TemplateMerger.Merge(templatePath, outputPath, data, force); }
catch (CliException ex) when (ex.Code == "file_not_found")
{ /* template path is wrong/missing; fix it */ }

Prevention

When it happens

Trigger: Calling merge with a templatePath that does not exist; relative path resolved from an unexpected working directory; the template was moved/deleted.

Common situations: Wrong/relative path; CWD mismatch; template generated into a different location than expected.

Related errors


AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13). Data as JSON: /api/errors/5132b94e9fe805d4. Report an issue: GitHub.