charmbracelet/crush · error

target file already exists and overwrite is not allowed: %s

Error message

target file already exists and overwrite is not allowed: %s

What it means

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.

Source

Thrown at internal/lsp/util/edit.go:226

	if change.RenameFile != nil {
		var newPath, oldPath string
		var err error

		oldPath, err = change.RenameFile.OldURI.Path()
		if err != nil {
			return err
		}

		newPath, err = change.RenameFile.NewURI.Path()
		if err != nil {
			return err
		}

		if change.RenameFile.Options != nil {
			if !change.RenameFile.Options.Overwrite {
				if _, err := os.Stat(newPath); err == nil {
					return fmt.Errorf("target file already exists and overwrite is not allowed: %s", newPath)
				}
			}
		}
		if err := os.Rename(oldPath, newPath); err != nil {
			return fmt.Errorf("failed to rename file: %w", err)
		}
	}

	if change.TextDocumentEdit != nil {
		textEdits := make([]protocol.TextEdit, len(change.TextDocumentEdit.Edits))
		for i, edit := range change.TextDocumentEdit.Edits {
			var err error
			textEdits[i], err = edit.AsTextEdit()
			if err != nil {
				return fmt.Errorf("invalid edit type: %w", err)
			}
		}
		return applyTextEdits(change.TextDocumentEdit.TextDocument.URI, textEdits, encoding)

View on GitHub (pinned to 7944b8e522)

Solutions

  1. Delete or move the existing target file, then retry the rename
  2. Have the server set RenameFile Options.Overwrite=true if overwriting is intended (usually gated on client capability)
  3. Pick a different target filename
  4. If you control the client capabilities, advertise overwrite support so servers set Overwrite appropriately

Example fix

// before
if !change.RenameFile.Options.Overwrite {
    if _, err := os.Stat(newPath); err == nil {
        return fmt.Errorf("target file already exists and overwrite is not allowed: %s", newPath)
    }
}
// after (server-side, when overwrite is intended)
change.RenameFile.Options = &protocol.RenameFileOptions{Overwrite: true}
Defensive patterns

Strategy: validation

Validate before calling

func canApplyRename(newPath string, opts *protocol.RenameFileOptions) error {
    if opts != nil && opts.Overwrite {
        return nil
    }
    if _, err := os.Stat(newPath); err == nil {
        return fmt.Errorf("target exists: %s", newPath)
    }
    return nil
}

Try / catch

if err := ApplyWorkspaceEdit(edit, enc); err != nil && strings.Contains(err.Error(), "overwrite is not allowed") {
    // prompt user to overwrite or choose another name
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of charmbracelet/crush@7944b8e522 (2026-08-29). Data as JSON: /api/errors/7ae119b90e93c1f0. Report an issue: GitHub.