Skip to content

Commit cd1568b

Browse files
committed
Use correct parent classloader for worksheets.
Instead of passing in `null` when we extend ClassLoader in our MdocClassLoader this passes in the actual parent since on Java +9 when using `null` that's the bootstrap classloader which doesn't contain modules like java.sql. Big thanks to @smarter pointing this out in scala/scala3#11658. Fixes scalameta#2187
1 parent f0ccc00 commit cd1568b

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

metals/src/main/scala/scala/meta/internal/worksheets/WorksheetClassLoader.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ package scala.meta.internal.worksheets
88
* allowing the Metals server to call mdoc instances from different Scala
99
* versions.
1010
*/
11-
class MdocClassLoader(parent: ClassLoader) extends ClassLoader(null) {
11+
class MdocClassLoader(parent: ClassLoader)
12+
extends ClassLoader(ClassLoader.getSystemClassLoader.getParent) {
1213
override def findClass(name: String): Class[_] = {
1314
val isShared =
1415
name.startsWith("mdoc.interfaces") || name.startsWith("coursierapi")

tests/unit/src/test/scala/tests/worksheets/WorksheetLspSuite.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,27 @@ class WorksheetLspSuite extends tests.BaseWorksheetLspSuite(V.scala212) {
9191
)
9292
} yield ()
9393
}
94+
// Ensure that on Java +9 that all modules are correctly loaded with the Mdoc
95+
// classloader including things like the java.sql module.
96+
// https://github.com/scalameta/metals/issues/2187
97+
test("classloader") {
98+
val path = "hi.worksheet.sc"
99+
for {
100+
_ <- server.initialize(
101+
s"""
102+
|/metals.json
103+
|{
104+
| "a": {}
105+
|}
106+
|/${path}
107+
|new java.sql.Date(100L)
108+
|""".stripMargin
109+
)
110+
_ <- server.didOpen(path)
111+
_ = assertNoDiff(
112+
client.workspaceDecorations,
113+
"new java.sql.Date(100L) // : java.sql.Date = 1970-01-01"
114+
)
115+
} yield ()
116+
}
94117
}

0 commit comments

Comments
 (0)