Skip to content

Commit

Permalink
improvement: Add test for Bazel semanticdb
Browse files Browse the repository at this point in the history
  • Loading branch information
jkciesluk authored and tgodzik committed Jan 26, 2024
1 parent a5e63e3 commit 6b8b008
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
82 changes: 70 additions & 12 deletions tests/slow/src/test/scala/tests/bazel/BazelLspSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, "")
Expand Down Expand Up @@ -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, "")
Expand All @@ -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,
)
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 6b8b008

Please sign in to comment.