Skip to content

gh pr list exits with the incorrect status when the output is captured #10951

Open

Description

@sfc-gh-bhannel

Describe the bug

If stdout of the command is piped to /dev/null, or captured with Python's subprocess.PIPE, the exit code is 0 even when it should be 1. This can silently mask real failures.

Affected version

Please run gh version and paste the output below.

$ gh version
gh version 2.25.1 (2023-03-21)
https://github.com/cli/cli/releases/tag/v2.25.1

Steps to reproduce the behavior

  1. Set up GH_TOKEN with a classic personal access token which does not have SSO access to the repo in question.
  2. Run gh pr list --author=@me > /dev/null
  3. Run echo $?
  4. Observe that the exit status was 0, even though the command failed. The failure is silently hidden.

Expected vs actual behavior

gh pr list should exit with a non-zero status if authentication failed.

Logs

Paste the activity from your command line. Redact if needed.

Actual behavior:

$ gh pr list --author=@me
GraphQL: Resource protected by organization SAML enforcement. You must grant your Personal Access token access to this organization. (repo_000)
Authorize in your web browser: <URL REDACTED>
$ echo $?
1
$ gh pr list --author=@me > /dev/null
$ echo $?
0

Expected behavior:

$ gh pr list --author=@me
GraphQL: Resource protected by organization SAML enforcement. You must grant your Personal Access token access to this organization. (repo_000)
Authorize in your web browser:  <URL REDACTED>
$ echo $?
1
$ gh pr list --author=@me > /dev/null
$ echo $?
1 # This is the issue. This command should exit with nonzero status when authentication fails.

Actual output with verbose logs:

$ export GH_DEBUG=true
$ gh pr list --author=@me > /dev/null
[git remote -v]
[git config --get-regexp ^remote\..*\.gh-resolved$]
* Request at 2025-05-08 15:45:31.763844771 +0000 UTC m=+0.051044783
* Request to https://api.github.com/graphql
* Request took 382.484907ms
$ echo $?
0

Activity

  1. added
    bug
    on May 8, 2025
  2. added
    needs-triage
    on May 8, 2025
  3. sfc-gh-bhannel commented on May 8, 2025

    @sfc-gh-bhannel
    Author

    I confirmed that this reproduces on 2.72.0 (the latest release):

    $ ./gh_2.72.0_linux_amd64/bin/gh version
    gh version 2.72.0 (2025-04-30)
    https://github.com/cli/cli/releases/tag/v2.72.0
    $ ./gh_2.72.0_linux_amd64/bin/gh pr list --author=@me
    GraphQL: Resource protected by organization SAML enforcement. You must grant your Personal Access token access to this organization. (repo_000)
    Authorize in your web browser:  https://github.com/orgs/snowflakedb/sso?authorization_request=A7H5YUTID55UZNK44BYCIT3IDTRJPA5PN5ZGOYLONF5GC5DJN5XF62LEZYAGE6QUVVRXEZLEMVXHI2LBNRPWSZGOQ3PNA45PMNZGKZDFNZ2GSYLML52HS4DFVNHWC5LUNBAWGY3FONZQ
    $ echo $?
    1
    $ ./gh_2.72.0_linux_amd64/bin/gh pr list --author=@me > /dev/null
    $ echo $?
    0
    
  4. babakks commented on May 8, 2025

    @babakks
    Member

    Hi @sfc-gh-bhannel, Thanks for reporting this! 🙏

    This is an interesting case. I tried to reproduce this issue and could get to the same error message, but the exit code is always 1, regardless of the /dev/null redirection:

    ➜  ~ gh pr list -R <OWNER/REPO> >/dev/null
    GraphQL: Resource protected by organization SAML enforcement. You must grant your Personal Access token access to this organization. (repository)
    Authorize in your web browser:  <REDACTED>
    ➜  ~ echo $?
    1
    

    Note that since you're applying the --author option, gh is calling a different GQL query than what I'm hitting in the above excerpt. I'm not sure how yet, but maybe that's the reason we're getting different exit codes.

    Now, I need these pieces of information from you to further investigate this:

    1. Your OS and shell.
    2. Result and exit code of these two commands: (please redact what's not meant to be shared)
      GH_DEBUG=api gh pr list -R <your-SSO-requiring-repo> > /dev/null
      GH_DEBUG=api gh pr list --author @me -R <your-SSO-requiring-repo> > /dev/null

    Could you please provide me with these?

  5. added
    more-info-needed
    on May 9, 2025
  6. sfc-gh-bhannel commented on May 9, 2025

    @sfc-gh-bhannel
    Author

    Thank you for looking at this issue @babakks. Here is the output of the commands you requested. The issue is still reproducing for me. The --author filter is necessary to reproduce the issue.

    $ GH_DEBUG=api gh pr list -R REDACTED > /dev/null
    * Request at 2025-05-09 12:25:41.436029523 +0000 UTC m=+0.042249196
    * Request to https://api.github.com/graphql
    > POST /graphql HTTP/1.1
    > Host: api.github.com
    > Accept: application/vnd.github.merge-info-preview+json, application/vnd.github.nebula-preview
    > Authorization: token REDACTED
    > Content-Length: 951
    > Content-Type: application/json; charset=utf-8
    > Graphql-Features: merge_queue
    > Time-Zone: UTC
    > User-Agent: GitHub CLI 2.25.1
    
    GraphQL query:
    fragment pr on PullRequest{number,title,state,url,headRefName,headRepositoryOwner{id,login,...on User{name}},isCrossRepository,isDraft,createdAt}
        query PullRequestList(
          $owner: String!,
          $repo: String!,
          $limit: Int!,
          $endCursor: String,
          $baseBranch: String,
          $headBranch: String,
          $state: [PullRequestState!] = OPEN
        ) {
          repository(owner: $owner, name: $repo) {
            pullRequests(
              states: $state,
              baseRefName: $baseBranch,
              headRefName: $headBranch,
              first: $limit,
              after: $endCursor,
              orderBy: {field: CREATED_AT, direction: DESC}
            ) {
              totalCount
              nodes {
                ...pr
              }
              pageInfo {
                hasNextPage
                endCursor
              }
            }
          }
        }
    GraphQL variables: {"limit":30,"owner":"REDACTED","repo":"REDACTED","state":["OPEN"]}
    
    < HTTP/2.0 200 OK
    < Access-Control-Allow-Origin: *
    < Access-Control-Expose-Headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset
    < Content-Security-Policy: default-src 'none'
    < Content-Type: application/json; charset=utf-8
    < Date: Fri, 09 May 2025 12:25:41 GMT
    < Github-Authentication-Token-Expiration: 2025-06-07 13:58:45 UTC
    < Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
    < Server: github.com
    < Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
    < Vary: Accept-Encoding, Accept, X-Requested-With
    < X-Accepted-Oauth-Scopes: repo
    < X-Content-Type-Options: nosniff
    < X-Frame-Options: deny
    < X-Github-Media-Type: github.v4; param=merge-info-preview.nebula-preview; format=json
    < X-Github-Request-Id: F324:21432:2C7CBAA:2DB3280:681DF445
    < X-Oauth-Scopes: gist, notifications, project, read:org, read:user, repo, user:email
    < X-Ratelimit-Limit: 5000
    < X-Ratelimit-Remaining: 4992
    < X-Ratelimit-Reset: 1746796633
    < X-Ratelimit-Resource: graphql
    < X-Ratelimit-Used: 8
    < X-Xss-Protection: 0
    
    {
      "data": {
        "repository": null
      },
      "errors": [
        {
          "type": "NOT_FOUND",
          "path": [
            "repository"
          ],
          "locations": [
            {
              "line": 11,
              "column": 4
            }
          ],
          "message": "Could not resolve to a Repository with the name 'REDACTED'."
        }
      ]
    }
    
    * Request took 232.251585ms
    GraphQL: Could not resolve to a Repository with the name 'REDACTED'. (repository)
    $ echo $?
    1
    $ GH_DEBUG=api gh pr list --author @me -R REDACTED > /dev/null
    * Request at 2025-05-09 12:25:42.040362014 +0000 UTC m=+0.042758738
    * Request to https://api.github.com/graphql
    > POST /graphql HTTP/1.1
    > Host: api.github.com
    > Accept: application/vnd.github.merge-info-preview+json, application/vnd.github.nebula-preview
    > Authorization: token REDACTED
    > Content-Length: 582
    > Content-Type: application/json; charset=utf-8
    > Graphql-Features: merge_queue
    > Time-Zone: UTC
    > User-Agent: GitHub CLI 2.25.1
    
    GraphQL query:
    fragment pr on PullRequest{number,title,state,url,headRefName,headRepositoryOwner{id,login,...on User{name}},isCrossRepository,isDraft,createdAt}
        query PullRequestSearch(
          $q: String!,
          $limit: Int!,
          $endCursor: String,
        ) {
          search(query: $q, type: ISSUE, first: $limit, after: $endCursor) {
            issueCount
            nodes {
              ...pr
            }
            pageInfo {
              hasNextPage
              endCursor
            }
          }
        }
    GraphQL variables: {"limit":30,"q":"author:@me repo:REDACTED state:open type:pr"}
    
    < HTTP/2.0 200 OK
    < Access-Control-Allow-Origin: *
    < Access-Control-Expose-Headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset
    < Content-Security-Policy: default-src 'none'
    < Content-Type: application/json; charset=utf-8
    < Date: Fri, 09 May 2025 12:25:42 GMT
    < Github-Authentication-Token-Expiration: 2025-06-07 13:58:45 UTC
    < Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
    < Server: github.com
    < Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
    < Vary: Accept-Encoding, Accept, X-Requested-With
    < X-Accepted-Oauth-Scopes: repo
    < X-Content-Type-Options: nosniff
    < X-Frame-Options: deny
    < X-Github-Media-Type: github.v4; param=merge-info-preview.nebula-preview; format=json
    < X-Github-Request-Id: 8E5E:C75E1:2910597:2A46363:681DF446
    < X-Oauth-Scopes: gist, notifications, project, read:org, read:user, repo, user:email
    < X-Ratelimit-Limit: 5000
    < X-Ratelimit-Remaining: 4991
    < X-Ratelimit-Reset: 1746796633
    < X-Ratelimit-Resource: graphql
    < X-Ratelimit-Used: 9
    < X-Xss-Protection: 0
    
    {
      "data": {
        "search": {
          "issueCount": 0,
          "nodes": [],
          "pageInfo": {
            "hasNextPage": false,
            "endCursor": null
          }
        }
      }
    }
    
    * Request took 382.703321ms
    $ echo $?
    0
    

    Note the last exit code is 0 - that is the issue.

  7. williammartin commented on May 9, 2025

    @williammartin
    Member

    Thanks for the swift response.

    Can you please provide the output without piping to devnull but including --author:

    GH_DEBUG=api gh pr list -R REDACTED --author=@me
    

    I understand why you are getting a zero exit code with --author=@me > /dev/null (because the search API is reporting no results since it doesn't have access to your repo - that is, unfortunately, how the API seems to work). What I'm surprised about is that you ever receive the SAML error when providing --author, even when not piping to dev null as you said in your original report:

    $ gh pr list --author=@me
    GraphQL: Resource protected by organization SAML enforcement. You must grant your Personal Access token access to this organization. (repo_000)
    Authorize in your web browser: <URL REDACTED>
    $ echo $?
    1
    $ gh pr list --author=@me > /dev/null
    $ echo $?
    0
    
  8. babakks commented on May 9, 2025

    @babakks
    Member

    Thanks for the details, @sfc-gh-bhannel!

    I agree with @williammartin that you shouldn't see the SAML error.

    But I'm a bit confused now that I'm looking at the trails, because the SAML error should be there on stderr (as with the debug logs). I think you're not reproducing the same error as you reported at the top. Maybe something is different with your environment now.

  9. babakks commented on May 20, 2025

    @babakks
    Member

    @sfc-gh-bhannel, It's been a while, so I'm nudging you if there's an update/change on your side.

  10. removed
    needs-triage
    on Oct 8, 2026
  11. williammartin commented on Oct 8, 2026

    @williammartin
    Member

    Hey @sfc-gh-bhannel,

    Thanks for the report, and sorry this took a while to untangle. In #10951 (comment) I was confused about why we saw the SAML error but I reproduced this with a token that isn't authorised for a SAML protected org. I ran gh pr list --author=@me in a clone with no default repo set:

    $ gh pr list --author=@me; echo $?
    GraphQL: Resource protected by organization SAML enforcement. You must grant your Personal Access token access to this organization. (repo_000)
    Authorize in your web browser: https://github.com/orgs/<REDACTED>/sso?authorization_request=<REDACTED>
    1
    $ gh pr list --author=@me > /dev/null; echo $?
    0
    

    The SAML error doesn't come from listing PRs at all. It comes from a query gh makes to pick a repo from your git remotes (that's the repo_000 alias). We only make that lookup when gh can prompt.

    Prompting requires both stdin and stdout to be a terminal. When stdout is redirected, we skip the lookup and use the first remote. That's also why the -R runs above never showed the SAML error.

    As for the original issue, to reiterate, with --author, the list itself uses the search API. It seems search quietly drops results from repos your token isn't authorised for. So gh gets an empty result, and no results exits 0. I'm not sure what we can do about that since the API makes no distinction.

  12. added
    needs-triage
    on Oct 8, 2026
  13. added
    platform
    gh-issue
    priority-3
    and removed
    bug
    more-info-needed
    needs-triage
    gh-issue
    on Oct 8, 2026
  14. added
    needs-triage
    and removed
    needs-triage
    on Oct 8, 2026
  15. added
    bug
    on Oct 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    blockedbugplatformpriority-3

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions