wavetermdev/waveterm · error

file already exists

Error message

file %q already exists

What it means

Returned when the target file already exists and the operation does not overwrite existing files. The path is quoted via %q.

Solutions

  1. Check existence first with RemoteFileInfoCommand and skip touch if present.
  2. Delete the existing file if overwrite is intended (RemoteFileDeleteCommand) then touch.
  3. Use a different destination path or add a timestamp suffix.

Example fix

// before
wshclient.RemoteFileTouchCommand(ctx, p, nil)
// after
if _, err := wshclient.RemoteFileInfoCommand(ctx, p, nil); errors.Is(err, fs.ErrNotExist) {
    wshclient.RemoteFileTouchCommand(ctx, p, nil)
}
Defensive patterns

Strategy: validation

Validate before calling

if _, err := wshclient.RemoteFileInfoCommand(ctx, path, nil); err == nil {
    return nil // already exists, skip touch
}

Try / catch

err := wshclient.RemoteFileTouchCommand(ctx, path, nil)
if err != nil && strings.Contains(err.Error(), "already exists") {
    return nil // idempotent touch
}

Prevention

When it happens

Trigger: Calling RemoteFileTouchCommand with a path that already exists as a file, directory, or symlink.

Common situations: Race where another client created the file first; re-running a script twice; assuming Unix touch semantics (which updates mtime instead of erroring).

Understand the failure class

Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.

Related errors


AI-assisted analysis of wavetermdev/waveterm@a4447c1563 (2026-09-01). Data as JSON: /api/errors/c6fc25ba5d917c63. Report an issue: GitHub.

Appendix: source

Thrown at pkg/wshrpc/wshremote/wshremote_file.go:421

		if err != nil {
			rtn[path] = wshrpc.FileInfo{
				Path:          wavebase.ReplaceHomeDir(cleanedPath),
				Dir:           computeDirPart(cleanedPath),
				Name:          filepath.Base(cleanedPath),
				StatError:     err.Error(),
				SupportsMkdir: true,
			}
			continue
		}
		rtn[path] = *fileInfo
	}
	return rtn, nil
}

func (impl *ServerImpl) RemoteFileTouchCommand(ctx context.Context, path string) error {
	cleanedPath := filepath.Clean(wavebase.ExpandHomeDirSafe(path))
	if _, err := os.Stat(cleanedPath); err == nil {
		return fmt.Errorf("file %q already exists", path)
	}
	if err := os.MkdirAll(filepath.Dir(cleanedPath), 0755); err != nil {
		return fmt.Errorf("cannot create directory %q: %w", filepath.Dir(cleanedPath), err)
	}
	if err := os.WriteFile(cleanedPath, []byte{}, 0644); err != nil {
		return fmt.Errorf("cannot create file %q: %w", cleanedPath, err)
	}
	return nil
}

func (impl *ServerImpl) RemoteFileMoveCommand(ctx context.Context, data wshrpc.CommandFileCopyData) error {
	destUri := data.DestUri
	srcUri := data.SrcUri

	destConn, err := connparse.ParseURIAndReplaceCurrentHost(ctx, destUri)
	if err != nil {
		return fmt.Errorf("cannot parse destination URI %q: %w", srcUri, err)
	}

View on GitHub (pinned to a4447c1563)