Add FAQ entry about `edit()` returning immediately
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Closes #2582
authored by Kevin Deldycke · committed by Kevin Deldycke · parent 420c8fb · Browse files · 2 files·+32 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+32 added−0 removed
- other:+32 added−0 removed
Provenance
- Commit
ce57b87- Tree
b979882- Author
Kevin Deldycke <kevin@deldycke.com>- Committer
Kevin Deldycke <kevin@deldycke.com>- Parent
420c8fb
2 files changed+32 added−0 removed
modified:docs/faqs.md+22 added
80 unmodified lines8182838485868788899091929394959697989910010110210310410510680 unmodified lines| `python -X utf8` | `1` | `utf-8` || `sys.stdout.reconfigure(encoding=…)` | `0` | `utf-8` |
Do not rely on `sys.getfilesystemencoding()` on Windows either: [PEP 529](https://peps.python.org/pep-0529/) makes it `utf-8` regardless of the actual stream encoding.
(edit-wait)=### The `edit` utility fails to open Visual Studio Code
Let's say our default editor is Visual Studio Code. A Click CLI calling `edit()` is supposed to open a new VSCode window on a temporary file then wait for us to edit that file before returning back the CLI execution.
But VSCode (and some other editors) returns immediately: the file is deleted before you had a chance to edit it. With `require_save=True` (the default), the function also returns `None` instead of the edited content.
This is not a bug in Click. In that particular case, the `code` command (VSCode's executable) is a launcher: it starts the real editor in the background, then exits. Click waits on that process and reads its exit as the end of the editing session. The same happens on any platform with any editor whose command returns before its window closes.
#### Answer
Setup your editor to make its launcher block until the window is closed, so Click keeps waiting on it.
You can set the `EDITOR` in your environment with its appropriate flags:
| Editor | Flags ||---|---|| Visual Studio Code | `EDITOR="code --wait"` || Sublime Text | `EDITOR="subl -w"` |
For an editor with no such flag, a workaround is to point `EDITOR` at a small wrapper script that opens the editor and blocks until it exits.modified:src/click/termui.py+10 added
868 unmodified lines869 Note for Windows: to simplify cross-platform usage, the newlines are870 automatically converted from POSIX to Windows and vice versa. As such,871 the message here will have ``\n`` as newline markers.872 873+ .. warning::874+875+ The contract of this utility is that the editor command blocks until876+ the editing session is over: Click waits for it to exit before877+ reading the file back and cleaning it up. Editors whose command878+ launches the real editor in the background and exits immediately879+ break that contract. Click then returns before editing begins and880+ removes the temporary file while the editor still has it open. See881+ :ref:`edit-wait` to make such editors wait.882+883 :param text: the text to edit.884 :param editor: optionally the editor to use. Defaults to automatic885 detection.886 :param env: environment variables to forward to the editor.138 unmodified lines