{"record":{"id":"bbfa9b5642f99429","repo":"iOfficeAI/OfficeCLI","slug":"file-exists","errorCode":"file_exists","errorMessage":"File already exists: {file}. Use --force to overwrite.","messagePattern":"File already exists: (.+?)\\. Use --force to overwrite\\.","errorType":"exception","errorClass":"CliException","httpStatus":null,"severity":"error","filePath":"src/officecli/CommandBuilder.Import.cs","lineNumber":191,"sourceCode":"                {\n                    ResidentClient.SendClose(fullPath);\n                }\n                else\n                {\n                    throw new CliException($\"{Path.GetFileName(file)} is currently opened by a resident process. Please run 'officecli close \\\"{file}\\\"' first.\")\n                    {\n                        Code = \"file_locked\",\n                        Suggestion = $\"Run: officecli close \\\"{file}\\\"\"\n                    };\n                }\n            }\n\n            // Refuse to silently overwrite an existing file unless --force is set.\n            // OpenXML SDK's Create truncates the target otherwise, which can destroy\n            // user data when an AI agent retries or mis-types the path.\n            if (File.Exists(fullPath) && !force)\n            {\n                throw new CliException($\"File already exists: {file}. Use --force to overwrite.\")\n                {\n                    Code = \"file_exists\",\n                    Suggestion = \"Add --force flag or remove the file first.\"\n                };\n            }\n            if (File.Exists(fullPath) && force)\n            {\n                Console.Error.WriteLine($\"Overwriting existing file: {file}\");\n            }\n\n            OfficeCli.BlankDocCreator.Create(file, locale, minimal);\n            var fullCreatedPath = Path.GetFullPath(file);\n\n            // If a --force overwrite replaced a file that currently has a live\n            // watch session, push a full SSE refresh so the preview reflects the\n            // new (blank) document instead of the stale pre-overwrite content\n            // (issue #169). create replaces the whole file, so a full re-render\n            // is the only correct shape — mirrors swap / refresh. Only reachable","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/iOfficeAI/OfficeCLI/blob/1ced45e900782c5083ed550ddf328ee974e425e7/src/officecli/CommandBuilder.Import.cs#L173-L209","documentation":"The target file already exists and --force was not given. Import (and create) refuse to overwrite because the underlying OpenXML SDK Create truncates the target, which would silently destroy data on an agent retry or a mistyped path. --force opts in explicitly.","triggerScenarios":"'officecli import existing.xlsx --file data.csv' where existing.xlsx exists and no --force; re-running a script that already produced the file.","commonSituations":"An agent retrying an import that partially completed; a script re-run without cleanup; pointing import at an important existing workbook by mistake.","solutions":["Add --force if overwriting is intended.","Or remove/back up the existing file first.","Make scripts idempotent: clean the target before import."],"exampleFix":"// before\nofficecli import out.xlsx --file data.csv   # out.xlsx exists\n// after\nofficecli import out.xlsx --file data.csv --force","handlingStrategy":"validation","validationCode":"if (File.Exists(target) && !force)\n    throw new IOException(\n        $\"Refusing to overwrite '{target}'; pass force: true or delete it.\");","typeGuard":null,"tryCatchPattern":"// Catch file_exists and decide: force-overwrite or skip.\ntry { RunImport(target, src, force: false); }\ncatch (CliException e) when (e.Code == \"file_exists\") {\n    RunImport(target, src, force: true);\n}","preventionTips":["Decide overwrite policy up front and pass --force intentionally.","Keep scripts idempotent (clean target first) to avoid the ambiguous retry.","Never blanket --force without understanding it truncates."],"tags":["file-io","import","overwrite-protection","file-exists"],"backgroundTag":null,"analyzedSha":"1ced45e900782c5083ed550ddf328ee974e425e7","analyzedAt":"2026-08-13T13:01:07.193Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}