From f7dd22880825e9a8215d1c1774d79858fef02dfc Mon Sep 17 00:00:00 2001 From: Ethan Atkins Date: Tue, 28 May 2019 16:16:23 -0700 Subject: [PATCH] Allow aliases to be used in continuous builds @japgolly reported in #4695 that aliased commands don't work in watch anymore. This was because we were extracting the task from the raw command rather than the aliased command. Since the alias wasn't a valid key, we weren't able to parse the scoped key. The fix is to find the aliased value and try that if we fail to parse the original command. Fixes #4695 --- .../main/scala/sbt/internal/Continuous.scala | 19 ++++++++++++++----- sbt/src/sbt-test/watch/alias/build.sbt | 6 ++++++ sbt/src/sbt-test/watch/alias/test | 3 +++ 3 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 sbt/src/sbt-test/watch/alias/build.sbt create mode 100644 sbt/src/sbt-test/watch/alias/test diff --git a/main/src/main/scala/sbt/internal/Continuous.scala b/main/src/main/scala/sbt/internal/Continuous.scala index 693142ab4..d9f9e92c4 100644 --- a/main/src/main/scala/sbt/internal/Continuous.scala +++ b/main/src/main/scala/sbt/internal/Continuous.scala @@ -380,12 +380,21 @@ private[sbt] object Continuous extends DeprecatedContinuous { // We have to add the <~ Parsers.any.* to ensure that we're able to extract the input key // from input tasks. val scopedKeyParser: Parser[Seq[ScopedKey[_]]] = Act.aggregatedKeyParser(state) <~ Parsers.any.* - Parser.parse(command, scopedKeyParser) match { - case Right(scopedKeys: Seq[ScopedKey[_]]) => scopedKeys - case Left(e) => - throw new IllegalStateException(s"Error attempting to extract scope from $command: $e.") - case _ => Nil: Seq[ScopedKey[_]] + @tailrec def impl(current: String): Seq[ScopedKey[_]] = { + Parser.parse(current, scopedKeyParser) match { + case Right(scopedKeys: Seq[ScopedKey[_]]) => scopedKeys + case Left(e) => + val aliases = BasicCommands.allAliases(state) + aliases.collectFirst { case (`command`, aliased) => aliased } match { + case Some(aliased) => impl(aliased) + case _ => + val msg = s"Error attempting to extract scope from $command: $e." + throw new IllegalStateException(msg) + } + case _ => Nil: Seq[ScopedKey[_]] + } } + impl(command) } private def getAllConfigs( diff --git a/sbt/src/sbt-test/watch/alias/build.sbt b/sbt/src/sbt-test/watch/alias/build.sbt new file mode 100644 index 000000000..0fab94fd9 --- /dev/null +++ b/sbt/src/sbt-test/watch/alias/build.sbt @@ -0,0 +1,6 @@ +val foo = taskKey[Unit]("foo") +foo := println("foo") + +foo / watchOnIteration := { _ => sbt.nio.Watch.CancelWatch } +addCommandAlias("bar", "foo") +addCommandAlias("baz", "foo") diff --git a/sbt/src/sbt-test/watch/alias/test b/sbt/src/sbt-test/watch/alias/test new file mode 100644 index 000000000..6781c97b8 --- /dev/null +++ b/sbt/src/sbt-test/watch/alias/test @@ -0,0 +1,3 @@ +> ~bar + +> ~baz \ No newline at end of file