How to resolve SBT problems by generating a stack trace

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 18.12, “Resolving Problems by Getting an SBT Stack Trace.”

Problem

In a Scala project, you’re trying to use SBT to compile, run, or package a project, and it’s failing, and you need to be able to see the stack trace to understand why it’s failing.

Solution

When an SBT command silently fails (typically with a “Nonzero exit code” message), but you can’t tell why, run your command from within the SBT shell, then use the last run command after the command that failed.

This pattern typically looks like this:

$ sbt run      // something fails here, but you can't tell what

$ sbt
> run          // failure happens again
> last run     // this shows the full stack trace

I’ve run into this on several projects where I was using JAR files and managing their dependencies myself, and in one specific case, I didn’t know I needed to include the Apache Commons Logging JAR file. This was causing the “Nonzero exit code” error message, but I couldn’t tell that until I issued the last run command from within the SBT shell. Once I ran that command, the problem was obvious from the stack trace.

Depending on the problem, another approach that can be helpful is to set the SBT logging level. See Recipe 18.13, “Setting the SBT Log Level,” for more information.