scalameta / metals

Scala language server with rich IDE features 🚀
https://scalameta.org/metals/
Apache License 2.0
2.1k stars 332 forks source link

Metals not starting when power directives like dockerFrom is in the code #6772

Open yadavan88 opened 1 month ago

yadavan88 commented 1 month ago

Describe the bug

EmailClient.scala

object EmailClient {
    def send() = println("email sent!")
}

Main.scala

//> using scala 3.3.3
//> using file EmailClient.scala
//> using packaging.dockerFrom openjdk:17

@main def main() = {
    EmailClient.send()
}

Open this directory in vscode and try to start the metals. It doesn't start. You will get the error in the metals.log

2024.09.13 09:43:36 INFO  no build target found for /Users/yadukrishnankrishnan/Downloads/test/Main.scala. Using presentation compiler with project's scala-library version: 3.3.3

Auto completion will not work but the code runs fine through command line. If you remove the docker directive, metals will start and everything works fine. Also, if there is only single file, then also no issues with this directive. The problem comes only with the combination of file directive and dockerFrom directive.

Expected behavior

It should be able to work normally

Operating system

macOS

Editor/Extension

VS Code

Version of Metals

1.3.5

Extra context or search terms

This is Mac M3, if that is relevant.

tgodzik commented 1 month ago

Thanks for reporting! I can't seem to reproduce the issue. Are you maybe able to get stack traces of the metals process? I suspect that the build server is not starting, but I am not sure how that would happen. Could also post the whole metals.log from .metals directory?

yadavan88 commented 1 month ago

Hi @tgodzik Thanks for the quick response. I am able to reproduce this exactly even in my MacBook Air. I am attaching the full logs, screenshots and the entire project as zip file for reference. Hope it can be reproduced.

metals.log

metals-test.zip

image

tgodzik commented 1 month ago

I don't see anything wrong in the logs. Would it be possible for you do get stack traces via jstack? You could also create a file: .metals/bsp.trace.json, which should get populated with the communication between Scala CLI and Metals.

I am looking into other possibilities. Also, are you using power option? You could add {"powerOptions":{"power":true}} to ide-launcher-options.json to make sure that is the case?

yadavan88 commented 1 month ago

Here is the bsp trace content:

[Trace - 10:52:27 am] Sending request 'build/initialize - (1)'
Params: {
  "displayName": "Metals",
  "version": "1.3.5",
  "bspVersion": "2.2.0-M2",
  "rootUri": "file:///Users/yadukrishnankrishnan/Downloads/metals-test/",
  "capabilities": {
    "languageIds": [
      "scala",
      "java"
    ],
    "jvmCompileClasspathReceiver": true
  },
  "data": {
    "javaSemanticdbVersion": "0.10.0",
    "semanticdbVersion": "4.9.9",
    "supportedScalaVersions": [
      "2.13.14",
      "2.12.19",
      "2.12.18",
      "2.12.17",
      "2.12.16",
      "2.13.11",
      "2.13.12",
      "2.13.13",
      "2.11.12"
    ],
    "enableBestEffortMode": false
  }
} 

I also tried with the power options. Also have this error in metals.log, but not sure which action caused this

2024.09.16 10:53:56 INFO  Running BSP server List(/Users/yadukrishnan/Library/Application Support/Coursier/bin/.scala-cli.aux, bsp, --json-options, /Users/yadukrishnan/temp/metals-test/.scala-build/ide-options-v2.json, --json-launcher-options, /Users/yadukrishnan/temp/metals-test/.scala-build/ide-launcher-options.json, --envs-file, /Users/yadukrishnan/temp/metals-test/.scala-build/ide-envs.json, /Users/yadukrishnan/temp/metals-test)
2024.09.16 10:53:56 ERROR Running process '/Users/yadukrishnan/Library/Application Support/Coursier/bin/.scala-cli.aux bsp --json-options /Users/yadukrishnan/temp/metals-test/.scala-build/ide-options-v2.json --json-launcher-options /Users/yadukrishnan/temp/metals-test/.scala-build/ide-launcher-options.json --envs-file /Users/yadukrishnan/temp/metals-test/.scala-build/ide-envs.json /Users/yadukrishnan/temp/metals-test' failed
java.io.IOException: Cannot run program "/Users/yadukrishnan/Library/Application Support/Coursier/bin/.scala-cli.aux" (in directory "/Users/yadukrishnankrishnan/Downloads/metals-test"): error=2, No such file or directory
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1143)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1073)
    at scala.meta.internal.process.SystemProcess$.run(SystemProcess.scala:48)
    at scala.meta.internal.bsp.BspServers.newConnection$1(BspServers.scala:117)
    at scala.meta.internal.bsp.BspServers.$anonfun$newServer$8(BspServers.scala:160)
    at scala.meta.internal.metals.BuildServerConnection$.setupServer$1(BuildServerConnection.scala:593)
    at scala.meta.internal.metals.BuildServerConnection$.fromSockets(BuildServerConnection.scala:661)
    at scala.meta.internal.bsp.BspServers.newServer(BspServers.scala:155)
    at scala.meta.internal.bsp.BspConnector.connect$1(BspConnector.scala:167)
    at scala.meta.internal.bsp.BspConnector.connect(BspConnector.scala:214)
    at scala.meta.internal.metals.ProjectMetalsLspService.$anonfun$autoConnectToBuildServer$4(ProjectMetalsLspService.scala:915)
    at scala.meta.internal.metals.TimerProvider.withTimer(TimerProvider.scala:38)
    at scala.meta.internal.metals.TimerProvider.timed(TimerProvider.scala:14)
    at scala.meta.internal.metals.ProjectMetalsLspService.$anonfun$autoConnectToBuildServer$3(ProjectMetalsLspService.scala:915)
    at scala.concurrent.impl.Promise$Transformation.run(Promise.scala:470)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
    at java.lang.Thread.run(Thread.java:833)
Caused by: java.io.IOException: error=2, No such file or directory
    at java.lang.ProcessImpl.forkAndExec(Native Method)
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:314)
    at java.lang.ProcessImpl.start(ProcessImpl.java:244)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1110)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1073)
    at scala.meta.internal.process.SystemProcess$.run(SystemProcess.scala:48)
    at scala.meta.internal.bsp.BspServers.newConnection$1(BspServers.scala:117)
    at scala.meta.internal.bsp.BspServers.$anonfun$newServer$8(BspServers.scala:160)
    at scala.meta.internal.metals.BuildServerConnection$.setupServer$1(BuildServerConnection.scala:593)
    at scala.meta.internal.metals.BuildServerConnection$.fromSockets(BuildServerConnection.scala:661)
    at scala.meta.internal.bsp.BspServers.newServer(BspServers.scala:155)
    at scala.meta.internal.bsp.BspConnector.connect$1(BspConnector.scala:167)
    at scala.meta.internal.bsp.BspConnector.connect(BspConnector.scala:214)
    at scala.meta.internal.metals.ProjectMetalsLspService.$anonfun$autoConnectToBuildServer$4(ProjectMetalsLspService.scala:915)
    at scala.meta.internal.metals.TimerProvider.withTimer(TimerProvider.scala:38)
    at scala.meta.internal.metals.TimerProvider.timed(TimerProvider.scala:14)
    at scala.meta.internal.metals.ProjectMetalsLspService.$anonfun$autoConnectToBuildServer$3(ProjectMetalsLspService.scala:915)
    at scala.concurrent.impl.Promise$Transformation.run(Promise.scala:470)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
    at java.lang.Thread.run(Thread.java:833)

2024.09.16 10:53:56 INFO  tracing is enabled: /Users/yadukrishnankrishnan/Downloads/metals-test/.metals/bsp.trace.json
2024.09.16 10:54:02 INFO  no build target found for /Users/yadukrishnankrishnan/Downloads/metals-test/Main.scala. Using presentation compiler with project's scala-library version: 3.3.3

But after these changes, the program is not running

[E] Caught bloop.data.Project$ProjectReadException: Failed to load project from /Users/yadukrishnankrishnan/Downloads/metals-test/.scala-build/.bloop/metals-test_d0706328d4-9f8be73145.json
Bloop 'bsp' command exited with code 1. Something may be wrong with the current configuration.
Running the clean sub-command to clear the working directory and remove caches might help.
If the error persists, please report the issue as a bug and attach a log with increased verbosity by passing -v -v -v.
Error: java.lang.RuntimeException: Bloop BSP connection in /Users/yadukrishnankrishnan/Library/Caches/ScalaCli/bsp-sockets/proc-7362 was unexpectedly closed or bloop didn't start.
For more details, please see '/Users/yadukrishnankrishnan/Downloads/metals-test/.scala-build/stacktraces/1726477063-1729102292817811323.log'

So I cleaned it up using scala-cli clean . and then it runs through command line as before

yadavan88 commented 1 month ago

Just an update: I killed the java processes and suddenly it is working fine in the project. No idea why

image

bsp.trace.json metals.log

But attaching the logs in case something makes sense:

tgodzik commented 1 month ago

This is quite curious: "/Users/yadukrishnan/Library/Application Support/Coursier/bin/.scala-cli.aux"

Looks like Metals was unable to start Scala CLI. Maybe it had the executable cached somehow and it got removed/reinstalled in the meantime? Did you change anything recently? Though why would it be influenced by the docker directive :thinking:

Does bsp.trace only contain the first message as you pasted above?

tgodzik commented 1 month ago

Anyway, do let me know if that happens again. We could not reproduce on any of our machines.

yadavan88 commented 1 month ago

I havent made any changes recently. I am still able to reproduce the issue from the attached zip file. I just used the same zip and cleared all metadata and tried again. Getting the exact same problem. In both of my laptops I am getting this issue.

Here is the bsp trace file bsp.trace.json

tgodzik commented 1 month ago

Would be great to compare both using a diff of some kind to see what exactly is different.

yadavan88 commented 1 month ago

which two files?

tgodzik commented 1 month ago

I would say the whole dirs if possible, not exactly sure which file might have caused it. Though I would rather bet on the json files inside scala-build/.bloop

yadavan88 commented 1 month ago

there were 3 json files in one dir and 4 in another. when compariging, there is no diff other than the dir path

image
yadavan88 commented 1 month ago

I have multiple scala-cli projects at work. When I added the docker directive, all those projects stopped working correctly in metals. I am not sure what is going wrong!

tgodzik commented 1 month ago

Maybe the problem is not exact directives but the order of adding them? :thinking:

tgodzik commented 1 month ago

Btw. Are you using Metals 1.3.3 ? Could you try with a newer version like 1.3.5 or latest snapshot? And maybe post again metals.log if it stills doesn't work

yadavan88 commented 1 month ago

No, mine is metals 1.3.5.

image

I again cleared the metals log. Cliecked on import, compile and connect to build server. Here si the current metals.log

2024.09.17 16:23:55 INFO  time: Imported build in 0.13s
2024.09.17 16:23:55 INFO  Correctly reloaded workspace
2024.09.17 16:23:55 INFO  running doctor check
2024.09.17 16:23:55 INFO  java targets: 
2024.09.17 16:23:58 INFO  time: indexed workspace in 3.17s
2024.09.17 16:24:33 INFO  no build target found for /Users/yadukrishnankrishnan/Downloads/metals-test 2/Main.scala. Using presentation compiler with project's scala-library version: 3.3.3
2024.09.17 16:24:34 INFO  no build target found for /Users/yadukrishnankrishnan/Downloads/metals-test 2/Main.scala. Using presentation compiler with project's scala-library version: 3.3.3
Sep 17, 2024 4:24:34 PM org.eclipse.lsp4j.jsonrpc.RemoteEndpoint handleCancellation
WARNING: Unmatched cancel notification for request id 12
2024.09.17 16:24:35 INFO  no build target found for /Users/yadukrishnankrishnan/Downloads/metals-test 2/Main.scala. Using presentation compiler with project's scala-library version: 3.3.3
2024.09.17 16:24:36 INFO  time: code lens generation in 2.15s
2024.09.17 16:24:51 INFO  no build target found for /Users/yadukrishnankrishnan/Downloads/metals-test 2/Main.scala. Using presentation compiler with project's scala-library version: 3.3.3
2024.09.17 16:24:51 INFO  no build target found for /Users/yadukrishnankrishnan/Downloads/metals-test 2/Main.scala. Using presentation compiler with project's scala-library version: 3.3.3
2024.09.17 16:24:52 INFO  no build target found for /Users/yadukrishnankrishnan/Downloads/metals-test 2/Main.scala. Using presentation compiler with project's scala-library version: 3.3.3
2024.09.17 16:24:52 INFO  no build target found for /Users/yadukrishnankrishnan/Downloads/metals-test 2/Main.scala. Using presentation compiler with project's scala-library version: 3.3.3
2024.09.17 16:24:53 INFO  no build target found for /Users/yadukrishnankrishnan/Downloads/metals-test 2/Main.scala. Using presentation compiler with project's scala-library version: 3.3.3
2024.09.17 16:24:59 INFO  Disconnecting from scala-cli session...
2024.09.17 16:24:59 INFO  Shut down connection with build server.
2024.09.17 16:24:59 INFO  Attempting to connect to the build server...
2024.09.17 16:24:59 INFO  Running BSP server List(/Users/yadukrishnankrishnan/Library/Application Support/Coursier/bin/.scala-cli.aux, --power, bsp, --json-options, /Users/yadukrishnankrishnan/Downloads/metals-test 2/.scala-build/ide-options-v2.json, --json-launcher-options, /Users/yadukrishnankrishnan/Downloads/metals-test 2/.scala-build/ide-launcher-options.json, --envs-file, /Users/yadukrishnankrishnan/Downloads/metals-test 2/.scala-build/ide-envs.json, /Users/yadukrishnankrishnan/Downloads/metals-test 2)
2024.09.17 16:24:59 INFO  tracing is enabled: /Users/yadukrishnankrishnan/Downloads/metals-test 2/.metals/bsp.trace.json
2024.09.17 16:25:00 INFO  time: Connected to build server in 1.39s
2024.09.17 16:25:00 INFO  Connected to Build server: scala-cli v1.5.0
2024.09.17 16:25:00 INFO  running doctor check
2024.09.17 16:25:00 INFO  java targets: 
2024.09.17 16:25:03 INFO  time: indexed workspace in 3.32s
2024.09.17 16:25:04 WARN  no build target for: /Users/yadukrishnankrishnan/Downloads/metals-test 2/Main.scala
2024.09.17 16:25:04 INFO  no build target found for /Users/yadukrishnankrishnan/Downloads/metals-test 2/Main.scala. Using presentation compiler with project's scala-library version: 3.3.3
2024.09.17 16:25:04 INFO  no build target found for /Users/yadukrishnankrishnan/Downloads/metals-test 2/Main.scala. Using presentation compiler with project's scala-library version: 3.3.3
2024.09.17 16:25:06 INFO  Disconnecting from scala-cli session...
2024.09.17 16:25:06 INFO  Shut down connection with build server.
2024.09.17 16:25:06 INFO  Attempting to connect to the build server...
2024.09.17 16:25:06 INFO  Running BSP server List(/Users/yadukrishnankrishnan/Library/Application Support/Coursier/bin/.scala-cli.aux, --power, bsp, --json-options, /Users/yadukrishnankrishnan/Downloads/metals-test 2/.scala-build/ide-options-v2.json, --json-launcher-options, /Users/yadukrishnankrishnan/Downloads/metals-test 2/.scala-build/ide-launcher-options.json, --envs-file, /Users/yadukrishnankrishnan/Downloads/metals-test 2/.scala-build/ide-envs.json, /Users/yadukrishnankrishnan/Downloads/metals-test 2)
2024.09.17 16:25:07 INFO  tracing is enabled: /Users/yadukrishnankrishnan/Downloads/metals-test 2/.metals/bsp.trace.json
2024.09.17 16:25:08 INFO  time: Connected to build server in 1.34s
2024.09.17 16:25:08 INFO  Connected to Build server: scala-cli v1.5.0
2024.09.17 16:25:08 INFO  running doctor check
2024.09.17 16:25:08 INFO  java targets: 
2024.09.17 16:25:11 INFO  time: indexed workspace in 3.31s
2024.09.17 16:25:11 WARN  no build target for: /Users/yadukrishnankrishnan/Downloads/metals-test 2/Main.scala
2024.09.17 16:25:11 INFO  no build target found for /Users/yadukrishnankrishnan/Downloads/metals-test 2/Main.scala. Using presentation compiler with project's scala-library version: 3.3.3
2024.09.17 16:25:11 INFO  no build target found for /Users/yadukrishnankrishnan/Downloads/metals-test 2/Main.scala. Using presentation compiler with project's scala-library version: 3.3.3
2024.09.17 16:25:52 INFO  no build target found for /Users/yadukrishnankrishnan/Downloads/metals-test 2/Main.scala. Using presentation compiler with project's scala-library version: 3.3.3
yadavan88 commented 1 month ago

WHen i click on restart build server, i get this

image
tgodzik commented 1 month ago

No, mine is metals 1.3.5.

Ach sorry, the first metals.log mentioned 1.3.3.

That looks like we are connecting, but the bsp server is processing something internally. Is the bsp.tace.json the same as previously? We might need to find the bsp server process and jstack it. I still can't seem to reproduce.

yadavan88 commented 1 month ago

Right now, it stopped working even without those directves. I found the bsp process and ran jstack. Here is it. jstack.log

//> using scala 3.3.3
// //> using file EmailClient.scala

@main def main() = {
    //EmailClient.send()
}

Even this code doesn't work now

yadavan88 commented 1 month ago

I cleaned up everything, killed all java processes. Still it is not working. I see this log in the metals doctor. What does this mean?

Metals Doctor
Metals Java: 17 from Eclipse Adoptium located at /Users/yadukrishnankrishnan/Library/Caches/Coursier/arc/https/github.com/adoptium/temurin17-binaries/releases/download/jdk-17%252B35/OpenJDK17-jdk_x64_mac_hotspot_17_35.tar.gz/jdk-17+35/Contents/Home

Metals Server version: 1.3.5

Below are listed the build targets for this workspace. One build target corresponds to one classpath. For example, normally one sbt project maps to two build targets: main and test.

Project's Java: 11.0.19 located at /Users/yadukrishnankrishnan/Library/Caches/Coursier/arc/https/github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.19%252B7/OpenJDK11U-jdk_x64_mac_hotspot_11.0.19_7.tar.gz/jdk-11.0.19+7/Contents/Home

Build definition is coming from scala-cli.

Build server currently being used is scala-cli v1.5.0. ([Reset](command:metals.reset-choice?%5B%22Build+server+selection%22%5D))

⚠️ No build targets were detected in this workspace so most functionality won't work.

Make sure the workspace directory '/Users/yadukrishnankrishnan/Downloads/metals-test 2' matches the root of your build.
Try removing the directories .metals/ and .bloop/, then restart metals And import the build again.
tgodzik commented 1 month ago

Looks like Scala CLi is not returning any build targets, so it seems to be hanging somewhere. Is there any additional process started aside from Bloop and Metals? The jstack you sent is Bloop server itself and everything seems to be ok there.

yadavan88 commented 1 month ago

I had killed all java processes and started the metals again in the same dir. Now I can see 2 java processes running.

  1. related to daemon:/Users/yadukrishnankrishnan/Library/Caches/ScalaCli/bloop/daemon
  2. related to scala.meta.metals.Main
yadavan88 commented 1 month ago

test-met.zip

Here is the sample zip file. The docker directive is commented out. I am doing the steps as follows exactly:

yadavan88 commented 1 month ago

@tgodzik Now, I added {"powerOptions":{"power":true}} again into the ide launcher options. Did clean compile and it worked! However, it still show the "Starts Metals", but also it shows then "run" dialog. I am sure that this was not working yesterday. Not sure how or why!

image