cli/cli · error

failed to remove Copilot CLI: %w

Error message

failed to remove Copilot CLI: %w

What it means

removeCopilot's os.RemoveAll(installDir) returned an error, wrapped with the OS cause. The directory existed (Stat succeeded) but could not be fully deleted.

Source

Thrown at pkg/cmd/copilot/copilot.go:476

	}
	defer func() {
		if cerr := out.Close(); err == nil && cerr != nil {
			err = fmt.Errorf("failed to close file: %w", cerr)
		}
	}()
	if _, err := io.Copy(out, r); err != nil {
		return fmt.Errorf("failed to write file: %w", err)
	}
	return nil
}

func removeCopilot(installDir string) error {
	if _, err := os.Stat(installDir); os.IsNotExist(err) {
		return fmt.Errorf("failed to remove Copilot CLI: Copilot CLI not installed through `gh`")
	}

	if err := os.RemoveAll(installDir); err != nil {
		return fmt.Errorf("failed to remove Copilot CLI: %w", err)
	}

	return nil
}

View on GitHub (pinned to 0eeec0b92e)

Solutions

  1. Read the wrapped error for the offending path and errno
  2. Stop any running processes using files in the install dir, then retry
  3. Fix ownership (chown -R) or remove with appropriate privileges
  4. If the filesystem is read-only, remount or choose a writable location
Defensive patterns

Strategy: try-catch

Validate before calling

// pre-flight writability check on the install dir
if info, err := os.Stat(installDir); err == nil {
    if info.Mode().Perm()&0200 == 0 {
        return fmt.Errorf("install dir %s not writable; fix permissions first", installDir)
    }
}

Try / catch

if err := removeCopilot(installDir); err != nil {
    if strings.Contains(err.Error(), "failed to remove Copilot CLI") && errors.Is(err, fs.ErrPermission) {
        return fmt.Errorf("run removal with sufficient privileges on %s", installDir)
    }
    return err
}

Prevention

When it happens

Trigger: RemoveAll fails on permission errors (files owned by root inside the dir), read-only filesystems, or files held open/locked by a running process (Windows file locking especially).

Common situations: Copilot CLI agent files installed via sudo so the directory is root-owned; the agent process still running and holding the binary open on Windows; install dir on a read-only or network mount.

Related errors


AI-assisted analysis of cli/cli@0eeec0b92e (2026-08-15). Data as JSON: /api/errors/738fa1ce5aa64694. Report an issue: GitHub.