emacs-lsp / dap-mode

Emacs :heart: Debug Adapter Protocol
https://emacs-lsp.github.io/dap-mode
GNU General Public License v3.0
1.31k stars 182 forks source link

Q: Scala/SBT debug template fails with NoSuchElementException #735

Open bahmanm opened 1 year ago

bahmanm commented 1 year ago

Hey 👋 I'm trying to debug a Scala/SBT application using a custom template as below:

(dap-register-debug-template
  "My Scala Attach"
  (list :type "scala"
        :request "attach"
        :name "My Scala Attach"
        :hostName "localhost"
        :port 5005
        :buildTarget "app"))

However, it keeps failing with the following in *Messages*:

helm-M-x-execute-command: ‘workspace/executeCommand’ with ‘debug-adapter-start’ failed.

(error "Internal error.")

And this is the relevant excerpt from metals.log:

May 02, 2023 8:49:21 AM org.eclipse.lsp4j.jsonrpc.RemoteEndpoint fallbackResponseError
SEVERE: Internal error: java.util.NoSuchElementException: None.get
java.util.concurrent.CompletionException: java.util.NoSuchElementException: None.get
    at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:292)
    at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:308)
    at java.util.concurrent.CompletableFuture.uniAccept(CompletableFuture.java:661)
    at java.util.concurrent.CompletableFuture$UniAccept.tryFire(CompletableFuture.java:646)
    at java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:488)
    at java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:1990)
    at scala.concurrent.java8.FuturesConvertersImpl$CF.apply(FutureConvertersImpl.scala:29)
    at scala.concurrent.java8.FuturesConvertersImpl$CF.apply(FutureConvertersImpl.scala:26)
    at scala.concurrent.impl.Promise$Transformation.run(Promise.scala:484)
    at scala.concurrent.ExecutionContext$parasitic$.execute(ExecutionContext.scala:222)
    at scala.concurrent.impl.Promise$Transformation.submitWithValue(Promise.scala:429)
    at scala.concurrent.impl.Promise$DefaultPromise.submitWithValue(Promise.scala:338)
    at scala.concurrent.impl.Promise$DefaultPromise.tryComplete0(Promise.scala:285)
    at scala.concurrent.impl.Promise$Transformation.run(Promise.scala:504)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:750)
Caused by: java.util.NoSuchElementException: None.get
    at scala.None$.get(Option.scala:627)
    at scala.None$.get(Option.scala:626)
    at scala.meta.internal.metals.debug.SourcePathAdapter$.apply(SourcePathAdapter.scala:72)
    at scala.meta.internal.metals.debug.MetalsDebugAdapter$.2$u002Ex(MetalsDebugAdapter.scala:70)
    at scala.meta.internal.metals.debug.DebugProvider.$anonfun$start$12(DebugProvider.scala:165)
    at scala.meta.internal.metals.debug.DebugServer.loop$1(DebugServer.scala:21)
    at scala.meta.internal.metals.debug.DebugServer.listen$lzycompute(DebugServer.scala:34)
    at scala.meta.internal.metals.debug.DebugServer.listen(DebugServer.scala:19)
    at scala.meta.internal.metals.debug.DebugProvider.start(DebugProvider.scala:190)
    at scala.meta.internal.metals.debug.DebugProvider.$anonfun$start$5(DebugProvider.scala:110)
    at scala.concurrent.impl.Promise$Transformation.run(Promise.scala:470)
    ... 3 more

Am I missing anything obvious? I'd appreciate any hint/help here before going down a potential rabbit hole 🙏

yyoncho commented 1 year ago

There is this in attach template: https://github.com/scalameta/metals-vscode/blob/main/packages/metals-vscode/package.json#L944

And the crash happens here:

https://github.com/scalameta/metals/blob/b1532e787797355855371a619302327a078da34b/metals/src/main/scala/scala/meta/internal/metals/debug/SourcePathAdapter.scala#L66

I don't know that that is in scala context, but maybe you know.

bahmanm commented 1 year ago

Thanks for promptly looking into it 🙏 That's exactly where my search stopped too. After reading the code, to my mind, a buildTarget identifies the SBT subproject/classpath you'd want to debug.

It looks like the only way to solve this is to clone metals repo, install a local version and try to debug-log the root cause but since this is quite involved I thought I'd ask the community in case I'm overlooking something 😕

yyoncho commented 1 year ago

AFAICS it says in their docs/samples.

 // name of the module that is being debugging