Skip to content
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

Fix #1103: Scala 3 support for Scala.js #1187

Merged
merged 1 commit into from
Mar 1, 2021
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
38 changes: 31 additions & 7 deletions scalajslib/src/ScalaJSModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mill
package scalajslib

import mill.eval.{PathRef, Result}
import mill.scalalib.api.Util.isScala3
import mill.scalalib.Lib.resolveDependencies
import mill.scalalib.{DepSyntax, Lib, TestModule, TestRunner}
import mill.util.Ctx
Expand Down Expand Up @@ -147,12 +148,28 @@ trait ScalaJSModule extends scalalib.ScalaModule { outer =>
).map(PathRef(_))
}

def scalacOptions = super.scalacOptions() ++ {
if(isScala3(scalaVersion())) Seq("-scalajs")
else Seq.empty
}

override def scalacPluginIvyDeps = T{
super.scalacPluginIvyDeps() ++
Seq(ivy"org.scala-js:::scalajs-compiler:${scalaJSVersion()}")
super.scalacPluginIvyDeps() ++ {
if(isScala3(scalaVersion())) {
Seq.empty
} else {
Seq(ivy"org.scala-js:::scalajs-compiler:${scalaJSVersion()}")
}
}
}
override def scalaLibraryIvyDeps = T{
Seq(ivy"org.scala-js::scalajs-library:${scalaJSVersion()}")
if(isScala3(scalaVersion())) {
super.scalaLibraryIvyDeps() ++ Seq(
ivy"org.scala-js:scalajs-library_2.13:${scalaJSVersion()}"
)
} else {
Seq(ivy"org.scala-js::scalajs-library:${scalaJSVersion()}")
}
}

// publish artifact with name "mill_sjs0.6.4_2.12" instead of "mill_sjs0.6_2.12"
Expand All @@ -179,10 +196,17 @@ trait TestScalaJSModule extends ScalaJSModule with TestModule {
val bridgeOrInterface =
if (mill.scalalib.api.Util.scalaJSUsesTestBridge(scalaJSVersion())) "bridge"
else "interface"
Loose.Agg(
ivy"org.scala-js::scalajs-library:${scalaJSVersion()}",
ivy"org.scala-js::scalajs-test-$bridgeOrInterface:${scalaJSVersion()}"
)
if(isScala3(scalaVersion())) {
Loose.Agg(
ivy"org.scala-js:scalajs-library_2.13:${scalaJSVersion()}",
ivy"org.scala-js:scalajs-test-bridge_2.13:${scalaJSVersion()}"
)
} else {
Loose.Agg(
ivy"org.scala-js::scalajs-library:${scalaJSVersion()}",
ivy"org.scala-js::scalajs-test-$bridgeOrInterface:${scalaJSVersion()}"
)
}
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import utest._
object ArgsParserTests extends TestSuite {

def tests: Tests = Tests {
'one - {
test("one") {
val result = ArgsParser.parse("hello:world")
assert(
result.length == 2,
result == Seq("hello", "world")
)
}
'two - { // we fail this test to check testing in scala.js
test("two") { // we fail this test to check testing in scala.js
val result = ArgsParser.parse("hello:world")
assert(
result.length == 80
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import utest._
object MainTests extends TestSuite {

def tests: Tests = Tests {
'vmName - {
'containJs - {
test("vmName") {
test("containJs") {
assert(
Main.vmName.contains("js")
)
}
'containScala - {
test("containScala") {
assert(
Main.vmName.contains("Scala")
)
Expand Down
40 changes: 22 additions & 18 deletions scalajslib/test/src/HelloJSWorldTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import mill._
import mill.define.Discover
import mill.eval.{Evaluator, Result}
import mill.scalalib.{CrossScalaModule, DepSyntax, Lib, PublishModule, TestRunner}
import mill.scalalib.api.Util.isScala3
import mill.scalalib.publish.{Developer, License, PomSettings, VersionControl}
import mill.util.{TestEvaluator, TestUtil}
import utest._
Expand All @@ -23,11 +24,12 @@ object HelloJSWorldTests extends TestSuite {
}

object HelloJSWorld extends TestUtil.BaseModule {
val scalaVersions = Seq("2.13.3", "2.12.12", "2.11.12")
val scalaJSVersionsAndUseECMA2015 = Seq(("1.3.1", false), ("1.3.1", true), ("1.0.1", false), ("0.6.33", false))
val scalaVersions = Seq("2.13.3", "3.0.0-RC1", "2.12.12", "2.11.12")
val scalaJSVersionsAndUseECMA2015 = Seq(("1.4.0", false), ("1.3.1", true), ("1.0.1", false), ("0.6.33", false))
val matrix = for {
scala <- scalaVersions
(scalaJS, useECMAScript2015) <- scalaJSVersionsAndUseECMA2015
if !(isScala3(scala) && scalaJS != scalaJSVersionsAndUseECMA2015.head._1)
} yield (scala, scalaJS, useECMAScript2015)

object helloJsWorld extends Cross[BuildModule](matrix:_*)
Expand Down Expand Up @@ -88,7 +90,7 @@ object HelloJSWorldTests extends TestSuite {

val outPath = result.classes.path
val outputFiles = os.walk(outPath)
val expectedClassfiles = compileClassfiles(outPath, scalaJSVersion)
val expectedClassfiles = compileClassfiles(outPath, scalaVersion, scalaJSVersion)
assert(
outputFiles.toSet == expectedClassfiles,
evalCount > 0
Expand Down Expand Up @@ -145,7 +147,7 @@ object HelloJSWorldTests extends TestSuite {
}
'artifactId_06 - testArtifactId(HelloJSWorld.scalaVersions.head, "0.6.33", "hello-js-world_sjs0.6_2.13")
'artifactId_10 - testArtifactId(HelloJSWorld.scalaVersions.head, "1.0.1", "hello-js-world_sjs1_2.13")
'artifactId_1 - testArtifactId(HelloJSWorld.scalaVersions.head, "1.3.1", "hello-js-world_sjs1_2.13")
'artifactId_1 - testArtifactId(HelloJSWorld.scalaVersions.head, "1.4.0", "hello-js-world_sjs1_2.13")
}

def runTests(testTask: define.NamedTask[(String, Seq[TestRunner.Result])]): Map[String, Map[String, TestRunner.Result]] = {
Expand Down Expand Up @@ -200,14 +202,14 @@ object HelloJSWorldTests extends TestSuite {

'test - {
val cached = false
testAllMatrix((scala, scalaJS, _) => checkUtest(scala, scalaJS, cached), skipScala = _.startsWith("2.11."))
testAllMatrix((scala, scalaJS, _) => checkScalaTest(scala, scalaJS, cached))
testAllMatrix((scala, scalaJS, _) => checkUtest(scala, scalaJS, cached), skipScala = v => v.startsWith("2.11.") || isScala3(v))
testAllMatrix((scala, scalaJS, _) => checkScalaTest(scala, scalaJS, cached), skipScala = isScala3)
}

'testCached - {
val cached = false
testAllMatrix((scala, scalaJS, _) => checkUtest(scala, scalaJS, cached), skipScala = _.startsWith("2.11."))
testAllMatrix((scala, scalaJS, _) => checkScalaTest(scala, scalaJS, cached))
testAllMatrix((scala, scalaJS, _) => checkUtest(scala, scalaJS, cached), skipScala = v => v.startsWith("2.11.") || isScala3(v))
testAllMatrix((scala, scalaJS, _) => checkScalaTest(scala, scalaJS, cached), skipScala = isScala3)
}

def checkRun(scalaVersion: String, scalaJSVersion: String, useECMAScript2015: Boolean): Unit = {
Expand All @@ -233,26 +235,28 @@ object HelloJSWorldTests extends TestSuite {
}
}

def compileClassfiles(parentDir: os.Path, scalaJSVersion: String) = {
def compileClassfiles(parentDir: os.Path, scalaVersion: String, scalaJSVersion: String) = {
val inAllVersions = Set(
parentDir / "ArgsParser$.class",
parentDir / "ArgsParser$.sjsir",
parentDir / "ArgsParser.class",
parentDir / "Main.class",
parentDir / "Main$.class",
parentDir / "Main$delayedInit$body.class",
parentDir / "Main$.sjsir",
)
val scalaJSVersionSpecific = if(scalaJSVersion.startsWith("1.")) Set(
parentDir / "ArgsParser.sjsir",
parentDir / "Main.sjsir"
) else Set.empty
val scalaVersionSpecific = if(isScala3(scalaVersion)) Set(
parentDir / "ArgsParser.tasty",
parentDir / "Main.tasty"
) else Set(
parentDir / "Main$delayedInit$body.class",
parentDir / "Main$delayedInit$body.sjsir"
)

if (scalaJSVersion.startsWith("1.")) {
inAllVersions ++ Set(
parentDir / "ArgsParser.sjsir",
parentDir / "Main.sjsir"
)
} else {
inAllVersions
}
inAllVersions ++ scalaJSVersionSpecific ++ scalaVersionSpecific
}

def prepareWorkspace(): Unit = {
Expand Down
2 changes: 1 addition & 1 deletion scalajslib/test/src/NodeJSConfigTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object NodeJSConfigTests extends TestSuite {
val workspacePath = TestUtil.getOutPathStatic() / "hello-js-world"
val scalaVersion = "2.12.4"
val scalaJSVersion = "0.6.32"
val utestVersion = "0.6.3"
val utestVersion = "0.7.5"
val nodeArgsEmpty = List()
val nodeArgs2G = List("--max-old-space-size=2048")
val nodeArgs4G = List("--max-old-space-size=4096")
Expand Down
2 changes: 1 addition & 1 deletion scalalib/src/Lib.scala
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ object Lib{
else if (mill.scalalib.api.Util.isScala3(scalaVersion))
Agg(
// note that dotty-library has a binary version suffix, hence the :: is necessary here
ivy"$scalaOrganization::scala3-library:$scalaVersion".forceVersion()
ivy"$scalaOrganization::scala3-library::$scalaVersion".forceVersion()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why's that change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want it to be scala3-library_sjs1_3.0.0-RC1 on Scala.js

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that was obvious. Sorry for the poor question. I was wondering, why that works at all, as this method is also used outside of a Scala.js context. I would expect it to fail when compiling a simple scala project. Also this should affect Scala Native modules. My initial though was, that a Scala.js specific runtime/ compiler classpath should be applied to the ScalaJsModule, not the ScalaModule. You see me confused.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

platformSuffix works on any platform. It works correctly on ScalaModule because platformSuffix = "".
The main difference between Scala 2 and Scala 3 on Scala JS is that the Scala.js backend is not implemented as a compiler plugin but inside the Scala 3 compiler. So if Scala 2 removes the scala-library and replaces it with its scalajs-library (as you can see there is no super.scalacPluginIvyDeps() ++ in the Scala2 part of scalacPluginIvyDeps, which means that we don't want the scala-library from Scala but we'll use the Scala.js one instead), the Scala 3 compiler emits different scala3-library artifacts for every backend.
Regarding Scala Native, I expect that when it will be supported, its support will be done in a similar fashion as the Scala.js one, so there will be a scala3-library_native0.x_3... artifact that the ScalaNativeModule will automatically use.
I apologise in advance with the Scala.js team if I said something not completely precise.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for that explanation. And sorry for not knowing that specific part of the Mill API better. The platform suffix handling still feels a bit odd, but that is another discussion. PR looks good to me!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing to be sorry of! Thank you for the review and the merge 🙏😃

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I wasn't that wrong with my suspicion. This assumes we only build with plain Scala Module or one of the built-in platform versions ScalaJSModule or ScalaNativeModule. But the platform concept is more general and if one module sets it to be unequal ""`, this implementation will fail. Instead, we should revert that specific line and implement it more specifically in the both modules that need it.

E.g. imagine, someone tries to build a Mill plugin with Scala 3 (which is currently not supported), the you would run into the following issue:

object millroot extends MillBuildRootModule {
  override def scalaVersion = "3.3.0"
  // ..
}
>> mill --meta-level 1 ivyDepsTree
[build.sc] [30/52] resolvedIvyDeps | Downloading [2/2] artifacts (~0/0 bytes)
1 targets failed
resolvedIvyDeps
Resolution failed for 1 modules:
--------------------------------------------
  org.scala-lang:scala3-library_mill0.11_3:3.3.0
	not found: /Users/me/ivy2/local/org.scala-lang/scala3-library_mill0.11_3/3.3.0/ivys/ivy.xml
	not found: https://repo1.maven.org/maven2/org/scala-lang/scala3-library_mill0.11_3/3.3.0/scala3-library_mill0.11_3-3.3.0.pom

Example adapted from https://matrix.to/#/!zXvttAKLPnvdnmiYsY:matrix.am/$FU_LdtZ6RFNOtmpbIlCGGzlkzWAV0NPUaF2BE7F2sqI?via=matrix.am&via=gitter.im&via=matrix.org

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened PR #2739

)
else
Agg(
Expand Down