From 6b8b00812683122af3380e197bf66d1b7aeacef0 Mon Sep 17 00:00:00 2001 From: Jakub Ciesluk <323892@uwr.edu.pl> Date: Thu, 25 Jan 2024 09:49:30 +0100 Subject: [PATCH] improvement: Add test for Bazel semanticdb --- .../meta/internal/metals/JavaTarget.scala | 2 +- .../internal/metals/MetalsEnrichments.scala | 8 +- .../meta/internal/metals/ScalaTarget.scala | 2 +- .../scala/tests/bazel/BazelLspSuite.scala | 82 ++++++++++++++++--- 4 files changed, 79 insertions(+), 15 deletions(-) diff --git a/metals/src/main/scala/scala/meta/internal/metals/JavaTarget.scala b/metals/src/main/scala/scala/meta/internal/metals/JavaTarget.scala index a08535c4c70..07320b37711 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/JavaTarget.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/JavaTarget.scala @@ -49,7 +49,7 @@ case class JavaTarget( def sourceVersion: Option[String] = javac.sourceVersion - def targetroot: Option[AbsolutePath] = javac.targetroot.map(_.toTargetRoot) + def targetroot: Option[AbsolutePath] = javac.targetroot.map(_.resolveIfJar) /** * Typically to verify that SemanticDB is enabled correctly we check the javacOptions to ensure diff --git a/metals/src/main/scala/scala/meta/internal/metals/MetalsEnrichments.scala b/metals/src/main/scala/scala/meta/internal/metals/MetalsEnrichments.scala index dc1459fc6ad..39b8836a62c 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/MetalsEnrichments.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/MetalsEnrichments.scala @@ -533,7 +533,13 @@ object MetalsEnrichments filename.endsWith(".jar") || filename.endsWith(".srcjar") } - def toTargetRoot: AbsolutePath = { + /** + * Bazelbsp provides us with a path to a jar. + * SemanticDB files are store in the same directory as the jar file. + * + * Example: `/path/hello.jar -> /path/_semanticdb/hello` + */ + def resolveIfJar: AbsolutePath = { if (path.isJar) { val filename = path.toNIO.getFileName.toString val filename0 = filename.stripSuffix(".jar").stripSuffix(".srcjar") diff --git a/metals/src/main/scala/scala/meta/internal/metals/ScalaTarget.scala b/metals/src/main/scala/scala/meta/internal/metals/ScalaTarget.scala index 0159ce74ce3..821b2918b08 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/ScalaTarget.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/ScalaTarget.scala @@ -107,7 +107,7 @@ case class ScalaTarget( private def containsSource3 = scalac.getOptions().asScala.exists(opt => opt.startsWith("-Xsource:3")) - def targetroot: AbsolutePath = scalac.targetroot(scalaVersion).toTargetRoot + def targetroot: AbsolutePath = scalac.targetroot(scalaVersion).resolveIfJar def scalaPlatform: ScalaPlatform = scalaInfo.getPlatform() diff --git a/tests/slow/src/test/scala/tests/bazel/BazelLspSuite.scala b/tests/slow/src/test/scala/tests/bazel/BazelLspSuite.scala index 7d5a0ef5cc8..6d798d03d2d 100644 --- a/tests/slow/src/test/scala/tests/bazel/BazelLspSuite.scala +++ b/tests/slow/src/test/scala/tests/bazel/BazelLspSuite.scala @@ -66,7 +66,7 @@ class BazelLspSuite |""".stripMargin, ) _ <- server.didChange(s"BUILD") { text => - text.replace("\"main\"", "\"main1\"") + text.replace("\"hello\"", "\"hello1\"") } _ = assertNoDiff(client.workspaceMessageRequests, "") _ = client.importBuildChanges = ImportBuildChanges.yes @@ -95,7 +95,7 @@ class BazelLspSuite // We dismissed the import request, so bsp should not be configured _ = assert(!bazelBspConfig.exists) _ <- server.didChange(s"BUILD") { text => - text.replace("\"main\"", "\"main1\"") + text.replace("\"hello\"", "\"hello1\"") } _ <- server.didSave(s"BUILD")(identity) _ = assertNoDiff(client.workspaceMessageRequests, "") @@ -144,7 +144,7 @@ class BazelLspSuite // We dismissed the import request, so bsp should not be configured _ = assert(!bazelBspConfig.exists) _ <- server.didChange(s"BUILD") { text => - text.replace("\"main\"", "\"main1\"") + text.replace("\"hello\"", "\"hello1\"") } _ <- server.didSave(s"BUILD")(identity) _ = assertNoDiff(client.workspaceMessageRequests, "") @@ -161,17 +161,17 @@ class BazelLspSuite _ = assertNoDiff( result.value.linesIterator.take(14).mkString("\n"), """|Target - | @//:lib + | @//:hello1 | |Tags - | library + | application | |Languages | scala | |Capabilities | Debug <- NOT SUPPORTED - | Run <- NOT SUPPORTED + | Run | Test <- NOT SUPPORTED | Compile""".stripMargin, ) @@ -188,21 +188,72 @@ class BazelLspSuite } } + // TODO: Unignore this test + test("references".flaky) { + cleanWorkspace() + for { + _ <- initialize( + BazelBuildLayout(workspaceLayout, V.scala213, bazelVersion) + ) + _ <- server.didOpen("Hello.scala") + references <- server.references("Hello.scala", "hello") + _ = assertNoDiff( + references, + """|Hello.scala:4:7: info: reference + | def hello: String = "Hello" + | ^^^^^ + |Bar.scala:5:24: info: reference + | def hi = new Hello().hello + | ^^^^^ + |""".stripMargin, + ) + _ <- server.didOpen("Main.scala") + references <- server.references("Hello.scala", "hello") + _ = assertNoDiff( + references, + """|Hello.scala:4:7: info: reference + | def hello: String = "Hello" + | ^^^^^ + |Main.scala:4:23: info: reference + |def msg = new Hello().hello + | ^^^^^ + |Bar.scala:5:24: info: reference + | def hi = new Hello().hello + | ^^^^^ + |""".stripMargin, + ) + } yield () + } + private val workspaceLayout = s"""|/BUILD + |load("@io_bazel_rules_scala//scala:scala_toolchain.bzl", "scala_toolchain") |load("@io_bazel_rules_scala//scala:scala.bzl", "scala_binary", "scala_library") | + |scala_toolchain( + | name = "semanticdb_toolchain_impl", + | enable_semanticdb = True, + | semanticdb_bundle_in_jar = False, + | visibility = ["//visibility:public"], + |) + | + |toolchain( + | name = "semanticdb_toolchain", + | toolchain = "semanticdb_toolchain_impl", + | toolchain_type = "@io_bazel_rules_scala//scala:toolchain_type", + | visibility = ["//visibility:public"], + |) + | |scala_library( - | name = "lib", - | srcs = ["Hello.scala"], - | deps = [], + | name = "hello_lib", + | srcs = ["Hello.scala", "Bar.scala"], |) | |scala_binary( - | name = "main", + | name = "hello", | srcs = ["Main.scala"], - | main_class = "hello", - | deps = [":lib"], + | main_class = "main", + | deps = [":hello_lib"], |) | |/Hello.scala @@ -213,6 +264,13 @@ class BazelLspSuite | |} | + |/Bar.scala + |package examples.scala3 + | + |class Bar { + | def bar: String = "bar" + | def hi = new Hello().hello + |} | |/Main.scala |import examples.scala3.Hello