From 06fcf78bc971461146f2921ed814fdd051683204 Mon Sep 17 00:00:00 2001 From: jvican Date: Tue, 2 May 2017 20:47:30 +0200 Subject: [PATCH] Add duplicated settings test This test shows how sbt is recomputing the settings completely (even if they have side effect) every time a `Project.extract` and settings manipulations are performed. This is worrying because it's: * Inefficient. * Surprising. * Incorrect -- settings may produce side effects. Unless I'm mistaken, this proves that the computation of settings is not performed once. --- .../unique-settings-computation/build.sbt | 9 +++++++ .../unique-settings-computation/pending | 3 +++ .../project/A.scala | 27 +++++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 sbt/src/sbt-test/project/unique-settings-computation/build.sbt create mode 100644 sbt/src/sbt-test/project/unique-settings-computation/pending create mode 100644 sbt/src/sbt-test/project/unique-settings-computation/project/A.scala diff --git a/sbt/src/sbt-test/project/unique-settings-computation/build.sbt b/sbt/src/sbt-test/project/unique-settings-computation/build.sbt new file mode 100644 index 000000000..01a12cdaf --- /dev/null +++ b/sbt/src/sbt-test/project/unique-settings-computation/build.sbt @@ -0,0 +1,9 @@ +lazy val root = project + +val checkComputedOnce = taskKey[Unit]("Check computed once") +checkComputedOnce := { + val buildValue = (foo in ThisBuild).value + assert(buildValue == "build 0", "Setting in ThisBuild was computed twice") + val globalValue = (foo in Global).value + assert(globalValue == "global 0", "Setting in Global was computed twice") +} diff --git a/sbt/src/sbt-test/project/unique-settings-computation/pending b/sbt/src/sbt-test/project/unique-settings-computation/pending new file mode 100644 index 000000000..e5cf3c46f --- /dev/null +++ b/sbt/src/sbt-test/project/unique-settings-computation/pending @@ -0,0 +1,3 @@ +> checkComputedOnce +> setUpScripted +> checkComputedOnce diff --git a/sbt/src/sbt-test/project/unique-settings-computation/project/A.scala b/sbt/src/sbt-test/project/unique-settings-computation/project/A.scala new file mode 100644 index 000000000..225665c3f --- /dev/null +++ b/sbt/src/sbt-test/project/unique-settings-computation/project/A.scala @@ -0,0 +1,27 @@ +import java.util.concurrent.atomic.AtomicInteger +import sbt._, Keys._ + +object A extends AutoPlugin { + object autoImport { + lazy val foo = settingKey[String]("Foo.") + } + import autoImport._ + override def trigger = allRequirements + + override def buildSettings: Seq[Setting[_]] = + (foo := s"build ${buildCount.getAndIncrement}") :: + Nil + + override def globalSettings: Seq[Setting[_]] = + (foo := s"global ${globalCount.getAndIncrement}") :: + (commands += setUpScripted) :: + Nil + + def setUpScripted = Command.command("setUpScripted") { (state0: State) => + Project.extract(state0).append(name := "foo", state0) + } + + // used to ensure the build-level and global settings are only added once + private[this] val buildCount = new AtomicInteger(0) + private[this] val globalCount = new AtomicInteger(0) +}