{"record":{"id":"7ae119b90e93c1f0","repo":"charmbracelet/crush","slug":"target-file-already-exists-and-overwrite-is-not-al","errorCode":null,"errorMessage":"target file already exists and overwrite is not allowed: %s","messagePattern":"target file already exists and overwrite is not allowed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/lsp/util/edit.go","lineNumber":226,"sourceCode":"\n\tif change.RenameFile != nil {\n\t\tvar newPath, oldPath string\n\t\tvar err error\n\n\t\toldPath, err = change.RenameFile.OldURI.Path()\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\tnewPath, err = change.RenameFile.NewURI.Path()\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\tif change.RenameFile.Options != nil {\n\t\t\tif !change.RenameFile.Options.Overwrite {\n\t\t\t\tif _, err := os.Stat(newPath); err == nil {\n\t\t\t\t\treturn fmt.Errorf(\"target file already exists and overwrite is not allowed: %s\", newPath)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif err := os.Rename(oldPath, newPath); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to rename file: %w\", err)\n\t\t}\n\t}\n\n\tif change.TextDocumentEdit != nil {\n\t\ttextEdits := make([]protocol.TextEdit, len(change.TextDocumentEdit.Edits))\n\t\tfor i, edit := range change.TextDocumentEdit.Edits {\n\t\t\tvar err error\n\t\t\ttextEdits[i], err = edit.AsTextEdit()\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"invalid edit type: %w\", err)\n\t\t\t}\n\t\t}\n\t\treturn applyTextEdits(change.TextDocumentEdit.TextDocument.URI, textEdits, encoding)","sourceCodeStart":208,"sourceCodeEnd":244,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/lsp/util/edit.go#L208-L244","documentation":"applyDocumentChange in internal/lsp/util/edit.go refuses to apply an LSP rename when the destination path already exists and the RenameFile options do not set Overwrite. This guards against silently destroying an existing file when a rename is applied. It is thrown before os.Rename executes, so no data loss occurs.","triggerScenarios":"A server returns a WorkspaceEdit containing a RenameFile whose Options.Overwrite is false/nil, and os.Stat(newPath) succeeds because the target path already exists on disk.","commonSituations":"Renaming a file to a name that already exists in the project (e.g. rename file.go to main.go when main.go exists); stale editor state where the target was created after the edit was computed; servers that omit OverwriteIgnoringCapability defaults.","solutions":["Delete or move the existing target file, then retry the rename","Have the server set RenameFile Options.Overwrite=true if overwriting is intended (usually gated on client capability)","Pick a different target filename","If you control the client capabilities, advertise overwrite support so servers set Overwrite appropriately"],"exampleFix":"// before\nif !change.RenameFile.Options.Overwrite {\n    if _, err := os.Stat(newPath); err == nil {\n        return fmt.Errorf(\"target file already exists and overwrite is not allowed: %s\", newPath)\n    }\n}\n// after (server-side, when overwrite is intended)\nchange.RenameFile.Options = &protocol.RenameFileOptions{Overwrite: true}","handlingStrategy":"validation","validationCode":"func canApplyRename(newPath string, opts *protocol.RenameFileOptions) error {\n    if opts != nil && opts.Overwrite {\n        return nil\n    }\n    if _, err := os.Stat(newPath); err == nil {\n        return fmt.Errorf(\"target exists: %s\", newPath)\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"if err := ApplyWorkspaceEdit(edit, enc); err != nil && strings.Contains(err.Error(), \"overwrite is not allowed\") {\n    // prompt user to overwrite or choose another name\n}","preventionTips":["os.Stat the destination before requesting/applying a rename edit","Advertise overwrite capability if your client supports it so servers set Overwrite","Prompt the user for confirmation whenever a rename target exists"],"tags":["lsp","filesystem","rename","workspace-edit"],"backgroundTag":"rename-target-exists","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}