Sbt: give better error messages on "No build target for"

An error that I hit numerous times throughout the year using Scala is this message in the Metals output section WARN no build target for: followed by a notification that it is defaulting to a previous version of scala Using presentation compiler with project's scala-library version: 3.6.4.

This message is irritating as it does not give any hint to it’s cause. Sometimes it is a random misconfigured build.sbt option, sometimes it’s due to my simplified file structure src/main.scala which diverges from the maven style src/main/scala/main.scala and me not putting the right project.in(file(".")) and scalaJSUseMainModuleInitializer := true, options etc. which I eventually fix through painful trial and error.

Today, depsite my project working fine for many months, I now am seeing this error again (and so I am unable to upgrade to 3.7 now). It is also accompanied with strange errors about Scala test cases not being supported and scalajs issues.

Scala test classes not supported by server
org.eclipse.lsp4j.jsonrpc.ResponseErrorException: java.lang.reflect.InvocationTargetException

Caused by: org.scalajs.testing.common.RPCCore$ClosedException: org.scalajs.testing.adapter.JSEnvRPC$RunTerminatedException

Again, I have no way to know if the error is related to that or not… Did I somehow break my custom file structure setup? Was it always broken and I didn’t notice? Does ScalaJS not work with the latest Scala 3.7? These are all possibilities i have to consider because zero additional information is ever given with this error.

I also don’t know how to efficiently go about debugging this besides nuking the entire project and copy pasting my progress into new fresh templates.

Please add more clarification as to why sbt fails to find build targets.

And I would appreciate some advice on how to quickly solve this error again in the future - (besides “just don’t change SBT options”)

You can run the Metals doctor to see what targets it sees and if you click on a specific ones you will be able to see what directories or source files it treats as sources for that module.

In most cases that message might pop up if Metals hasn’t yet managed to connect to the build target or the build target wasn’t refreshed properly when reimporting.

The Scala JS error might be related, but it’s not that likely. It’s probably a separate issue altogether. Did you seen any issues reported when reimporting? Was anything weird popping up in .metals/metals.log?

Here is another example I just hit trying to setup a simple cross-project app.

I run sbt new scala/scala3.g8. Following the guides, I paste into build.sbt

// If you are using Scala.js 0.6.x, you need the following import:
//import sbtcrossproject.CrossPlugin.autoImport.{crossProject, CrossType}

val sharedSettings = Seq(scalaVersion := "2.13.14")

lazy val bar =
  // select supported platforms
  crossProject(JSPlatform, JVMPlatform, NativePlatform)
    .crossType(CrossType.Pure) // [Pure, Full, Dummy], default: CrossType.Full
    .settings(sharedSettings)
    .jsSettings(/* ... */) // defined in sbt-scalajs-crossproject
    .jvmSettings(/* ... */)
    // configure Scala-Native settings
    .nativeSettings(/* ... */) // defined in sbt-scala-native

// Optional in sbt 1.x (mandatory in sbt 0.13.x)
lazy val barJS     = bar.js
lazy val barJVM    = bar.jvm
lazy val barNative = bar.native

lazy val foo =
  crossProject(JSPlatform, JVMPlatform, NativePlatform)
    .settings(sharedSettings)
    .settings(
      // %%% now include Scala Native. It applies to all selected platforms
      libraryDependencies += "org.example" %%% "foo" % "1.2.3"
    )

// Optional in sbt 1.x (mandatory in sbt 0.13.x)
lazy val fooJS = foo.js
lazy val fooJVM = foo.jvm
lazy val fooNative = foo.native

then I make a project/plugins.sbt that contains

addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject"      % "1.3.2")
addSbtPlugin("org.portable-scala" % "sbt-scala-native-crossproject" % "1.3.2")
addSbtPlugin("org.scala-js"       % "sbt-scalajs"                   % "1.16.0")
addSbtPlugin("org.scala-native"   % "sbt-scala-native"              % "0.5.4")

I reimport build, and then I see in Metals output

Metals version: 1.5.3
Using coursier located at /opt/homebrew/bin/coursier
Searching for Java on PATH. Found java executable under /usr/bin/java that resolves to /usr/bin/java
Using Java Home: /opt/homebrew/Cellar/openjdk/23.0.1/libexec/openjdk.jdk/Contents/Home
INFO  logging to files /Users/john/proj/testproject/.metals/metals.log
INFO  Started: Metals version 1.5.3 in folders '/Users/john/proj/testproject' for client Visual Studio Code 1.100.2.
Initializing Scala Debugger
Release notes was not shown: not showing release notes since they've already been seen for your current version
INFO  Database: jdbc:h2:file:/Users/john/proj/testproject/.metals/metals (H2 2.3)
INFO  Schema history table "PUBLIC"."flyway_schema_history" does not exist yet
INFO  Successfully validated 6 migrations (execution time 00:00.010s)
INFO  Creating Schema History table "PUBLIC"."flyway_schema_history" ...
INFO  Current version of schema "PUBLIC": << Empty Schema >>
INFO  Migrating schema "PUBLIC" to version "1 - Create tables"
INFO  Migrating schema "PUBLIC" to version "2 - Server discovery"
INFO  Migrating schema "PUBLIC" to version "3 - Jar symbols"
INFO  Migrating schema "PUBLIC" to version "4 - Fingerprints"
INFO  Migrating schema "PUBLIC" to version "5 - Jar type hierarchy"
INFO  Migrating schema "PUBLIC" to version "6 - Delete indices"
INFO  Successfully applied 6 migrations to schema "PUBLIC", now at version v6 (execution time 00:00.014s)
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
WARN  no build target for: /Users/john/proj/testproject/build.sbt
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
WARN  no build target for: /Users/john/proj/testproject/build.sbt
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
WARN  no build target for: /Users/john/proj/testproject/project/plugins.sbt
INFO  no build target found for /Users/john/proj/testproject/project/plugins.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/project/plugins.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/project/plugins.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/project/plugins.sbt. Using presentation compiler with project's scala-library version: 3.3.6
WARN  no build target for: /Users/john/proj/testproject/project/plugins.sbt
INFO  no build target found for /Users/john/proj/testproject/project/plugins.sbt. Using presentation compiler with project's scala-library version: 3.3.6
WARN  no build target for: /Users/john/proj/testproject/build.sbt
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
WARN  no build target for: /Users/john/proj/testproject/build.sbt
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/build.sbt. Using presentation compiler with project's scala-library version: 3.3.6
WARN  no build target for: /Users/john/proj/testproject/project/plugins.sbt
INFO  no build target found for /Users/john/proj/testproject/project/plugins.sbt. Using presentation compiler with project's scala-library version: 3.3.6
INFO  no build target found for /Users/john/proj/testproject/project/plugins.sbt. Using presentation compiler with project's scala-library version: 3.3.6

What am I supposed to gleam from that error 45 times? It follows me everywhere I go in every project and I never know how to solve it.

Running metals doctor only shows

⚠️ No build targets were detected in this workspace so most functionality won't work.