From 4df049ed0c9dadbf12b7ed007a5520c24bbeb437 Mon Sep 17 00:00:00 2001 From: Ethan Atkins Date: Mon, 16 Sep 2019 14:21:16 -0700 Subject: [PATCH] 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