Page MenuHomePhabricator

editor.py can't read "export EDITOR='code -w'" from bash config
Closed, ResolvedPublic

Description

Pywikibot is unable to read my environmental variable 'EDITOR' which is set to 'code -w' for VSCode.

I see on editor.py that only a limited number of text editor names are supported by the TextEditor class, including kate, vim, emacs, jedit, and notepad++. First of all, I would recommend adding Sublime Text and VSCode to this list of supported editors. But if only such a limited number of editors are supported anyway, it's important to fall back to e.g. tkinter if the environmental variable for 'EDITOR' cannot be interpreted. Right now there is no fallback if there is any value for "EDITOR", resulting in a FileNotFoundError if TextEditor can't interpret that environmental variable.

Event Timeline

@ElSeiver: could you please also post the traceback. In principle there are no unsupported editors.

Inventing a bit into this issue I am pretty sure that the FileNotFoundError is raised when the editor module tries to read the changed temp file after it was edited with your editor or the edit command was done and the temp file was lost in mean time. This is not a Pywikibot issue but made by your os or editor due to unknown reason. Therefore I close this task. Please ask if you have further questions about Pywikibot or feel free to re-opnen this task if i was wrong. Note: there is no restriction about any editors except "notepad.exe" on Windows which will replaced by Tkinter by default.

So I'm still having a problem with export EDITOR="code -w", though it's no longer returning FileNotFoundError. Rather, it says ERROR: [Errno 2] No such file or directory: 'code -w'. When I remove the -w flag, it opens a blank tab in VSCode with a name like tmp******.wiki where the stars appear to be random characters, and proceeds along with the pwb script as if I had saved the file with no changes. I think it's because pywikibot can't handle this variable having a space and/or flag. I accept that VS Code may never work with pywikibot. Would you consider having the option of checking a different envt variable first before EDITOR, so I could do something like export PWB_EDITOR=nano so I don't have to change my default editor for every single application on my machine?

ElSeiver renamed this task from FileNotFoundError because no default text editor fallback in editor.py to editor.py can't read "export EDITOR='code -w'" from bash config.Jun 1 2023, 11:31 PM
ElSeiver reopened this task as Open.

Change 926170 had a related patch set uploaded (by ElSeiver; author: ElSeiver):

[pywikibot/core@master] TextEditor.editor: Use shlex to parse editor name

https://gerrit.wikimedia.org/r/926170

Addressed in Gerrit by using shlex to handle spaces in environment variables.
Looks like this issue dates back to T102465

Xqt claimed this task.

Change 926170 merged by jenkins-bot:

[pywikibot/core@master] TextEditor.editor: Use shlex to parse editor name

https://gerrit.wikimedia.org/r/926170