From 6aa6fcede3ef390a1e04f55dbb3551fcad2e988f Mon Sep 17 00:00:00 2001 From: Ethan Atkins Date: Mon, 16 Sep 2019 11:57:50 -0700 Subject: [PATCH 1/2] Monitor project build sources In sbt 1.3.0, we only monitor build sources in the root project directory and the root project meta build directory. This commit adds these inputs for each project. Fixes https://github.com/sbt/sbt/issues/5061. --- main/src/main/scala/sbt/Defaults.scala | 7 +++++-- sbt/src/sbt-test/nio/reload/build.sbt | 2 ++ sbt/src/sbt-test/nio/reload/changes/broken.sbt | 2 ++ sbt/src/sbt-test/nio/reload/changes/sub.sbt | 1 + sbt/src/sbt-test/nio/reload/changes/working.sbt | 2 ++ sbt/src/sbt-test/nio/reload/sub/build.sbt | 0 sbt/src/sbt-test/nio/reload/sub/src/main/scala/Test.scala | 3 +++ sbt/src/sbt-test/nio/reload/test | 6 ++++++ 8 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 sbt/src/sbt-test/nio/reload/changes/sub.sbt create mode 100644 sbt/src/sbt-test/nio/reload/sub/build.sbt create mode 100644 sbt/src/sbt-test/nio/reload/sub/src/main/scala/Test.scala diff --git a/main/src/main/scala/sbt/Defaults.scala b/main/src/main/scala/sbt/Defaults.scala index 03212aa94..072a7595d 100755 --- a/main/src/main/scala/sbt/Defaults.scala +++ b/main/src/main/scala/sbt/Defaults.scala @@ -371,13 +371,16 @@ object Defaults extends BuildCommon { }).value ) - def paths = Seq( + // Appended to JvmPlugin.projectSettings + def paths: Seq[Setting[_]] = Seq( baseDirectory := thisProject.value.base, target := baseDirectory.value / "target", historyPath := (historyPath or target(t => Option(t / ".history"))).value, sourceDirectory := baseDirectory.value / "src", sourceManaged := crossTarget.value / "src_managed", - resourceManaged := crossTarget.value / "resource_managed" + resourceManaged := crossTarget.value / "resource_managed", + // Adds subproject build.sbt files to the global list of build files to monitor + Scope.Global / checkBuildSources / fileInputs += baseDirectory.value.toGlob / "*.sbt" ) lazy val configPaths = sourceConfigPaths ++ resourceConfigPaths ++ outputConfigPaths diff --git a/sbt/src/sbt-test/nio/reload/build.sbt b/sbt/src/sbt-test/nio/reload/build.sbt index c143ba056..c5e320e75 100644 --- a/sbt/src/sbt-test/nio/reload/build.sbt +++ b/sbt/src/sbt-test/nio/reload/build.sbt @@ -11,3 +11,5 @@ exists := { } Global / onChangedBuildSource := ReloadOnSourceChanges + +val sub = project diff --git a/sbt/src/sbt-test/nio/reload/changes/broken.sbt b/sbt/src/sbt-test/nio/reload/changes/broken.sbt index 57e15cd57..5210c3db2 100644 --- a/sbt/src/sbt-test/nio/reload/changes/broken.sbt +++ b/sbt/src/sbt-test/nio/reload/changes/broken.sbt @@ -8,3 +8,5 @@ exists := { } Global / onChangedBuildSource := ReloadOnSourceChanges + +val sub = project diff --git a/sbt/src/sbt-test/nio/reload/changes/sub.sbt b/sbt/src/sbt-test/nio/reload/changes/sub.sbt new file mode 100644 index 000000000..f0ce2b852 --- /dev/null +++ b/sbt/src/sbt-test/nio/reload/changes/sub.sbt @@ -0,0 +1 @@ +libraryDependencies += "org.scala-sbt" % "sbt" % "1.3.0" \ No newline at end of file diff --git a/sbt/src/sbt-test/nio/reload/changes/working.sbt b/sbt/src/sbt-test/nio/reload/changes/working.sbt index c143ba056..c5e320e75 100644 --- a/sbt/src/sbt-test/nio/reload/changes/working.sbt +++ b/sbt/src/sbt-test/nio/reload/changes/working.sbt @@ -11,3 +11,5 @@ exists := { } Global / onChangedBuildSource := ReloadOnSourceChanges + +val sub = project diff --git a/sbt/src/sbt-test/nio/reload/sub/build.sbt b/sbt/src/sbt-test/nio/reload/sub/build.sbt new file mode 100644 index 000000000..e69de29bb diff --git a/sbt/src/sbt-test/nio/reload/sub/src/main/scala/Test.scala b/sbt/src/sbt-test/nio/reload/sub/src/main/scala/Test.scala new file mode 100644 index 000000000..405e020be --- /dev/null +++ b/sbt/src/sbt-test/nio/reload/sub/src/main/scala/Test.scala @@ -0,0 +1,3 @@ +object Test { + val x = sbt.Keys +} \ No newline at end of file diff --git a/sbt/src/sbt-test/nio/reload/test b/sbt/src/sbt-test/nio/reload/test index abfe64f5e..81c024396 100644 --- a/sbt/src/sbt-test/nio/reload/test +++ b/sbt/src/sbt-test/nio/reload/test @@ -17,3 +17,9 @@ $ copy-file changes/working.sbt build.sbt > foo foo; reload > exists foo + +-> compile + +$ copy-file changes/sub.sbt sub/build.sbt + +> compile From 4df049ed0c9dadbf12b7ed007a5520c24bbeb437 Mon Sep 17 00:00:00 2001 From: Ethan Atkins Date: Mon, 16 Sep 2019 14:21:16 -0700 Subject: [PATCH 2/2] Monitor meta build sources We want to recursively monitor the project meta build, but we also want to avoid listing directories that don't exist. To compromise, I rework the buildSourceFileInputs to add the nested project directories if they exist. Because the fileInputs are a setting, this means that adding a new project directory and *.sbt or *.scala will not immediately trigger a rebuild, but in most common cases, it will. I added a scripted test for this. --- .../sbt/internal/nio/CheckBuildSources.scala | 26 ++++++++++++------- sbt/src/sbt-test/nio/reload/.scalafmt.conf | 21 +++++++++++++++ .../nio/reload/changes/ScalafmtVersion.scala | 3 +++ .../sbt-test/nio/reload/project/plugins.sbt | 8 ++++++ .../project/project/ScalafmtVersion.scala | 3 +++ sbt/src/sbt-test/nio/reload/test | 6 +++++ 6 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 sbt/src/sbt-test/nio/reload/.scalafmt.conf create mode 100644 sbt/src/sbt-test/nio/reload/changes/ScalafmtVersion.scala create mode 100644 sbt/src/sbt-test/nio/reload/project/plugins.sbt create mode 100644 sbt/src/sbt-test/nio/reload/project/project/ScalafmtVersion.scala diff --git a/main/src/main/scala/sbt/internal/nio/CheckBuildSources.scala b/main/src/main/scala/sbt/internal/nio/CheckBuildSources.scala index 54a0fe752..782975b75 100644 --- a/main/src/main/scala/sbt/internal/nio/CheckBuildSources.scala +++ b/main/src/main/scala/sbt/internal/nio/CheckBuildSources.scala @@ -13,7 +13,10 @@ import sbt.SlashSyntax0._ import sbt.io.syntax._ import sbt.nio.FileChanges import sbt.nio.Keys._ -import sbt.nio.file.{ Glob, RecursiveGlob } +import sbt.nio.file.{ Glob, ** } +import sbt.nio.file.syntax._ + +import scala.annotation.tailrec private[sbt] object CheckBuildSources { private[sbt] def needReloadImpl: Def.Initialize[Task[StateTransform]] = Def.task { @@ -60,15 +63,20 @@ private[sbt] object CheckBuildSources { private[sbt] def buildSourceFileInputs: Def.Initialize[Seq[Glob]] = Def.setting { if (onChangedBuildSource.value != IgnoreSourceChanges) { val baseDir = (LocalRootProject / baseDirectory).value - val sourceFilter = "*.{sbt,scala,java}" val projectDir = baseDir / "project" - Seq( - Glob(baseDir, "*.sbt"), - Glob(projectDir, sourceFilter), - // We only want to recursively look in source because otherwise we have to search - // the project target directories which is expensive. - Glob(projectDir / "src", RecursiveGlob / sourceFilter), - ) + @tailrec + def projectGlobs(projectDir: File, globs: Seq[Glob]): Seq[Glob] = { + val glob = projectDir.toGlob + val base = Seq( + glob / "*.{sbt,scala,java}", + // We only want to recursively look in source because otherwise we have to search + // the project target directories which is expensive. + glob / "src" / ** / "*.{scala,java}" + ) + val nextLevel = projectDir / "project" + if (nextLevel.exists) projectGlobs(nextLevel, base) else base + } + projectGlobs(projectDir, baseDir.toGlob / "*.sbt" :: Nil) } else Nil } } diff --git a/sbt/src/sbt-test/nio/reload/.scalafmt.conf b/sbt/src/sbt-test/nio/reload/.scalafmt.conf new file mode 100644 index 000000000..8a81e8505 --- /dev/null +++ b/sbt/src/sbt-test/nio/reload/.scalafmt.conf @@ -0,0 +1,21 @@ +version = 2.0.0 +maxColumn = 100 +project.git = true +project.excludeFilters = [ "\\Wsbt-test\\W", "\\Winput_sources\\W", "\\Wcontraband-scala\\W" ] + +# http://docs.scala-lang.org/style/scaladoc.html recommends the JavaDoc style. +# scala/scala is written that way too https://github.com/scala/scala/blob/v2.12.2/src/library/scala/Predef.scala +docstrings = JavaDoc + +# This also seems more idiomatic to include whitespace in import x.{ yyy } +spaces.inImportCurlyBraces = true + +# This is more idiomatic Scala. +# http://docs.scala-lang.org/style/indentation.html#methods-with-numerous-arguments +align.openParenCallSite = false +align.openParenDefnSite = false + +# For better code clarity +danglingParentheses = true + +trailingCommas = preserve diff --git a/sbt/src/sbt-test/nio/reload/changes/ScalafmtVersion.scala b/sbt/src/sbt-test/nio/reload/changes/ScalafmtVersion.scala new file mode 100644 index 000000000..42ad05178 --- /dev/null +++ b/sbt/src/sbt-test/nio/reload/changes/ScalafmtVersion.scala @@ -0,0 +1,3 @@ +object ScalafmtVersion { + val value = "2.0.4" +} \ No newline at end of file diff --git a/sbt/src/sbt-test/nio/reload/project/plugins.sbt b/sbt/src/sbt-test/nio/reload/project/plugins.sbt new file mode 100644 index 000000000..99c853f12 --- /dev/null +++ b/sbt/src/sbt-test/nio/reload/project/plugins.sbt @@ -0,0 +1,8 @@ +libraryDependencies ++= { + if (ScalafmtVersion.value == "2.0.4") { + val sbtV = (sbtBinaryVersion in pluginCrossBuild).value + val scalaV = (scalaBinaryVersion in update).value + val dep = "org.scalameta" % "sbt-scalafmt" % ScalafmtVersion.value + sbt.Defaults.sbtPluginExtra(dep, sbtV, scalaV) :: Nil + } else Nil +} diff --git a/sbt/src/sbt-test/nio/reload/project/project/ScalafmtVersion.scala b/sbt/src/sbt-test/nio/reload/project/project/ScalafmtVersion.scala new file mode 100644 index 000000000..d21e33494 --- /dev/null +++ b/sbt/src/sbt-test/nio/reload/project/project/ScalafmtVersion.scala @@ -0,0 +1,3 @@ +object ScalafmtVersion { + val value = "2.0.3" +} \ No newline at end of file diff --git a/sbt/src/sbt-test/nio/reload/test b/sbt/src/sbt-test/nio/reload/test index 81c024396..26ef1a9ff 100644 --- a/sbt/src/sbt-test/nio/reload/test +++ b/sbt/src/sbt-test/nio/reload/test @@ -23,3 +23,9 @@ $ copy-file changes/working.sbt build.sbt $ copy-file changes/sub.sbt sub/build.sbt > compile + +-> scalafmt + +$ copy-file changes/ScalafmtVersion.scala project/project/ScalafmtVersion.scala + +> scalafmt