sbt not picking up dependencies with a crossProject

38 views Asked by At

SBT is not picking up changes to my source code and recompiling appropriately.

I'm working on a multi-project build with two projects:

  • cli: a command-line interface for using the library. It's the root project.
  • lib: code that is used by the cli and that I want to publish separately. It's cross-platform, both JVM and JS. The cli is JVM only.

The build.sbt file for my minimal example is:

ThisBuild / scalaVersion := "3.3.1"

lazy val lib = crossProject(JSPlatform, JVMPlatform)
  .crossType(CrossType.Full)
  .in(file("lib"))
  .jsSettings(scalacOptions ++= Seq("-scalajs"))

lazy val cli = (project in file("."))
  .dependsOn(lib.jvm)
  .aggregate(lib.jvm, lib.js)

The "library", in lib/shared/src/main/scala/mylib/MyLib.scala is

package mylib

object MyLib {
  val life = 42 + 0
}

and my "command line interpreter" in src/main/scala/myapp/Main.scala is

package myapp

object Main {
  def main(args: Array[String]): Unit = {
    println(mylib.MyLib.life)
  }
}

Running this program from within SBT produces 42, of course.

However, I edit the definition of life to be life = 42 + 1, rerun the program, it still prints 42.

In the following test, I changed MyLib.scala at 16:02. I then waited until 16:05 and re-ran the program. Note that the MyLib class files in the lib/jvm/target directory were updated. The class files in ./target were not. I don't think I expect the class files for Main to be updated, but not updating the class files for MyLib smells fishy to me.

-rw-r--r--  1 bwbecker  staff   695  7 Mar 16:05 ./lib/jvm/target/scala-3.3.1/classes/mylib/MyLib$.class
-rw-r--r--  1 bwbecker  staff   280  7 Mar 16:05 ./lib/jvm/target/scala-3.3.1/classes/mylib/MyLib.class
-rw-r--r--  1 bwbecker  staff    52  7 Mar 16:02 ./lib/shared/src/main/scala/mylib/MyLib.scala
-rw-r--r--  1 bwbecker  staff   107  7 Mar 15:19 ./src/main/scala/myapp/Main.scala
-rw-r--r--  1 bwbecker  staff  1002  7 Mar 15:52 ./target/scala-3.3.1/classes/myapp/Main$.class
-rw-r--r--  1 bwbecker  staff   296  7 Mar 15:52 ./target/scala-3.3.1/classes/myapp/Main.class
-rw-r--r--  1 bwbecker  staff   695  7 Mar 15:59 ./target/scala-3.3.1/classes/mylib/MyLib$.class
-rw-r--r--  1 bwbecker  staff   280  7 Mar 15:59 ./target/scala-3.3.1/classes/mylib/MyLib.class

This was done with just sbt and vi. No fancy IDE running that might be interfering with things.

I figure I've made a mistake in build.sbt. I've read the SBT documentation and this excellent StackOverflow article on .dependsOn and .aggregate. But I don't see the problem -- except that I'm involving a crossProject.

Thanks for any assistance you can offer.


I first noted this issue with SBT v1.9.7. Updated to the most recent (1.9.9) with no change. MacOS 14.2.1. openjdk version "17.0.9" 2023-10-17

0

There are 0 answers