5

In SBT, you can use the "~" mark to trigger actions whenever a source file changes. For example,

sbt> ~test

will run the unit tests whenever source changes.

Is there any good way to trigger actions whenever source changes or a local dependency changes? This would be useful when developing two projects simultaneously, where one depends on the other.

I know you can get this behavior by manually specifying a path to a file or the base project, but that's brittle, and SBT already knows where it's getting its local artifacts, so it's something I'd like to avoid.

Community
  • 1
  • 1
emchristiansen
  • 3,550
  • 3
  • 26
  • 40

1 Answers1

1

From the documentation for Triggered Execution, the watchSources task is where you can add additional files to be watched.

From another question, the managedClasspath task provides the part of the classpath that comes from managed dependencies.

Then, the following definition adds the managed test classpath to the files to watch for triggered execution:

watchSources <++=
  (managedClasspath in Test) map { cp => cp.files }

Define this in each project that you want to trigger on.

Community
  • 1
  • 1
Mark Harrah
  • 6,999
  • 1
  • 26
  • 31
  • I'm getting an apparent infinite loop with that setting, just using the command "~ compile". – emchristiansen Mar 23 '13 at 02:55
  • Do you mean there is a loop in sbt code or it keeps retriggering when nothing changes or something else? Some other information that might be relevant: what sbt version and what plugins are you using? – Mark Harrah Mar 23 '13 at 12:53
  • Here's an example: https://github.com/emchristiansen/WatchDependenciesInfiniteLoop – emchristiansen Mar 23 '13 at 19:03
  • Ok, thanks for the example. What happens is that `~` is polling based and will evaluate the `watchSources` task every second (or whatever is configured by `pollInterval`). In this case, evaluating `managedClasspath` requires evaluating `update`, but `update` doesn't do any work because it is cached. What isn't cached are the warnings, though. – Mark Harrah Mar 25 '13 at 11:50
  • The warnings aren't generated in 0.12.3, which is at RC2, so you could switch to that. – Mark Harrah Mar 25 '13 at 11:51
  • I switched to 0.12.3-RC2, and it's working beautifully, thanks! – emchristiansen Mar 25 '13 at 18:37
  • BTW, have you considered making this the default behavior? It seems more natural to me. – emchristiansen Mar 25 '13 at 18:39