From 805fa002a7b457a9325729ffd2224c09e608bf72 Mon Sep 17 00:00:00 2001 From: Ethan Atkins Date: Sun, 17 Nov 2019 17:58:20 -0800 Subject: [PATCH 1/2] Only print unused setting warning if there are any --- .../main/scala/sbt/internal/LintUnused.scala | 41 ++++++++++--------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/main/src/main/scala/sbt/internal/LintUnused.scala b/main/src/main/scala/sbt/internal/LintUnused.scala index 668693381..625db4ea1 100644 --- a/main/src/main/scala/sbt/internal/LintUnused.scala +++ b/main/src/main/scala/sbt/internal/LintUnused.scala @@ -70,26 +70,29 @@ object LintUnused { import scala.collection.mutable.ListBuffer val buffer = ListBuffer.empty[String] - val size = result.size - if (size == 1) buffer.append("there's a key that's not used by any other settings/tasks:") - else buffer.append(s"there are $size keys that are not used by any other settings/tasks:") - buffer.append(" ") - result foreach { - case (_, str, positions) => - buffer.append(s"* $str") - positions foreach { - case pos: FilePosition => buffer.append(s" +- ${pos.path}:${pos.startLine}") - case _ => () - } + if (result.isEmpty) Vector.empty + else { + val size = result.size + if (size == 1) buffer.append("there's a key that's not used by any other settings/tasks:") + else buffer.append(s"there are $size keys that are not used by any other settings/tasks:") + buffer.append(" ") + result foreach { + case (_, str, positions) => + buffer.append(s"* $str") + positions foreach { + case pos: FilePosition => buffer.append(s" +- ${pos.path}:${pos.startLine}") + case _ => () + } + } + buffer.append(" ") + buffer.append( + "note: a setting might still be used by a command; to exclude a key from this `lintUnused` check" + ) + buffer.append( + "either append it to `Global / excludeLintKeys` or call .withRank(KeyRanks.Invisible) on the key" + ) + buffer.toVector } - buffer.append(" ") - buffer.append( - "note: a setting might still be used by a command; to exclude a key from this `lintUnused` check" - ) - buffer.append( - "either append it to `Global / excludeLintKeys` or call .withRank(KeyRanks.Invisible) on the key" - ) - buffer.toVector } def lintUnused( From 3bb847fc724279ada4b3b1ececc1558d477cdbaf Mon Sep 17 00:00:00 2001 From: Ethan Atkins Date: Sun, 17 Nov 2019 18:05:40 -0800 Subject: [PATCH 2/2] Allow lintUnusedKeys to be disabled The linting can take a while for large projects because `Def.compiled` scales in the number of settings. Even for small projects (i.e. scripted tests), it takes about 50 ms on my computer. This doesn't change the current behavior because the default value is true. --- main/src/main/scala/sbt/Keys.scala | 1 + main/src/main/scala/sbt/internal/LintUnused.scala | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/main/src/main/scala/sbt/Keys.scala b/main/src/main/scala/sbt/Keys.scala index ae23c5a0d..99aed78f8 100644 --- a/main/src/main/scala/sbt/Keys.scala +++ b/main/src/main/scala/sbt/Keys.scala @@ -495,6 +495,7 @@ object Keys { val lintUnused = inputKey[Unit]("Check for keys unused by other settings and tasks.") val excludeLintKeys = settingKey[Set[Def.KeyedInitialize[_]]]("Keys excluded from lintUnused task") val includeLintKeys = settingKey[Set[Def.KeyedInitialize[_]]]("Task keys that are included into lintUnused task") + val lintUnusedKeysOnLoad = settingKey[Boolean]("Toggles whether or not to check for unused keys during startup") val stateStreams = AttributeKey[Streams]("stateStreams", "Streams manager, which provides streams for different contexts. Setting this on State will override the default Streams implementation.") val resolvedScoped = Def.resolvedScoped diff --git a/main/src/main/scala/sbt/internal/LintUnused.scala b/main/src/main/scala/sbt/internal/LintUnused.scala index 625db4ea1..cfc04be30 100644 --- a/main/src/main/scala/sbt/internal/LintUnused.scala +++ b/main/src/main/scala/sbt/internal/LintUnused.scala @@ -39,6 +39,7 @@ object LintUnused { ivyConfiguration, ), Keys.lintUnused := lintUnusedTask.evaluated, + Keys.lintUnusedKeysOnLoad := true, ) // input task version of the lintUnused @@ -59,8 +60,10 @@ object LintUnused { val extracted = Project.extract(s) val includeKeys = extracted.get(includeLintKeys in Global) map { _.scopedKey.key.label } val excludeKeys = extracted.get(excludeLintKeys in Global) map { _.scopedKey.key.label } - val result = lintUnused(s, includeKeys, excludeKeys) - lintResultLines(result) foreach { log.warn(_) } + if (extracted.get(lintUnusedKeysOnLoad in Global)) { + val result = lintUnused(s, includeKeys, excludeKeys) + lintResultLines(result) foreach { log.warn(_) } + } s }