Fixing the External Editor Add-on

Hi everyone. A little bit of an intro: I’m an avid Anki user and in my Anki usage I’ve often found myself not completely satisfied with the built-in html editor. What I did in the past was copying the HTML input over to Vim, make my modifications, and copying it back over to Anki. Lucky for me, I found an add-on which could save me a few steps: External Editor, which allows you to use an external editor to edit the HTML of cards (link). I’ve recently upgraded to Anki 2.1.48 and sadly that broke the add-on, since the add-on relies on using the Ctrl-Shift-X to open the external editor, which has now been bounded to the new inline HTML editor (which I like by the way, definitely an upgrade over the old HTML editor). Lucky for me, I found an add-on with supposedly the same functionality for Anki 2.1.48, but that allows the user to choose the keybinding, obviating the aforementioned shortcut conflict (link). Unfortunately though, the add-on doesn’t work: as I wrote in a comment under the add-on I couldn’t get it to work neither on MacOS nor on Linux, with any editor. What happens on Linux is that a window briefly appears but is closed instantly after. Me being nosy I took a look at the source code of the add-on (at https://github.com/RisingOrange/anki-external-editor). I’m by no means no expert about developing Anki add-ons, in fact I know almost nothing about their development. I would like to learn, but I’m struggling to find the time (funnily enough, one of the biggest time suckers for me is reviewing Anki flashcard, lol). However, I’m fairly knowledgeable about Python, so I went on with it. I think I narrowed the problem down to the following function under src/anki-external-editor)/ __init__.py (at https://github.com/RisingOrange/anki-external-editor/blob/master/src/anki-external-editor/__init__.py):

def edit(text):
    editor = get_editor()
    filename = tempfile.mktemp(suffix=".html")

    with io.open(filename, 'w', encoding='utf-8') as file:
        file.write(text)

    cmd_list = editor.split() + [filename]
    proc = subprocess.Popen(cmd_list, close_fds=True)
    proc.communicate()

    with io.open(filename, 'r', encoding='utf-8') as file:
        return file.read()

The problem seems to be at the proc = subprocess.Popen(cmd_list, close_fds=True) line. For some reason the subprocess that is spawned is terminating instantly before I’m able to edit the HTML. I’ve tried running that line in the python REPL with the arguments that are passed to the add-on by Anki at runtime, and in that case it works, so I’m not really sure what’s going on. I’m assuming there is some kind of interaction with the Anki framework that I’m not aware of that has some requirements for subprocesses that are not being satisfied by this module. Or it could be something else entirely, not sure. Anyway, can anybody help me debug this module?

If you’re using a packaged version of Anki, it could possibly be related to LD_LIBRARY_PATH - see _packagedCmd()