Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion mdoc-js/src/main/scala/mdoc/modifiers/JsModifier.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mdoc.modifiers

import mdoc.{OnLoadContext, PostProcessContext, PreModifierContext}
import mdoc.internal.cli.InputFile
import mdoc.internal.cli.ScalacOptions
import mdoc.internal.io.ConsoleReporter
import mdoc.internal.livereload.Resources
import mdoc.internal.markdown.{CodeBuilder, Gensym, MarkdownCompiler}
Expand Down Expand Up @@ -91,14 +92,15 @@ class JsModifier extends mdoc.PreModifier {
reporter = ctx.reporter
val compileClasspath = Classpath(classpath)
val linkerClasspath = Classpath(config.classpath)
val parsedScalacOptions = ScalacOptions.parse(scalacOptions)

val newClasspathHash =
(classpath, linkerClasspath, scalacOptions, config.fullOpt).hashCode()
// Reuse the linker and compiler when the classpath+scalacOptions haven't changed
// to speed up unit tests by nearly 2x.
if (classpathHash != newClasspathHash) {
classpathHash = newClasspathHash
maybeCompiler = Some(new MarkdownCompiler(classpath, scalacOptions, target))
maybeCompiler = Some(new MarkdownCompiler(classpath, parsedScalacOptions, target))

val loader =
ScalaJSClassloader.create(linkerClasspath.entries.map(_.toURI.toURL()).toArray)
Expand Down
4 changes: 2 additions & 2 deletions mdoc-sbt/src/main/scala/mdoc/MdocPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ object MdocPlugin extends AutoPlugin with MdocPluginCompat {
if (withoutPrefix eq o) o
else pluginPrefix + converter.toPath(VirtualFileRef.of(withoutPrefix))
}
.mkString(" ")
.mkString("\n")
)
val classpath = ListBuffer.empty[File]
// Can't use fullClasspath.value because it introduces cyclic dependency between
Expand Down Expand Up @@ -233,7 +233,7 @@ object MdocPlugin extends AutoPlugin with MdocPluginCompat {

props.put(
s"js-scalac-options",
scalacOptions.mkString(" ")
scalacOptions.mkString("\n")
)
props.put(
s"js-classpath",
Expand Down
29 changes: 29 additions & 0 deletions mdoc-sbt/src/sbt-test/sbt-mdoc/scalac-options-spaces/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
ThisBuild / scalaVersion := "3.3.7"

enablePlugins(MdocPlugin)

// -Wconf option containing a space in the msg regex pattern.
// Previously this caused mdoc to break the option when re-splitting
// the scalacOptions string by whitespace.
scalacOptions += "-Wconf:msg=unused import:s"

TaskKey[Unit]("check") := {
val file = mdocOut.value / "readme.md"
val obtained = IO.read(file)
IO.delete(file)
println(s"----${scalaVersion.value}----")
println(obtained)
println()
assert(
obtained.trim ==
"""
# Test
```scala
val x = 1
// x: Int = 1
```
""".trim,
"\"\"\"\n" + obtained + "\n\"\"\""
)
println("------------------")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Test
```scala mdoc
val x = 1
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("org.scalameta" % "sbt-mdoc" % sys.props("plugin.version"))
3 changes: 3 additions & 0 deletions mdoc-sbt/src/sbt-test/sbt-mdoc/scalac-options-spaces/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Test that scalacOptions containing spaces (e.g. -Wconf with regex patterns) work correctly
> mdoc
> check
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import mdoc.internal.worksheets.Compat._

class MarkdownCompiler(
classpath: String,
val scalacOptions: String,
val scalacOptions: List[String],
target: AbstractFile = new VirtualDirectory("(memory)", None)
) {
private val settings = new Settings()
Expand All @@ -45,7 +45,7 @@ class MarkdownCompiler(
// https://github.com/scala/bug/issues/9824
// https://github.com/scalameta/mdoc/issues/124
settings.Ydelambdafy.value = "inline"
settings.processArgumentString(scalacOptions)
settings.processArguments(scalacOptions.filter(_.nonEmpty), processAll = true)

def classpathEntries: Seq[Path] = global.classPath.asURLs.map(url => Paths.get(url.toURI()))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class MarkdownDriver(val settings: List[String]) extends Driver {

class MarkdownCompiler(
classpath: String,
val scalacOptions: String,
val scalacOptions: List[String],
target: AbstractFile = new VirtualDirectory("(memory)")
) {

Expand All @@ -82,7 +82,7 @@ class MarkdownCompiler(
case head :: tail => head :: removeDuplicatedOptions(tail)
case Nil => Nil

val options = removeDuplicatedOptions(scalacOptions.split("\\s+").filter(_.nonEmpty).toList)
val options = removeDuplicatedOptions(scalacOptions.filter(_.nonEmpty))
val settings =
options ::: defaultFlags ::: "-classpath" :: classpath :: Nil

Expand Down
3 changes: 2 additions & 1 deletion mdoc/src/main/scala/mdoc/internal/cli/Context.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ object Context {
new Context(options, reporter, compiler)
}
def fromOptions(options: Settings, reporter: Reporter = ConsoleReporter.default): Context = {
val compiler = MarkdownBuilder.fromClasspath(options.classpath, options.scalacOptions)
val compiler =
MarkdownBuilder.fromClasspath(options.classpath, ScalacOptions.parse(options.scalacOptions))
fromCompiler(options, reporter, compiler)
}
}
5 changes: 3 additions & 2 deletions mdoc/src/main/scala/mdoc/internal/cli/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import scala.meta.io.Classpath
import coursierapi.Dependency
import mdoc.internal.markdown.MarkdownCompiler
import mdoc.internal.markdown.MarkdownBuilder
import mdoc.internal.cli.ScalacOptions
import scala.meta.io.AbsolutePath
import coursierapi.Repository
import mdoc.internal.markdown.Instrumented
Expand All @@ -30,9 +31,9 @@ object Dependencies {
Classpath(Classpath(settings.classpath).entries ++ jars.map(AbsolutePath(_)))
val scalacOptions = instrumented.scalacOptionImports match {
case Nil =>
settings.scalacOptions
ScalacOptions.parse(settings.scalacOptions)
case options =>
s"${settings.scalacOptions} ${options.map(_.value).mkString(" ")}"
ScalacOptions.parse(settings.scalacOptions) ++ options.map(_.value)
}
MarkdownBuilder.fromClasspath(classpath.syntax, scalacOptions)
}
Expand Down
16 changes: 16 additions & 0 deletions mdoc/src/main/scala/mdoc/internal/cli/ScalacOptions.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package mdoc.internal.cli

object ScalacOptions {

private val OptionSeparator = "\n"

def parse(s: String): List[String] =
if (s.isEmpty) Nil
else if (s.contains(OptionSeparator))
s.split(OptionSeparator).filter(_.nonEmpty).toList
else
s.split("\\s+").filter(_.nonEmpty).toList

def serialize(options: Seq[String]): String =
options.mkString(OptionSeparator)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import java.util.concurrent.atomic.AtomicReference

object MarkdownBuilder {

def default(): MarkdownCompiler = fromClasspath(classpath = "", scalacOptions = "")
def default(): MarkdownCompiler = fromClasspath(classpath = "", scalacOptions = Nil)

def buildDocument(
compiler: MarkdownCompiler,
Expand Down Expand Up @@ -78,7 +78,7 @@ object MarkdownBuilder {
EvaluatedDocument(doc, sectionInputs)
}

def fromClasspath(classpath: String, scalacOptions: String): MarkdownCompiler = {
def fromClasspath(classpath: String, scalacOptions: List[String]): MarkdownCompiler = {
val fullClasspath =
if (classpath.isEmpty) defaultClasspath(_ => true)
else {
Expand Down
8 changes: 8 additions & 0 deletions tests/jsdocs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ abstract class BaseMarkdownSuite extends tests.BaseSuite {
myStdout.reset()
new ConsoleReporter(new PrintStream(myStdout))
}
protected def scalacOptions: String = ""
protected def scalacOptions: List[String] = Nil
private val compiler = MarkdownBuilder.fromClasspath("", scalacOptions)
private def newContext(settings: Settings, reporter: ConsoleReporter) = {
Context.fromSettings(settings, reporter)
Expand Down
25 changes: 15 additions & 10 deletions tests/unit/src/test/scala/tests/imports/ScalacSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ class ScalacSuite extends BaseMarkdownSuite {

for (
(name, importStyle) <- Seq(
"ammonite" -> "import $scalac.`-Wunused:imports -Xfatal-warnings`",
"ammonite" ->
"""|import $scalac.`-Wunused:imports`
|import $scalac.`-Xfatal-warnings`""".stripMargin,
"using" -> "//> using option -Wunused:imports -Xfatal-warnings"
)
)
Expand All @@ -20,19 +22,20 @@ class ScalacSuite extends BaseMarkdownSuite {
|println(42)
|```
|""".stripMargin,
s"""|error: No warnings can be incurred under -Werror.
|warning: import-$name.md:4:1: Unused import
|import scala.util.Try
|^^^^^^^^^^^^^^^^^^^^^
|""".stripMargin,
compat = Map(
Compat.Scala213 ->
name match {
case "ammonite" =>
s"""|error: No warnings can be incurred under -Werror.
|warning: import-$name.md:5:19: Unused import
|import scala.util.Try
| ^^^
|""".stripMargin
case "using" =>
s"""|error: No warnings can be incurred under -Werror.
|warning: import-$name.md:4:19: Unused import
|import scala.util.Try
| ^^^
|""".stripMargin
)
}
)

check(
Expand All @@ -53,7 +56,9 @@ class ScalacSuite extends BaseMarkdownSuite {

for (
(name, importStyle) <- Seq(
"ammonite" -> "import $scalac.`-Wunused:imports -Xfatal-warnings`",
"ammonite" ->
"""|import $scalac.`-Wunused:imports`
|import $scalac.`-Xfatal-warnings`""".stripMargin,
"using" -> "//> using option -Wunused:imports -Xfatal-warnings"
)
)
Expand Down