metals-vscode icon indicating copy to clipboard operation
metals-vscode copied to clipboard

Goto definition is very slow

Open anton-zen opened this issue 3 years ago • 14 comments

Describe the bug

Goto definition takes from 7 to 15 seconds.

To Reproduce Steps to reproduce the behavior:

Find a val or def reference and go to its definition via context menu.

Expected behavior

200ms

Installation:

  • Operating system: macOS
  • VSCode version: 1.64.0
  • VSCode extension version: v1.12.0
  • Metals version: 0.11.1

Additional context

Macbook pro 2019, 2.6 GHz 6-Core Intel Core i7, 32 GB of RAM.

anton-zen avatar Feb 07 '22 00:02 anton-zen

Is this a Scala 3 project?

kpodsiad avatar Feb 07 '22 08:02 kpodsiad

Thanks for reporting! Unfortunately we can't do anything without a reproduction, since normally go to definition works fine. I would be great if you could provide more information.

tgodzik avatar Feb 07 '22 09:02 tgodzik

@kpodsiad yes, it's a Scala 3 project.

@tgodzik I'll be happy to assist as much as I can. Is there some way to diagnose this? There is nothing in Output tab when I use this feature.

anton-zen avatar Feb 07 '22 09:02 anton-zen

We would need information whether this happens on every source code in your project or maybe a particular symbol. Is it in the same file? Does the code compile? The best would be a small snippet that we can test it out on or a link to the project if it's possible.

tgodzik avatar Feb 07 '22 09:02 tgodzik

Yes, code compiles, sbt shell runs fine, and metals import finishes successfully.

It seems to happen with any symbol but I am using a reference to a sibling method and it's always slow, not just at first attempt. Opening an additional vscode window seems to be triggering this issue but I am not 100% sure. Maybe it's just happening after a couple of minutes.

I could reproduce this with this Scala 3 project https://github.com/scala/scala3-example-project with runExample symbol. Also noticed that java process is taking between 100-300% of CPU when this happens.

Also happens in a tiny hello world Scala 2 project but takes just 2-3 seconds (sbt new scala/hello-world.g8) with this code:

object Main extends App {
  println(hello)

  def hello = {
    "hello!"
  }
}

anton-zen avatar Feb 09 '22 00:02 anton-zen

There is something interesting in metals.log though:

2022.02.09 10:25:13 ERROR code navigation does not work for the file '/Users/anton.kuzmin/Code/3rd/scala3-example-project/src/main/scala/Main.scala' because the SemanticDB file '/Users/anton.kuzmin/Code/3rd/scala3-example-project/.bloop/root/bloop-bsp-clients-classes/classes-Metals-7uavCLAZQQOxDH43jx6ffg==/META-INF/semanticdb/src/main/scala/Main.scala.semanticdb' doesn't exist. There can be many reasons for this error. 
2022.02.09 10:25:15 INFO  time: indexed workspace in 4.87s
Exception in thread "pool-3-thread-1" java.lang.NullPointerException
	at scala.meta.internal.pc.ScalaPresentationCompiler.hover$$anonfun$1(ScalaPresentationCompiler.scala:346)
	at scala.meta.internal.pc.CompilerAccess.withSharedCompiler(CompilerAccess.scala:137)
	at scala.meta.internal.pc.CompilerAccess.withNonInterruptableCompiler$$anonfun$1(CompilerAccess.scala:125)
	at scala.meta.internal.pc.CompilerAccess.onCompilerJobQueue$$anonfun$1(CompilerAccess.scala:197)
	at scala.meta.internal.pc.CompilerJobQueue$Job.run(CompilerJobQueue.scala:139)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:829)

metals.log

anton-zen avatar Feb 09 '22 00:02 anton-zen

Got it! It starts happening after new client connected: network-1 appears in sbt shell:

[info] sbt server started at local:///Users/anton.kuzmin/.sbt/1.0/server/8bb7b84bd7940a6bad5f/sock
[info] started sbt server
[info] new client connected: network-1

anton-zen avatar Feb 09 '22 01:02 anton-zen

You seem to be running Bloop build server, so this should actually not show up. When did new client connected: network-1 pop up?

I don't see anything wrong in the logs and the go to definition should work very fast in the sample projects you mentioned (I double checked and everything works).

When everything is compile it means that the indexes are in place and finding definition is just a matter of reading that.

Looks like there might be something in your env triggering this behaviour. Is the CPU working fully when using go-to-definition?You mentioned a second VS Code window, is it only happens when you open more workspaces?

tgodzik avatar Feb 09 '22 17:02 tgodzik

@tgodzik The problem starts happening exactly after new client connected: network-1 appears in the sbt shell. I demonstrated on this video https://www.youtube.com/watch?v=_SkpS_2nhSU

You mentioned a second VS Code window, is it only happens when you open more workspaces?

I had only 1 vscode window running, so I was wrong to think it was causing it.

Is the CPU working fully when using go-to-definition?

java process is using 100-120% of CPU and other processes barely use any CPU.

anton-zen avatar Feb 09 '22 23:02 anton-zen

That's super weird, are you using SBT via BSP or the default bloop build tool? Meaning did you ever run 'switch to SBT build server' or something along those lines?

Where does the additional connection come from? If it's metals trying to connect to SBT then maybe it's an outdated SBT version? There were some issues in older SBT versions.

tgodzik avatar Feb 10 '22 19:02 tgodzik

@tgodzik I've found the culprit. Once I've uninstalled lightbend.vscode-sbt-scala (v0.2.4) extension, sbt shell stopped opening and that fixed the problem with goto definition. There must be some incompatibility between this extension and metals?

anton-zen avatar Feb 13 '22 21:02 anton-zen

There might, though I am not sure how that would come about when Bloop is used :thinking:

tgodzik avatar Feb 14 '22 08:02 tgodzik

It does support Scala files from what I see, so I think it might not be possible to make the two work together.

tgodzik avatar Feb 14 '22 08:02 tgodzik

Just to confirm that I had the same problem and fixed it by disinstalling Lightbend sbt extension.

Teudimundo avatar Sep 01 '22 07:09 Teudimundo

Just to confirm that I had the same problem and fixed it by disinstalling Lightbend sbt extension.

The same for me! :) And I have the same Macbook pro 2019, 2.6 GHz 6-Core Intel Core i7, 32 GB of RAM.

mkharytonau avatar Jan 25 '23 13:01 mkharytonau

Thanks for confirming! I think we can close this one for now, doesn't seem like an issue with Metals.

tgodzik avatar Jan 25 '23 13:01 tgodzik