Skip to content

Commit

Permalink
Make sure that evalaution runs with a proper classloader
Browse files Browse the repository at this point in the history
Previously, we would run evalaution with the default mdoc classloader, which might have not contained things like default akka config. Now, we run it in a Thread with the specific classloader set.

Fixes scalameta/metals#2525
  • Loading branch information
tgodzik committed Mar 26, 2021
1 parent 5df6290 commit 2429a02
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 24 deletions.
64 changes: 40 additions & 24 deletions mdoc/src/main/scala/mdoc/internal/markdown/MarkdownBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import mdoc.internal.document.MdocNonFatal
import mdoc.internal.CompatClassloader
import java.nio.file.Path
import java.nio.file.Paths
import java.util.concurrent.atomic.AtomicReference

object MarkdownBuilder {

Expand Down Expand Up @@ -40,31 +41,35 @@ object MarkdownBuilder {
val ctor = cls.getDeclaredConstructor()
ctor.setAccessible(true)
val doc = ctor.newInstance().asInstanceOf[DocumentBuilder].$doc
try {
doc.build(instrumentedInput)
} catch {
case e: DocumentException =>
val index = e.sections.length - 1
val input = sectionInputs(index).input
val pos =
if (e.pos.isEmpty) {
Position.Range(input, 0, 0)
} else {
val slice = Position.Range(
input,
e.pos.startLine,
e.pos.startColumn,
e.pos.endLine,
e.pos.endColumn
)
slice.toUnslicedPosition
}
reporter.error(pos, e.getCause)
Document(instrumentedInput, e.sections)
case MdocNonFatal(e) =>
reporter.error(e)
Document.empty(instrumentedInput)
var evaluatedDoc = Document.empty(instrumentedInput)
runInClassLoader(cls.getClassLoader()) { () =>
try {
evaluatedDoc = doc.build(instrumentedInput)
} catch {
case e: DocumentException =>
val index = e.sections.length - 1
val input = sectionInputs(index).input
val pos =
if (e.pos.isEmpty) {
Position.Range(input, 0, 0)
} else {
val slice = Position.Range(
input,
e.pos.startLine,
e.pos.startColumn,
e.pos.endLine,
e.pos.endColumn
)
slice.toUnslicedPosition
}
reporter.error(pos, e.getCause)
evaluatedDoc = Document(instrumentedInput, e.sections)
case MdocNonFatal(e) =>
reporter.error(e)
evaluatedDoc = Document.empty(instrumentedInput)
}
}
evaluatedDoc
case None =>
// An empty document will render as the original markdown
Document.empty(instrumentedInput)
Expand Down Expand Up @@ -92,6 +97,17 @@ object MarkdownBuilder {
new MarkdownCompiler(fullClasspath.syntax, scalacOptions)
}

private def runInClassLoader(classloader: ClassLoader)(f: () => Unit) = {
val thread = new Thread {
override def run: Unit = {
f()
}
}
thread.setContextClassLoader(classloader)
thread.start()
thread.join()
}

private def defaultClasspath(fn: Path => Boolean): Classpath = {
val paths =
CompatClassloader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,22 @@ class WorksheetSuite extends BaseSuite {
|""".stripMargin
)

checkDecorations(
"akka".tag(SkipScala3).tag(SkipScala211),
"""|import $dep.`com.typesafe.akka::akka-actor:2.6.13`
|import akka.actor.ActorSystem
|
|implicit val system = ActorSystem("worksheet")
|
|""".stripMargin,
"""|import $dep.`com.typesafe.akka::akka-actor:2.6.13`
|import akka.actor.ActorSystem
|
|<implicit val system = ActorSystem("worksheet")> // : ActorSystem = akka...
|system: ActorSystem = akka://worksheet
|""".stripMargin
)

checkDecorations(
"placeholder",
"""|def x = 1 -> 2
Expand Down

0 comments on commit 2429a02

Please sign in to comment.