ValdemarGr / mezel

Scala BSP implementation for Bazel.
Apache License 2.0
17 stars 2 forks source link

scala 3 support? #12

Open name-snrl opened 11 months ago

name-snrl commented 11 months ago

When I try to run mezel with the scala 3 example, it complains about the lack of semanticdb:

### //src:lib
  - target type: Scala 3.1.0
  - goto functionality: metalsDecode:file%3A%2F%2F%2Ftest%2F%2F%2Fsrc%3Alib.metals-buildtarget
  - compilation: ✅ 
  - diagnostics: ✅ 
  - interactive: ✅ 
  - semanticdb: ❌
  - debugging: ❌
  - java: ⚠️ 
  - recommendation: Run 'Import build' to enable code navigation.

I haven't read the code much, but it looks like it uses scala 2 options to generate semanticdb, I guess this is a problem for me?

https://github.com/ValdemarGr/mezel/blob/89ab108cdff0773458b647dea884521b0627119e/src/main/scala/mezel/BSPServer.scala#L523-L527

ValdemarGr commented 11 months ago

Thanks for the report. I'll look into it.

ValdemarGr commented 11 months ago

I got semanticdb working for Scala 3 by adding the correct flags. You can take a look at the ./example project

### //example/src/main/scala/example:example
  - target type: Scala 3.3.0
  - goto functionality: metalsDecode:file%3A%2F%2F%2Fmezel%2F%2F%2Fexample%2Fsrc%2Fmain%2Fscala%2Fexample%3Aexample.metals-buildtarget
  - compilation: ✅ 
  - diagnostics: ✅ 
  - interactive: ✅ 
  - semanticdb: ✅ 
  - debugging: ❌
  - java: ⚠️ 

However I noticed that rules_scala don't seem to generate diagnosticsproto files for Scala 3, so I cannot provide editor diagnostics (errors and warnings).

It is marked as a output of the Scalac action, and is in fact generated if there are no errors, but if errors occur in a target the diagnostics file is not emitted, if warnings but no errors occur the file will be present but it will be empty.

bazel aquery 'mnemonic("Scalac", //example/src/main/scala/example)' | grep "Outputs:.*diagnostics.*"
#   Outputs: [bazel-out/k8-fastbuild/bin/example/src/main/scala/example/_semanticdb/example/META-INF/semanticdb/example/src/main/scala/example/Main.scala.semanticdb, bazel-out/k8-fastbuild/bin/example/src/main/scala/example/example.diagnosticsproto, bazel-out/k8-fastbuild/bin/example /src/main/scala/example/example.jar, bazel-out/k8-fastbuild/bin/example/src/main/scala/example/example.sdeps, bazel-out/k8-fastbuild/bin/example/src/main/scala/example/example.statsfile]

I think this might be a bug (or missing feature) that needs to be addressed in rules_scala, I'll look into it. For now, I'll publish a version that won't expect diagnosticsproto to exist on the filesystem, so navigation and type hits should work for Scala 3.

ValdemarGr commented 11 months ago

After some quick investigation it seems that Scala 3 diagnostics are a missing feature.

name-snrl commented 11 months ago

by adding the correct flags

Thanks for the tests, glad to hear it's working. I haven't had a chance to test it since yesterday.

rules_scala don't seem to generate diagnosticsproto

This is not a problem for us, because we are using our own forked https://github.com/higherkindness/rules_scala rules and diagnosticproto is not yet implemented.

name-snrl commented 11 months ago

at the ./example project

At first I tried the example, but could not succeed. Then I thought "ok, I'll try with scala 2, from the beginning" and created this repo with two branches, bazel and sbt versions.

https://github.com/name-snrl/mezel_example

The main feature I expect is code navigation, but goto-definition only works on the sbt branch. For example, if I try to navigate to IOApp, it doesn't work on the bazel branch. Any ideas?

Cold start logs, after something like:

bazel clean --expunge &&
    pkill java &&
    rm -rf .metals/ .bloop/ project/ target/ ~/.cache/{bazel/,coursier/,bloop/,bazelisk/} &&
    git restore .

bazel.log sbt.log

ValdemarGr commented 11 months ago

The aspect does not catch rules of kind scala_binary. I'll add that to the aspect c06d9d88568f4ad09c4fca072b57839374f83720 fixes it.

If you use scala_library it should also work.

load("@io_bazel_rules_scala//scala:scala.bzl", "scala_library")

scala_library(
    name = "example",
    srcs = ["Main.scala"],
    # main_class = "example.Main",
    visibility = ["//visibility:public"],
    deps = [
      "@maven//:org_typelevel_cats_effect_2_13",
    ],
)

image

name-snrl commented 11 months ago

If you use scala_library it should also work.

Hm... But as I see, goto definition still doesn't work.

image

I thought the main problem with bazel-bsp was semanticdb, that after providing semanticdb to metals, all code navigation like goto definition would start working. Am I still doing something wrong? Or do we need to add something else to make it work?

p.s. I have autocomplete like in your screenshot

ValdemarGr commented 11 months ago

Sorry for the inconvenience, the issue you hit was twofold:

https://github.com/ValdemarGr/mezel/assets/22582195/ceaa1256-8bc8-45d6-9f39-6404878ba8b0