Skip to content

Scala js mode validation #2630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 8, 2024
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
3 changes: 2 additions & 1 deletion modules/build/src/main/scala/scala/build/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,8 @@ object Build {
scaladocDir = scaladocDir,
scalaCompiler = scalaCompilerParamsOpt,
scalaJsOptions =
if (options.platform.value == Platform.JS) Some(options.scalaJsOptions.config(logger))
if (options.platform.value == Platform.JS)
Some(value(options.scalaJsOptions.config(logger)))
else None,
scalaNativeOptions =
if (options.platform.value == Platform.Native)
Expand Down
62 changes: 61 additions & 1 deletion modules/build/src/test/scala/scala/build/tests/BuildTests.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package scala.build.tests

import bloop.config.Config.LinkerMode
import ch.epfl.scala.bsp4j
import com.eed3si9n.expecty.Expecty.expect
import com.google.gson.Gson
Expand All @@ -18,14 +19,15 @@ import scala.build.options.{
InternalOptions,
JavaOpt,
MaybeScalaVersion,
ScalaJsMode,
ScalacOpt,
ScriptOptions,
ShadowingSeq
}
import scala.build.tastylib.TastyData
import scala.build.tests.TestUtil.*
import scala.build.tests.util.BloopServer
import scala.build.{BuildThreads, Directories, LocalRepo, Positioned}
import scala.build.{Build, BuildThreads, Directories, LocalRepo, Positioned}
import scala.meta.internal.semanticdb.TextDocuments
import scala.util.Properties
import scala.jdk.CollectionConverters.*
Expand Down Expand Up @@ -975,4 +977,62 @@ abstract class BuildTests(server: Boolean) extends TestUtil.ScalaCliBuildSuite {
expect(maybeBuild.left.exists(_.message.startsWith("Toolkits do not support Scala 2.12")))
}
}

for {
(modeStr, bloopMode) <-
Seq("fastLinkJs" -> LinkerMode.Debug, "fullLinkJs" -> LinkerMode.Release)
if server
} do {
test(s"bloop config for $modeStr") {
val testInputs = TestInputs(
os.rel / "Simple.scala" ->
"""//> using platform js
|def foo(): String = "foo"
|""".stripMargin
)
val jsLinkBuildOptions = defaultOptions.copy(
scalaOptions = defaultOptions.scalaOptions.copy(
scalaVersion = None
),
scalaJsOptions = defaultOptions.scalaJsOptions.copy(
mode = ScalaJsMode(Some(modeStr))
)
)
testInputs.withBuild(jsLinkBuildOptions, buildThreads, bloopConfigOpt) {
(_, _, maybeBuild) =>
maybeBuild match {
case Right(b: Build.Successful) =>
assert(b.project.scalaJsOptions.exists(_.mode == bloopMode))
case _ => fail("Build failed")
}

}
}

test(s"bloop config for noOpt and $modeStr") {
val testInputs = TestInputs(
os.rel / "Simple.scala" ->
"""//> using platform js
|def foo(): String = "foo"
|""".stripMargin
)
val noOptBuildOptions = defaultOptions.copy(
scalaOptions = defaultOptions.scalaOptions.copy(
scalaVersion = None
),
scalaJsOptions = defaultOptions.scalaJsOptions.copy(
mode = ScalaJsMode(Some(modeStr)),
noOpt = Some(true)
)
)
testInputs.withBuild(noOptBuildOptions, buildThreads, bloopConfigOpt) {
(_, _, maybeBuild) =>
maybeBuild match {
case Right(b: Build.Successful) =>
assert(b.project.scalaJsOptions.exists(_.mode == LinkerMode.Debug))
case _ => fail("Build failed")
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ class SourcesTests extends TestUtil.ScalaCliBuildSuite {
os.rel / "something.sc" ->
"""//> using jsVersion "1.8.0"
|//> using jsMode "mode"
|//> using jsNoOpt
|//> using jsModuleKind "commonjs"
|//> using jsCheckIr true
|//> using jsEmitSourceMaps true
Expand Down Expand Up @@ -529,11 +530,12 @@ class SourcesTests extends TestUtil.ScalaCliBuildSuite {
val jsConfig = jsOptions.linkerConfig(TestLogger())
expect(
jsOptions.version == Some("1.8.0"),
jsOptions.mode == Some("mode"),
jsOptions.mode.nameOpt.contains("mode"),
jsOptions.moduleKindStr == Some("commonjs"),
jsOptions.checkIr == Some(true),
jsOptions.emitSourceMaps == true,
jsOptions.dom == Some(true)
jsOptions.dom == Some(true),
jsOptions.noOpt == Some(true)
)
expect(
jsConfig.moduleKind == ScalaJsLinkerConfig.ModuleKind.CommonJSModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ final case class ScalaJsOptions(
@HelpMessage("The Scala.js mode, for `fastLinkJS` use one of [`dev`, `fastLinkJS` or `fast`], for `fullLinkJS` use one of [`release`, `fullLinkJS`, `full`]")
jsMode: Option[String] = None,

@Group(HelpGroup.ScalaJs.toString)
@HelpMessage("Disable optimalisation for Scala.js, overrides `--js-mode`")
@Tag(tags.implementation)
@Hidden
jsNoOpt: Option[Boolean] = None,

@HelpMessage("The Scala.js module kind: commonjs/common, esmodule/es, nomodule/none")
@Group(HelpGroup.ScalaJs.toString)
@Tag(tags.should)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ final case class SharedOptions(
import opts._
options.ScalaJsOptions(
version = jsVersion,
mode = jsMode,
mode = options.ScalaJsMode(jsMode),
moduleKindStr = jsModuleKind,
checkIr = jsCheckIr,
emitSourceMaps = jsEmitSourceMaps,
Expand All @@ -244,7 +244,8 @@ final case class SharedOptions(
avoidLetsAndConsts = jsAvoidLetsAndConsts,
moduleSplitStyleStr = jsModuleSplitStyle,
smallModuleForPackage = jsSmallModuleForPackage,
esVersionStr = jsEsVersion
esVersionStr = jsEsVersion,
noOpt = jsNoOpt
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ object ProjectDescriptor {
for (checkIr <- options.checkIr)
calls = calls :+ s".withCheckIR($checkIr)"

val withOptimizer = options.mode.contains("release")
val withOptimizer = options.fullOpt.getOrElse(false)
calls = calls :+ s".withOptimizer($withOptimizer)"
calls = calls :+ s".withClosureCompiler($withOptimizer)"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package scala.build.preprocessing.directives
import scala.build.EitherCps.{either, value}
import scala.build.directives.*
import scala.build.errors.BuildException
import scala.build.options.{BuildOptions, JavaOpt, ScalaJsOptions, ShadowingSeq}
import scala.build.options.{BuildOptions, JavaOpt, ScalaJsMode, ScalaJsOptions, ShadowingSeq}
import scala.build.{Logger, Positioned, options}
import scala.cli.commands.SpecificationLevel

Expand All @@ -16,6 +16,8 @@ import scala.cli.commands.SpecificationLevel
|
|`//> using jsMode` _value_
|
|`//> using jsNoOpt` _true|false_
|
|`//> using jsModuleKind` _value_
|
|`//> using jsSmallModuleForPackage` _value1_ _value2_ …
Expand Down Expand Up @@ -45,6 +47,7 @@ import scala.cli.commands.SpecificationLevel
final case class ScalaJs(
jsVersion: Option[String] = None,
jsMode: Option[String] = None,
jsNoOpt: Option[Boolean] = None,
jsModuleKind: Option[String] = None,
jsCheckIr: Option[Boolean] = None,
jsEmitSourceMaps: Option[Boolean] = None,
Expand All @@ -61,7 +64,7 @@ final case class ScalaJs(
def buildOptions: Either[BuildException, BuildOptions] = either {
val scalaJsOptions = ScalaJsOptions(
version = jsVersion,
mode = jsMode,
mode = ScalaJsMode(jsMode),
moduleKindStr = jsModuleKind,
checkIr = jsCheckIr,
emitSourceMaps = jsEmitSourceMaps.getOrElse(ScalaJsOptions().emitSourceMaps),
Expand All @@ -72,7 +75,8 @@ final case class ScalaJs(
avoidClasses = jsAvoidClasses,
avoidLetsAndConsts = jsAvoidLetsAndConsts,
moduleSplitStyleStr = jsModuleSplitStyleStr,
esVersionStr = jsEsVersionStr
esVersionStr = jsEsVersionStr,
noOpt = jsNoOpt
)
BuildOptions(
scalaJsOptions = scalaJsOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,27 @@ trait RunScalaJsTestDefinitions { _: RunTestDefinitions =>
test(s"simple script JS in fullLinkJs mode") {
val output = simpleJsTestOutput("--js-mode", "fullLinkJs", "-v", "-v", "-v")
expect(output.contains("--fullOpt"))

expect(!output.contains("--fastOpt"))
expect(!output.contains("--noOpt"))
}

test(s"simple script JS in fastLinkJs mode") {
val output = simpleJsTestOutput("--js-mode", "fastLinkJs", "-v", "-v", "-v")
expect(output.contains("--fastOpt"))

expect(!output.contains("--fullOpt"))
expect(!output.contains("--noOpt"))
}

test(s"simple script JS with noOpt") {
val output = simpleJsTestOutput("--js-mode", "fullLinkJs", "--js-no-opt", "-v", "-v", "-v")
expect(output.contains("--noOpt"))

expect(!output.contains("--fastOpt"))
expect(!output.contains("--fullOpt"))
}

test("without node on the PATH") {
val fileName = "simple.sc"
val message = "Hello"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import dependency._
import java.util.Locale

import scala.build.Logger
import scala.build.errors.UnrecognizedJsOptModeError
import scala.build.errors.{BuildException, UnrecognizedJsOptModeError}
import scala.build.internal.{Constants, ScalaJsLinkerConfig}

final case class ScalaJsOptions(
version: Option[String] = None,
mode: Option[String] = None,
mode: ScalaJsMode = ScalaJsMode(),
moduleKindStr: Option[String] = None,
checkIr: Option[Boolean] = None,
emitSourceMaps: Boolean = false,
Expand All @@ -26,27 +26,18 @@ final case class ScalaJsOptions(
esVersionStr: Option[String] = None,
noOpt: Option[Boolean] = None
) {
private val validFullLinkAliases = Set(
"release",
"fullLinkJs",
"full"
)
private val validFastLinkAliases = Set(
"dev",
"fastLinkJs",
"fast"
)

def fullOpt: Either[UnrecognizedJsOptModeError, Boolean] =
if (mode.isEmpty || mode.exists(validFullLinkAliases.union(validFastLinkAliases).contains))
Right(mode.exists(validFullLinkAliases.contains))
if (mode.isValid)
if (noOpt.contains(true))
Right(false)
else
Right(mode.nameOpt.exists(ScalaJsMode.validFullLinkAliases.contains))
else
Left(UnrecognizedJsOptModeError(
mode.get,
validFullLinkAliases.toSeq,
validFastLinkAliases.toSeq
mode.nameOpt.getOrElse("None"), // shouldn't happen since None is valid
ScalaJsMode.validFullLinkAliases.toSeq,
ScalaJsMode.validFastLinkAliases.toSeq
))

def platformSuffix: String =
"sjs" + ScalaVersion.jsBinary(finalVersion).getOrElse(finalVersion)
def jsDependencies(scalaVersion: String): Seq[AnyDependency] =
Expand Down Expand Up @@ -113,7 +104,9 @@ final case class ScalaJsOptions(

def finalVersion = version.map(_.trim).filter(_.nonEmpty).getOrElse(Constants.scalaJsVersion)

private def configUnsafe(logger: Logger): BloopConfig.JsConfig = {
private def configUnsafe(logger: Logger): Either[BuildException, BloopConfig.JsConfig] = for {
isFullOpt <- fullOpt
} yield {
val kind = moduleKind(logger) match {
case ScalaJsLinkerConfig.ModuleKind.CommonJSModule => BloopConfig.ModuleKindJS.CommonJSModule
case ScalaJsLinkerConfig.ModuleKind.ESModule => BloopConfig.ModuleKindJS.ESModule
Expand All @@ -124,7 +117,7 @@ final case class ScalaJsOptions(
BloopConfig.JsConfig(
version = finalVersion,
mode =
if (mode.contains("release")) BloopConfig.LinkerMode.Release
if isFullOpt then BloopConfig.LinkerMode.Release
else BloopConfig.LinkerMode.Debug,
kind = kind,
emitSourceMaps = emitSourceMaps,
Expand All @@ -135,7 +128,7 @@ final case class ScalaJsOptions(
)
}

def config(logger: Logger): BloopConfig.JsConfig =
def config(logger: Logger): Either[BuildException, BloopConfig.JsConfig] =
configUnsafe(logger)

def linkerConfig(logger: Logger): ScalaJsLinkerConfig = {
Expand All @@ -160,6 +153,25 @@ final case class ScalaJsOptions(
}
}

case class ScalaJsMode(nameOpt: Option[String] = None) {
lazy val isValid = nameOpt.isEmpty ||
nameOpt.exists(
ScalaJsMode.validFullLinkAliases.union(ScalaJsMode.validFastLinkAliases).contains
)
}
object ScalaJsMode {
val validFullLinkAliases = Set(
"release",
"fullLinkJs",
"full"
)
val validFastLinkAliases = Set(
"dev",
"fastLinkJs",
"fast"
)
}

object ScalaJsOptions {
implicit val hasHashData: HasHashData[ScalaJsOptions] = HasHashData.derive
implicit val monoid: ConfigMonoid[ScalaJsOptions] = ConfigMonoid.derive
Expand Down
5 changes: 5 additions & 0 deletions website/docs/reference/cli-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,11 @@ The Scala.js version (1.14.0 by default).

The Scala.js mode, for `fastLinkJS` use one of [`dev`, `fastLinkJS` or `fast`], for `fullLinkJS` use one of [`release`, `fullLinkJS`, `full`]

### `--js-no-opt`

[Internal]
Disable optimalisation for Scala.js, overrides `--js-mode`

### `--js-module-kind`

The Scala.js module kind: commonjs/common, esmodule/es, nomodule/none
Expand Down
2 changes: 2 additions & 0 deletions website/docs/reference/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,8 @@ Add Scala.js options

`//> using jsMode` _value_

`//> using jsNoOpt` _true|false_

`//> using jsModuleKind` _value_

`//> using jsSmallModuleForPackage` _value1_ _value2_ …
Expand Down
6 changes: 6 additions & 0 deletions website/docs/reference/scala-command/cli-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,12 @@ The Scala.js version (1.14.0 by default).

The Scala.js mode, for `fastLinkJS` use one of [`dev`, `fastLinkJS` or `fast`], for `fullLinkJS` use one of [`release`, `fullLinkJS`, `full`]

### `--js-no-opt`

`IMPLEMENTATION specific` per Scala Runner specification

Disable optimalisation for Scala.js, overrides `--js-mode`

### `--js-module-kind`

`SHOULD have` per Scala Runner specification
Expand Down
2 changes: 2 additions & 0 deletions website/docs/reference/scala-command/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ Add Scala.js options

`//> using jsMode` _value_

`//> using jsNoOpt` _true|false_

`//> using jsModuleKind` _value_

`//> using jsSmallModuleForPackage` _value1_ _value2_ …
Expand Down
Loading