Repository navigation
Description
Describe the feature or problem you’d like to solve
#2408 has gone a long way to help avoid losing data when something goes wrong while creating a PR. I have still been bitten a couple of times though, and it would be great if the backup/recovery functionality could be expanded a bit - so that text like PR descriptions can be "recovered" even if GitHub CLI doesn't think they were lost.
The same kind of problem and opportunities that led to that PR are still relevant now IMO. Many failure scenarios are now covered, but some are not.
Proposed solution
The/tmp/gh* files (which are already created for use with the --recover option) should be stored and kept always, not just if the CLI detects an issue. This can save users from significant time and data-loss.
I don't think the filename needs to be reported to the user, since they probably don't need it if there was no error, as long as it's available somewhere when they go looking for it.
Additional context
My particular issue today triggered when creating a PR, writing a (long) description in an editor locally, and then selecting "Continue in browser". This is my preferred workflow since I normally add screenshots in-browser before submitting the PR.
In this case, the browser that opened was not logged in to GitHub, and the repo I was submitting to was private. So I think what happened is the browser immediately redirected to GitHub's login page, and so lost the PR description from the URL (and it's not in the browser history - which has saved me in some other similar scenarios). The temporary file I wrote the description in had already been deleted, and the /tmp/gh* file that would be left if GitHub CLI detected an issue wasn't stored either, so I'm left with no option to recover.
Activity
jayeheffernan commentedon Jan 17, 2023
Sorry to ping again so soon, but I thought it might help to compare what
gitdoes in a similar situation. When it asks me to type a commit message (after runninggit commit), then it opens a file in the same editor thatgh pr createdoes ($EDITOR- Vim in my case). So the workflows here seem very similar. Whether or not the commit succeeds, that text file remains saved to.git/COMMIT_EDITMSG.If we're concerned about littering the filesystem with too many backup files (I'm not) then maybe a single
/tmp/gh/PR_EDITMSG(text file) or/tmp/gh123.json(current recovery format) would be okay. Better yet if, like thegitmessage, it can be stored somewhere outside of/tmp/, since then it can be recovered even if the system had to reboot.mislav commentedon Jan 18, 2023
Hi, thanks for the report and I'm sorry you lost your typed work.
The browser flow mode is one where we unfortunately can't ensure atomicity. After handing the data (via a URL) to the browser launch process and that process exiting with a zero status, we can't know whether the data actually successfully submitted in the end.
Your suggestion to always persist the data in a recovery file has merit, but I wonder how we would build a UX around that. For instance, if the browser launches successfully, the gh process would exit successfully too, without printing recovery instructions (that would otherwise get printed on API failures). If then your data got lost in the browser, how would you know which recovery file to pass to
gh pr create --recover <file>?Furthermore, git commits getting saved to
.git/COMMIT_EDITMSGis one thing, but permanently saving the data from every gh operation to the filesystem could go against our users' expectations. Saving gh's own files under.gitwould have some benefits, but due to these files then never being cleaned up by the system, I think it's better to keep them in TMPDIR where there is usually some automatic cleanup happening.jayeheffernan commentedon Jan 25, 2023
Thanks for the response @mislav , and apologies for the delay getting back (I've been on holiday 🙂 ).
Yes, the browser flow is a bit of a challenge to design for, since you can't know whether the data was really handled successfully once it's handed over. For that reason I don't think a perfect UX will be possible, but we can aim for "good enough". I suggest no change to when the
gh pr create --recoverinstructions are printed. After handing off to the browser, the overall operation is probably successful - it's reasonable to omit the recovery instructions, as long as recovery is still possible somehow for those users who dig further. Finding which file to use for recovery won't be difficult - if they all match/tmp/gh*then we can simply check the modified times of those files, search withack/ag/rg, or inspect the contents visually. This could be considered clunky but is preferable to losing data, and doesn't interfere with any existing UX flows.I agree also with the concerns about permanent file storage, and in particular that we shouldn't be interfering with
.git. It wouldn't be surprising for this tool to have it's own per-project.ghdirectory, but introducing that is probably overkill in the context of this issue. What about a new config key? Eithertmp_directory(to put all temporary files), orrecovery_directory(only put recovery files here, leave other temporary files where they are) would work. That way we could setrecovery_directory: ~/.ghas a way to opt-in to storing the files permanently and doing any clean up ourselves.Another approach would be to write (and overwrite) recovery data to the same filename every time. E.g.
~/.config/gh/.recovery.json. Although the recovery data is not "config", it still seems a normal use of this directory based on what other tools are doing - e.g.~/.config/pgcliincludes command history and logs,~/.config/lnavincludes logs and crash reports. This has the advantages:.git/COMMIT_EDITMSG(only difference in storing one file globally/per-user rather than one per-repo)vilmibm commentedon Feb 18, 2023
I think there are steps we could take to improve the recovery feature.
I think this corresponds to @jayeheffernan 's thinking, so seems like this is a good approach to take while setting N=1. I honestly don't remember why I overengineered the recovery system like this when it could have been simpler in both UX and in implementation :|
I think as far as backwards compat is concerned we just need to allow
--recoverto take an argument, still, but change it to work on an empty string argument.That should be enough to go on if someone wants to pick up this work.
mculp commentedon Feb 24, 2023
yesterday, I tried
gh pr create --body-file -for the first time. I typed a long PR description only for it to say "you must provide a title" and there was no recovery file :/needless to say, I had chatgpt generate it the second time around. But still, that hurt.
vilmibm commentedon Feb 28, 2023
@mculp out of curiosity if you're looking to interactively write the body of a PR why not invoke
gh pr createand use your$EDITOR?mculp commentedon Feb 28, 2023
No real reason. I usually do what you said. I was just looking through the list of options and tried it. Element of least surprise would be to not destroy everything I had written because I didn’t include a
--title(which isn’t stated as a requirement for--body-file, as far as I noticed)But if you’re questioning why I’m using it, why is it an option? :)
vilmibm commentedon Mar 1, 2023
It's an option because we assumed people would want
--body-filefor piping in a file from somewhere else, not type directly in their terminal as STDIN. In that case, the failure would not lose state because the content of the body exists somewhere already.Don't get me wrong, I do this a lot. if i'm just writing a quick note i'll often do
cat - > foo.txtto save the ~1 second of openingvim. I asked about your reasoning to help gauge if the recovery improvements we're talking about should extend to non-interactive version ofpr createsince I was not planning on that.john-peterson commentedon Jun 2, 2025
I would like a broader setting to keep all temporary files for instance this comment that is written to $TMPDIR/1132881500.md and deleted as soon as I submit it! why not keep it just in case it was probably nothing useless
so I can search in all the gigantic amount of comments I have made faster
and make prepend a modeline where it was submitted
I know about the search functions
but it still doesn't show all comments I made. and
I want a local copy any way and I was recently censored here which has never happened since this site started in 2008. it made me so upset I have to keep local copies of everything I write so I can put it in my blog instead of. it is censored and deleted. by a misunderstanding essentially by someone who doesn't know who I am