Skip to content

Commit 538a317

Browse files
committed
Fix scala-js#3661: Load linker reflectively
This gives us the following advantages: - Allows us to give resources to the linker that the build itself compiles first (unblocks scala-js#3537). - Hides linker dependencies away from other sbt plugins. Notably the Google Closure Compiler and Guava (fixes scala-js#3193).
1 parent 5328166 commit 538a317

File tree

8 files changed

+279
-53
lines changed

8 files changed

+279
-53
lines changed

Jenkinsfile

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def Tasks = [
298298
"bootstrap": '''
299299
setJavaVersion $java
300300
npm install &&
301-
sbt ++$scala linker/test &&
301+
sbt ++$scala! linker/test &&
302302
sbt ++$scala irJS/test linkerJS/test &&
303303
sbt 'set scalaJSStage in Global := FullOptStage' \
304304
'set scalaJSStage in testSuite := FastOptStage' \
@@ -315,14 +315,14 @@ def Tasks = [
315315
"tools": '''
316316
setJavaVersion $java
317317
npm install &&
318-
sbt ++$scala ir/test logging/compile linkerInterface/compile \
318+
sbt ++$scala! ir/test logging/compile linkerInterface/compile \
319319
linker/compile jsEnvs/test nodeJSEnv/test testAdapter/test \
320320
ir/mimaReportBinaryIssues logging/mimaReportBinaryIssues \
321321
linkerInterface/mimaReportBinaryIssues linker/mimaReportBinaryIssues \
322322
jsEnvs/mimaReportBinaryIssues jsEnvsTestKit/mimaReportBinaryIssues \
323323
nodeJSEnv/mimaReportBinaryIssues \
324324
testAdapter/mimaReportBinaryIssues &&
325-
sbt ++$scala ir/compile:doc logging/compile:doc \
325+
sbt ++$scala! ir/compile:doc logging/compile:doc \
326326
linkerInterface/compile:doc \
327327
linker/compile:doc jsEnvs/compile:doc \
328328
jsEnvsTestKit/compile:doc nodeJSEnv/compile:doc \
@@ -332,7 +332,7 @@ def Tasks = [
332332
"tools-sbtplugin": '''
333333
setJavaVersion $java
334334
npm install &&
335-
sbt ++$scala ir/test logging/compile linkerInterface/compile \
335+
sbt ++$scala! ir/test logging/compile linkerInterface/compile \
336336
linker/compile jsEnvs/test nodeJSEnv/test testAdapter/test \
337337
sbtPlugin/package \
338338
ir/mimaReportBinaryIssues logging/mimaReportBinaryIssues \
@@ -341,7 +341,7 @@ def Tasks = [
341341
nodeJSEnv/mimaReportBinaryIssues \
342342
testAdapter/mimaReportBinaryIssues \
343343
sbtPlugin/mimaReportBinaryIssues &&
344-
sbt ++$scala library/scalastyle javalanglib/scalastyle javalib/scalastyle \
344+
sbt ++$scala! library/scalastyle javalanglib/scalastyle javalib/scalastyle \
345345
ir/scalastyle compiler/scalastyle \
346346
compiler/test:scalastyle \
347347
logging/scalastyle logging/test:scalastyle \
@@ -359,7 +359,7 @@ def Tasks = [
359359
jUnitPlugin/scalastyle jUnitRuntime/scalastyle \
360360
jUnitTestOutputsJVM/scalastyle jUnitTestOutputsJVM/test:scalastyle \
361361
jUnitTestOutputsJS/scalastyle jUnitTestOutputsJS/test:scalastyle &&
362-
sbt ++$scala ir/compile:doc logging/compile:doc \
362+
sbt ++$scala! ir/compile:doc logging/compile:doc \
363363
linkerInterface/compile:doc \
364364
linker/compile:doc jsEnvs/compile:doc \
365365
jsEnvsTestKit/compile:doc nodeJSEnv/compile:doc \
@@ -381,8 +381,7 @@ def Tasks = [
381381
sbt ++2.11.12 compiler/publishLocal library/publishLocal \
382382
testInterface/publishLocal testBridge/publishLocal \
383383
jUnitPlugin/publishLocal jUnitRuntime/publishLocal &&
384-
sbt ++$toolsscala \
385-
ir/publishLocal logging/publishLocal \
384+
sbt ir/publishLocal logging/publishLocal \
386385
linkerInterface/publishLocal \
387386
linker/publishLocal jsEnvs/publishLocal \
388387
nodeJSEnv/publishLocal testAdapter/publishLocal \
@@ -402,19 +401,19 @@ def Tasks = [
402401
"partest-noopt": '''
403402
setJavaVersion $java
404403
npm install &&
405-
sbt ++$scala package "partestSuite/testOnly -- --showDiff"
404+
sbt ++$scala! package "partestSuite/testOnly -- --showDiff"
406405
''',
407406

408407
"partest-fastopt": '''
409408
setJavaVersion $java
410409
npm install &&
411-
sbt ++$scala package "partestSuite/testOnly -- --fastOpt --showDiff"
410+
sbt ++$scala! package "partestSuite/testOnly -- --fastOpt --showDiff"
412411
''',
413412

414413
"partest-fullopt": '''
415414
setJavaVersion $java
416415
npm install &&
417-
sbt ++$scala package "partestSuite/testOnly -- --fullOpt --showDiff"
416+
sbt ++$scala! package "partestSuite/testOnly -- --fullOpt --showDiff"
418417
'''
419418
]
420419

@@ -455,7 +454,7 @@ allJavaVersions.each { javaVersion ->
455454
quickMatrix.add([task: "tools", scala: "2.11.12", java: javaVersion])
456455
}
457456
quickMatrix.add([task: "partestc", scala: "2.12.1", java: mainJavaVersion])
458-
quickMatrix.add([task: "sbtplugin-test", toolsscala: "2.12.8", java: mainJavaVersion])
457+
quickMatrix.add([task: "sbtplugin-test", java: mainJavaVersion])
459458

460459
// The 'full' matrix
461460
def fullMatrix = quickMatrix.clone()

project/Build.scala

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ object MyScalaJSPlugin extends AutoPlugin {
104104
jsEnv := new NodeJSEnv(
105105
NodeJSEnv.Config().withSourceMap(wantSourceMaps.value)),
106106

107+
scalaJSLinkerImpl := {
108+
val v = (scalaVersion in Build.linker).value
109+
assert(v.startsWith("2.12."), "The linker is configured with a Scala " +
110+
s"version ($v) that is not compatible with this build (2.12.x). ")
111+
LinkerImpl.default(Attributed.data((fullClasspath in (Build.linker, Runtime)).value))
112+
},
113+
107114
// Link source maps to GitHub sources
108115
addScalaJSCompilerOption(Def.setting {
109116
"mapSourceURI:" +
@@ -412,6 +419,10 @@ object Build {
412419
scalacOptions += "-Xfatal-warnings"
413420
)
414421

422+
val freezeVersionForToolsSettings = Def.settings(
423+
crossScalaVersions := Seq("2.12.8")
424+
)
425+
415426
private def publishToBintraySettings = Def.settings(
416427
publishTo := {
417428
val proj = bintrayProjectName.value
@@ -554,6 +565,7 @@ object Build {
554565

555566
lazy val irProject: Project = Project(id = "ir", base = file("ir")).settings(
556567
commonIrProjectSettings,
568+
freezeVersionForToolsSettings,
557569
libraryDependencies +=
558570
"com.novocode" % "junit-interface" % "0.9" % "test"
559571
)
@@ -638,7 +650,8 @@ object Build {
638650
)
639651

640652
lazy val logging: Project = (project in file("logging/jvm")).settings(
641-
commonLoggingSettings
653+
commonLoggingSettings,
654+
freezeVersionForToolsSettings,
642655
)
643656

644657
lazy val loggingJS: Project = (project in file("logging/js")).enablePlugins(
@@ -670,6 +683,7 @@ object Build {
670683

671684
lazy val linkerInterface: Project = (project in file("linker-interface/jvm")).settings(
672685
commonLinkerInterfaceSettings,
686+
freezeVersionForToolsSettings,
673687
).dependsOn(irProject, logging)
674688

675689
lazy val linkerInterfaceJS: Project = (project in file("linker-interface/js")).settings(
@@ -711,6 +725,7 @@ object Build {
711725

712726
lazy val linker: Project = (project in file("linker/jvm")).settings(
713727
commonLinkerSettings,
728+
freezeVersionForToolsSettings,
714729
libraryDependencies ++= Seq(
715730
"com.google.javascript" % "closure-compiler" % "v20190513",
716731
"com.novocode" % "junit-interface" % "0.9" % "test"
@@ -732,6 +747,7 @@ object Build {
732747

733748
lazy val jsEnvs: Project = (project in file("js-envs")).settings(
734749
commonSettings,
750+
freezeVersionForToolsSettings,
735751
publishSettings,
736752
fatalWarningsSettings,
737753
name := "Scala.js JS Envs",
@@ -742,6 +758,7 @@ object Build {
742758

743759
lazy val jsEnvsTestKit: Project = (project in file("js-envs-test-kit")).settings(
744760
commonSettings,
761+
freezeVersionForToolsSettings,
745762
publishSettings,
746763
fatalWarningsSettings,
747764
name := "Scala.js JS Envs Test Kit",
@@ -756,6 +773,7 @@ object Build {
756773

757774
lazy val nodeJSEnv: Project = (project in file("nodejs-env")).settings(
758775
commonSettings,
776+
freezeVersionForToolsSettings,
759777
publishSettings,
760778
fatalWarningsSettings,
761779
name := "Scala.js Node.js env",
@@ -770,6 +788,7 @@ object Build {
770788

771789
lazy val testAdapter = (project in file("test-adapter")).settings(
772790
commonSettings,
791+
freezeVersionForToolsSettings,
773792
publishSettings,
774793
fatalWarningsSettings,
775794
name := "Scala.js sbt test adapter",
@@ -815,7 +834,7 @@ object Build {
815834

816835
sbtJars.map(_.data -> docUrl).toMap
817836
}
818-
).dependsOn(linker, jsEnvs, nodeJSEnv, testAdapter)
837+
).dependsOn(linkerInterface, jsEnvs, nodeJSEnv, testAdapter)
819838

820839
lazy val delambdafySetting = {
821840
scalacOptions ++= (
@@ -1274,6 +1293,7 @@ object Build {
12741293

12751294
lazy val jUnitTestOutputsJVM = (project in file("junit-test/output-jvm")).settings(
12761295
commonJUnitTestOutputsSettings,
1296+
freezeVersionForToolsSettings,
12771297
name := "Tests for Scala.js JUnit output in JVM.",
12781298
libraryDependencies ++= Seq(
12791299
"org.scala-sbt" % "test-interface" % "1.0" % "test",
@@ -1303,6 +1323,7 @@ object Build {
13031323

13041324
lazy val jUnitAsyncJVM = (project in file("junit-async/jvm")).settings(
13051325
commonSettings,
1326+
freezeVersionForToolsSettings,
13061327
name := "Scala.js internal JUnit async JVM support",
13071328
publishArtifact in Compile := false
13081329
)

project/build.sbt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ addSbtPlugin("org.scalastyle" % "scalastyle-sbt-plugin" % "1.0.0")
66

77
addSbtPlugin("org.portable-scala" % "sbt-platform-deps" % "1.0.0")
88

9-
libraryDependencies += "com.google.javascript" % "closure-compiler" % "v20190513"
10-
119
libraryDependencies += "com.google.jimfs" % "jimfs" % "1.1"
1210

1311
libraryDependencies += "org.eclipse.jgit" % "org.eclipse.jgit.pgm" % "3.2.0.201312181205-r"
@@ -20,8 +18,6 @@ unmanagedSourceDirectories in Compile ++= {
2018
root / "logging/jvm/src/main/scala",
2119
root / "linker-interface/shared/src/main/scala",
2220
root / "linker-interface/jvm/src/main/scala",
23-
root / "linker/shared/src/main/scala",
24-
root / "linker/jvm/src/main/scala",
2521
root / "js-envs/src/main/scala",
2622
root / "nodejs-env/src/main/scala",
2723
root / "test-adapter/src/main/scala",

sbt-plugin-test/project/build.sbt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
addSbtPlugin("org.scala-js" % "sbt-scalajs" %
2-
org.scalajs.ir.ScalaJSVersions.current)
1+
val scalaJSVersion = org.scalajs.ir.ScalaJSVersions.current
2+
3+
addSbtPlugin("org.scala-js" % "sbt-scalajs" % scalaJSVersion)
4+
5+
libraryDependencies += "org.scala-js" %% "scalajs-linker" % scalaJSVersion
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Scala.js (https://www.scala-js.org/)
3+
*
4+
* Copyright EPFL.
5+
*
6+
* Licensed under Apache License 2.0
7+
* (https://www.apache.org/licenses/LICENSE-2.0).
8+
*
9+
* See the NOTICE file distributed with this work for
10+
* additional information regarding copyright ownership.
11+
*/
12+
13+
package org.scalajs.sbtplugin
14+
15+
/** A CacheBox is a support class to abuse an sbt setting as a cache.
16+
*
17+
* A CacheBox is needed, once the cached result needs to depend on a task,
18+
* since then it cannot simply be made a setting anymore.
19+
*/
20+
private[sbtplugin] final class CacheBox[T] {
21+
private[this] var value: T = _
22+
23+
def ensure(f: => T): T = synchronized {
24+
if (value == null) {
25+
value = f
26+
}
27+
value
28+
}
29+
30+
def foreach(f: T => Unit): Unit = synchronized {
31+
if (value != null) {
32+
f(value)
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)