Repository navigation
gh search does not properly handle multi-word search query terms #11228
Description
Activity
Thanks for this catch, @annettejanewilson! 🙏
Looked into this, and I can confirm there is an annoying bug here. As a bit of context,
ghtends to surround multi-word query args with double quotes before submitting an API request:Lines 133 to 138 in 1cbfbf8
func quote(s string) string { if strings.ContainsAny(s, " \"\t\r\n") { return fmt.Sprintf("%q", s) } return s } This is all fine, except that the changes (i.e. quoted values) replace the original state, and then the same quoting happens when
ghis fetching the next page. As you can see in the snippet below, argument passed to theksparameter is changed after the function returns:Lines 151 to 161 in 1cbfbf8
func formatKeywords(ks []string) []string { for i, k := range ks { before, after, found := strings.Cut(k, ":") if !found { ks[i] = quote(k) } else { ks[i] = fmt.Sprintf("%s:%s", before, quote(after)) } } return ks } As a result of this argument mutation, the query grows and worse that, it changes into a different query. Therefore, the returned result is not exactly what the user was looking for (except for the first page, though).
Thanks again for reporting this issue, @annettejanewilson! I'm going to put down the A/C for this in the next comment.
Reacted by Annette Wilson- addedpriority-2Affects more than a few users but doesn't prevent core functionsAffects more than a few users but doesn't prevent core functionscoreThis issue is not accepting PRs from outside contributorsThis issue is not accepting PRs from outside contributorsgh-searchrelating to the gh search commandrelating to the gh search commandand removedneeds-triageneeds to be reviewedneeds to be reviewed
on Jul 7, 2025 Adding a note that this is would be a problem for any
gh searchcommand where--limitexceeds the 100 results per page limit of the REST Search endpoints used. Essentially, a search query with a multi-word search term with--limitover 100 could be impacted by this.Reacted by Babak K. ShandizAs mentioned in the above comment, we should the unwanted argument mutation in
formatKeywords, and return a new slice from the function.Acceptance Criteria
Given I have a query that contains a whitespace character, and my query will result in several pages of pagination
When I run thegh searchcommand (for any subcommand)
Then my query should complete without errorNote to reviewers: you can also validate the change by setting
GH_DEBUG=1and seeing that the query isn’t exponentially increasing in length.- changed the title
[-]"The search is longer than 256 characters" when setting high --limit[/-][+]`gh search` does not properly handle multi-word search query terms[/+]on Jul 7, 2025 - linked a pull request that will close this issueFix query object state mutation during pagination #11244
on Jul 7, 2025
Describe the bug
When using
gh search prswith--limit 1000, the request fails with the error message "Invalid search query The search is longer than 256 characters."It appears that internally, an extra layer of escaping is applied every for every subsequent page of results, until the search string becomes so long that it is rejected by the server.
Affected version
Steps to reproduce the behavior
Run:
Observe
and an exit code of 1.
Expected vs actual behavior
The expected behaviour is that a short query that succeeds for
--limit 30should not result in "Invalid search query" with--limit 1000.Logs
As can be seen, every subsequent request made to the search has multiplied the size of the previous request. They consist primarly of backslashes. It's unclear whether they are actually performing the intended search.