cli/cli · error

failed to list codespaces: %w

Error message

failed to list codespaces: %w

What it means

Returned by the gh codespace stop command when no specific codespace was given and listing candidate codespaces via a.apiClient.ListCodespaces fails. The list is filtered to running codespaces afterwards, but this error means the API call itself errored before filtering.

Source

Thrown at pkg/cmd/codespace/stop.go:58

func (a *App) StopCodespace(ctx context.Context, opts *stopOptions) error {
	var (
		codespaceName = opts.selector.codespaceName
		repoName      = opts.selector.repoName
		ownerName     = opts.userName
	)

	if codespaceName == "" {
		var codespaces []*api.Codespace
		err := a.RunWithProgress("Fetching codespaces", func() (err error) {
			codespaces, err = a.apiClient.ListCodespaces(ctx, api.ListCodespacesOptions{
				RepoName: repoName,
				OrgName:  opts.orgName,
				UserName: ownerName,
			})
			return
		})
		if err != nil {
			return fmt.Errorf("failed to list codespaces: %w", err)
		}

		var runningCodespaces []*api.Codespace
		for _, c := range codespaces {
			cs := codespace{c}
			if cs.running() {
				runningCodespaces = append(runningCodespaces, c)
			}
		}
		if len(runningCodespaces) == 0 {
			return errors.New("no running codespaces")
		}

		includeOwner := opts.orgName != ""
		skipPromptForSingleOption := repoName != ""
		codespace, err := chooseCodespaceFromList(ctx, runningCodespaces, includeOwner, skipPromptForSingleOption)
		if err != nil {
			return fmt.Errorf("failed to choose codespace: %w", err)

View on GitHub (pinned to 0eeec0b92e)

Solutions

  1. Run 'gh auth status' and refresh credentials if needed
  2. Drop --org/--repo filters to test whether entitlement scoping causes the failure
  3. Retry after transient network/API errors
  4. Check the wrapped error body for the API's reason (403, 404, 429)
Defensive patterns

Strategy: retry

Validate before calling

if _, err := apiClient.ListCodespaces(ctx, api.ListCodespacesOptions{}); err != nil {
    return fmt.Errorf("cannot list codespaces: %w", err)
}

Try / catch

if err := stopRun(opts); err != nil && strings.HasPrefix(err.Error(), "failed to list codespaces:") {
    time.Sleep(5 * time.Second)
    return stopRun(opts) // transient API failure
}

Prevention

When it happens

Trigger: codespaceName == "" and a.RunWithProgress wrapping a.apiClient.ListCodespaces(ctx, api.ListCodespacesOptions{RepoName, OrgName, UserName}) returns an error: invalid/expired auth, API failure, or network error.

Common situations: gh auth token expired or lacks the codespaces scope; using --org or --repo filters the token is not entitled to; GitHub API degradation; proxy blocking api.github.com.

Related errors


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