phoenixframework / phoenix_live_reload

Provides live-reload functionality for Phoenix
MIT License
315 stars 90 forks source link

Live reload breaks debugging session in elixirLS debugger #133

Open lukaszsamson opened 1 year ago

lukaszsamson commented 1 year ago

I know that this most likely not be planned but there are repeated bugs and support requests related to that issue.

https://github.com/elixir-lsp/elixir-ls/issues/797 https://github.com/elixir-lsp/elixir-ls/issues/798

There are 2 problems when phoenix is run under elixirLS debugger

  1. The code reloader (https://github.com/phoenixframework/phoenix/blob/master/lib/phoenix/code_reloader/server.ex) is purging and reloading modules on the fly with no regard for the ongoing debug session. The reloaded modules are not properly interpreted via erlang debugger.
  2. It is messing with IO interception. Debug Adapter Protocol has strict requirements on stdout IO. All standard IO needs to be sent as JSONRPC protocol messages.
josevalim commented 1 year ago

This is tricky because I can’t come with good suggestions to address this. At best we could block the code reloader but I am not sure that would help either.

CharlesIrvineKC commented 1 year ago

Thought I would add a note specifying the impact of and work around for the problem. Basically, any time code is changed while in a debugging session, the Elixir compiler will likely throw spurious (false positive) errors. The work around is to bounce the debugger. This clears the problem and allows debugging to continue.

josevalim commented 1 year ago

@lukaszsamson if you can reproduce it we should do a pairing session :)

CharlesIrvineKC commented 1 year ago

There may be multiple error paths, but here is one that comes up often. To reproduce in vscode:

  1. Create a new Phoenix 1.6 app: "mix phx.new hello".
  2. Create a launch config. Add: "task": "phx.server".
  3. In "page_controller.ex" add a break point to line 5.
  4. Execute: Run/Start Debugging.
  5. Go to "localhost:4000" to trigger the breakpoint. Continue past the breakpoint.
  6. Insert "IO.inspect(conn)" above line 5.
  7. Reset breakpoint to new line 5 and refresh "localhost:4000".
  8. The following error occurs:
Compiling 1 file (.ex)

== Compilation error in file lib/hello_web/controllers/page_controller.ex ==
** (UndefinedFunctionError) function HelloWeb.__using__/1 is undefined or private. However there is a macro with the same name and arity. Be sure to require HelloWeb if you intend to invoke this macro
    (hello 0.1.0) HelloWeb.__using__(:controller)
    lib/hello_web/controllers/page_controller.ex:2: (module)
    (elixir 1.14.2) lib/kernel/parallel_compiler.ex:346: anonymous fn/5 in Kernel.ParallelCompiler.spawn_workers/7
coreyvan commented 1 month ago

Is there any update to this issue or other workarounds? This makes the dev experience while debugging (I'm basically always debugging) pretty painful.

lukaszsamson commented 1 month ago

No. To address it either phoenix should check if modules are interpreted and call :int.ni after it recompiles or emit some event that the debug adapter can listen for and reinterpret. Probably breakpoints also need to be reset after that. A workaround could be to periodically reinterpret all modules in the VM that match launch config settings.

CharlesIrvineKC commented 1 month ago

My impression is that everyone in the Phoenix development community has given up on using a debugger and just use IO.inspect. Coming from a Java background where everyone used a debugger, I struggled for quite a while trying to get debugging working with Phoenix and ElixirLS and finally gave up and gave in to using IO.inspect. It's a shame I think. A lot of people would be very happy to be able to use the ElixirLS debugger with Phoenix.

morganhein commented 1 month ago

Ya this is a shame. I have been trying to audit phoenix and see if I should propose we switch to it for some things at the company I work for, but this is a hard blocker. I really love this ecosystem but being unable to use the tools like we're accustomed and requiring console logging is a non-starter.

That being said, thank you all that have worked on these projects and gotten things this far. Hopefully this gets fixed in the future, and then I'll revisit.

CharlesIrvineKC commented 1 month ago

I did a post for this on the forum. Let's see if anyone responds.

https://elixirforum.com/t/source-level-debugging-for-phoenix-and-phoenix-liveview-apps/65400