copilot-emacs / copilot.el

An unofficial Copilot plugin for Emacs.
MIT License
1.71k stars 122 forks source link

copilot-mode hangs and then prevents buffer from being foregrounded #280

Open petergaultney opened 3 months ago

petergaultney commented 3 months ago

from Messages

Error during redisplay: (copilot--on-doc-focus #<window 6 on *Warnings*>) signaled (jsonrpc-error "request id=1 failed:" (jsonrpc-error-code . -1) (jsonrpc-error-message . "Server died") (jsonrpc-error-data))

there's a long pause; sometimes several seconds. Then this error. The buffer itself doesn't come up. I can't even switch to the buffer afterward - it is listed in the set of open buffers, but every attempt to switch just causes this same issue.

I see the following in copilot-events:

[stderr] 
[stderr] 
[stderr] nil
[stderr] nil
[stderr] Process copilot agent stderr finished
[stderr] node:internal/modules/cjs/loader:1078
[stderr]   throw err;
[stderr]   ^
[stderr] 
[stderr] Error: Cannot find module '/Users/peter.gaultney/.emacs.d/.cache/copilot/bin/copilot-node-server'
[stderr]     at Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)
[stderr]     at Module._load (node:internal/modules/cjs/loader:920:27)
[stderr]     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
[stderr]     at node:internal/main/run_main_module:23:47 {
[stderr]   code: 'MODULE_NOT_FOUND',
[stderr]   requireStack: []
[stderr] }
[stderr] 
[stderr] Node.js v18.15.0

----------b---y---e---b---y---e----------

that seems straightforward enough, but shouldn't the failure to load copilot not 1. hang emacs and 2. stop the buffer from appearing at all?

i'm using straight.el, and the md5 of copilot.el is 24254afdb7fc64841c1f7c15786cfa14

emil-vdw commented 3 months ago

This error case can definitely be handled better. Could you try a copilot-reinstall-server and report back?

petergaultney commented 3 months ago

Thanks for the response.

copilot-reinstall-server failed for reasons having to do with asdf being... randomly broken. Unfortunately that error was hard to interpret as well. While I was trying to debug what was wrong with asdf, I kept trying to open files (e.g. .tool-versions with emacs, and every time i tried to open a file, emacs would hang because copilot-mode was hanging on every switch to a buffer.

Emacs being single-threaded can exacerbate unfortunate problems like this one, where a plugin like copilot-mode can essentially completely break the editor with no way to turn it off or debug it, because the emacs debugging facilities are unreachable and every buffer that gets opened tries to activate the offending plugin, resulting in infinite recursive hangs. At some point I just got lucky and happened to guess that I should try to fix everything via the command line.

I did eventually get asdf to behave (there are a long list of my particular asdf issue out there, with no clear workaround, but eventually you can beat it into submission), and that resolved all my immediate issues.

Still, if you can think of a way to avoid suppressing the buffer switch (and also a way to, perhaps, somehow use async I/O to wait for the results of external processes?), that would really help for cases where the underlying environment is somehow misconfigured.