Skip to content

Avoid creating ScalaInstance when not used #6556

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 4 commits into from
May 28, 2019
Merged
Changes from 1 commit
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
69 changes: 35 additions & 34 deletions sbt-dotty/src/dotty/tools/sbtplugin/DottyPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import java.util.Optional
import scala.util.Properties.isJavaAtLeast

object DottyPlugin extends AutoPlugin {
val dottyScalaInstance = taskKey[ScalaInstance]("ScalaInstance for Dotty")
object autoImport {
val isDotty = settingKey[Boolean]("Is this project compiled with Dotty?")

Expand Down Expand Up @@ -142,6 +143,8 @@ object DottyPlugin extends AutoPlugin {
}
)

// https://github.com/sbt/sbt/issues/3110
val Def = sbt.Def
override def projectSettings: Seq[Setting[_]] = {
Seq(
isDotty := scalaVersion.value.startsWith("0."),
Expand Down Expand Up @@ -266,41 +269,39 @@ object DottyPlugin extends AutoPlugin {
},
// ... instead, we'll fetch the compiler and its dependencies ourselves.
scalaInstance := Def.taskDyn {
if (isDotty.value) Def.task {
val updateReport =
fetchArtifactsOf(
dependencyResolution.value,
scalaModuleInfo.value,
updateConfiguration.value,
(unresolvedWarningConfiguration in update).value,
streams.value.log,
scalaOrganization.value %% "dotty-doc" % scalaVersion.value)
val scalaLibraryJar = getJar(updateReport,
"org.scala-lang", "scala-library", revision = AllPassFilter)
val dottyLibraryJar = getJar(updateReport,
scalaOrganization.value, s"dotty-library_${scalaBinaryVersion.value}", scalaVersion.value)
val compilerJar = getJar(updateReport,
scalaOrganization.value, s"dotty-compiler_${scalaBinaryVersion.value}", scalaVersion.value)
val allJars =
getJars(updateReport, AllPassFilter, AllPassFilter, AllPassFilter)

makeScalaInstance(
state.value,
scalaVersion.value,
scalaLibraryJar,
dottyLibraryJar,
compilerJar,
allJars
)
}
else Def.task {
// This should really be `old` with `val old = scalaInstance.value`
// above, except that this would force the original definition of the
// `scalaInstance` task to be computed when `isDotty` is true, which
// would fail because `managedScalaInstance` is false.
Defaults.scalaInstanceTask.value
}
val isD = isDotty.value
val si = scalaInstance.taskValue
val siTaskInitialize = Def.valueStrict { si }
if (isD) dottyScalaInstance
Copy link
Member

Choose a reason for hiding this comment

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

Can we inline some of this stuff ? e.g.

if (isDotty.value) Def.task { ... }
else Def.valueStrict { ... }
```

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 split things out so each step is easier to understand (for me). You can inline as much as you like.

Copy link
Member Author

Choose a reason for hiding this comment

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

else siTaskInitialize
}.value,
dottyScalaInstance := {
val updateReport =
fetchArtifactsOf(
dependencyResolution.value,
scalaModuleInfo.value,
updateConfiguration.value,
(unresolvedWarningConfiguration in update).value,
streams.value.log,
scalaOrganization.value %% "dotty-doc" % scalaVersion.value)
val scalaLibraryJar = getJar(updateReport,
"org.scala-lang", "scala-library", revision = AllPassFilter)
val dottyLibraryJar = getJar(updateReport,
scalaOrganization.value, s"dotty-library_${scalaBinaryVersion.value}", scalaVersion.value)
val compilerJar = getJar(updateReport,
scalaOrganization.value, s"dotty-compiler_${scalaBinaryVersion.value}", scalaVersion.value)
val allJars =
getJars(updateReport, AllPassFilter, AllPassFilter, AllPassFilter)

makeScalaInstance(
state.value,
scalaVersion.value,
scalaLibraryJar,
dottyLibraryJar,
compilerJar,
allJars
)
},

// Because managedScalaInstance is false, sbt won't add the standard library to our dependencies for us
libraryDependencies ++= {
Expand Down