Why does the sourceDirectories parameter not affect sbt?

I am reading the SBT Getting Started tutorial. This page provides an example of setting sourceDirectories . To try this, I put the following in my build.sbt file for the basic hello world project.

 sourceDirectories in Compile := Seq(file("other")) 

(Adding with += or ++= does not work either.)

I have source files in <base-dir>/other and <base-dir>/src/main/scala . I ran sbt and typed compile . He ignored the setting:

 > compile [info] Compiling 1 Scala source to /Learning/Scala/proj1/target/scala-2.9.2/classes... [error] /Learning/Scala/proj1/src/main/scala/hello.scala:7: not found: value Foo [error] println(Foo.foo) 

The Foo object is defined in foo.scala in the other source directory.

What am I missing?

+4
source share
2 answers

tl; dr The answer from @schleichardt is valid - you need to add the following to the build.sbt file:

 unmanagedSourceDirectories in Compile += file("other") 

The explanation boils down to checking the dependencies of the compile task. Use inspect tree compile to find them.

 [proj1]> inspect tree compile [info] compile:compile = Task[sbt.inc.Analysis] [info] +-compile:compile::compileInputs = Task[sbt.Compiler$Inputs] [info] | +-compile:scalacOptions = Task[scala.collection.Seq[java.lang.String]] [info] | +-compile:sources = Task[scala.collection.Seq[java.io.File]] [info] | +-*/*:sourcePositionMappers = Task[scala.collection.Seq[scala.Function1[xsbti.Position, scala.Option[xsbti.Position]]]] [info] | +-*/*:maxErrors = 100 [info] | +-*:compilers = Task[sbt.Compiler$Compilers] [info] | +-compile:compile::streams = Task[sbt.std.TaskStreams[sbt.Init$ScopedKey[_ <: Any]]] [info] | | +-*/*:streamsManager = Task[sbt.std.Streams[sbt.Init$ScopedKey[_ <: Any]]] [info] | | [info] | +-compile:incCompileSetup = Task[sbt.Compiler$IncSetup] [info] | +-*/*:compileOrder = Mixed [info] | +-compile:dependencyClasspath = Task[scala.collection.Seq[sbt.Attributed[java.io.File]]] [info] | +-compile:classDirectory = target/scala-2.10/classes [info] | +-*/*:javacOptions = Task[scala.collection.Seq[java.lang.String]] [info] | [info] +-compile:compile::streams = Task[sbt.std.TaskStreams[sbt.Init$ScopedKey[_ <: Any]]] [info] +-*/*:streamsManager = Task[sbt.std.Streams[sbt.Init$ScopedKey[_ <: Any]]] [info] 

As you can see, the task depends on the compile:sources task. The task, in turn, depends on the compile:unmanagedSources task, which uses the compile:unmanagedSourceDirectories parameter.

 [proj1]> inspect tree compile:sources [info] compile:sources = Task[scala.collection.Seq[java.io.File]] [info] +-compile:unmanagedSources = Task[scala.collection.Seq[java.io.File]] [info] | +-compile:unmanagedSourceDirectories = List(/Users/jacek/sandbox/stackoverflow/proj1/src/main/scala, /Users/jacek/sandbox/stackoverflow/proj1/src/main/java, /Users/.. [info] | | +-compile:javaSource = src/main/java [info] | | | +-compile:sourceDirectory = src/main [info] | | | +-*:sourceDirectory = src [info] | | | | +-*:baseDirectory = /Users/jacek/sandbox/stackoverflow/proj1 [info] | | | | +-*:thisProject = Project(id: proj1, base: /Users/jacek/sandbox/stackoverflow/proj1, aggregate: List(), dependencies: List(), configurations: List(compile.. [info] | | | | [info] | | | +-compile:configuration = compile [info] | | | [info] | | +-compile:scalaSource = src/main/scala [info] | | | +-compile:sourceDirectory = src/main [info] | | | +-*:sourceDirectory = src [info] | | | | +-*:baseDirectory = /Users/jacek/sandbox/stackoverflow/proj1 [info] | | | | +-*:thisProject = Project(id: proj1, base: /Users/jacek/sandbox/stackoverflow/proj1, aggregate: List(), dependencies: List(), configurations: List(compile.. [info] | | | | [info] | | | +-compile:configuration = compile [info] | | | [info] | | +-*:baseDirectory = /Users/jacek/sandbox/stackoverflow/proj1 [info] | | +-*:thisProject = Project(id: proj1, base: /Users/jacek/sandbox/stackoverflow/proj1, aggregate: List(), dependencies: List(), configurations: List(compile, runt.. [info] | | [info] | +-*/*:excludeFilter = sbt.SimpleFileFilter@7ced8ea7 [info] | +-*:baseDirectory = /Users/jacek/sandbox/stackoverflow/proj1 [info] | +-*/*:unmanagedSources::includeFilter = sbt.SimpleFilter@1beb6bba [info] | +-*/*:sourcesInBase = true [info] | [info] +-compile:managedSources = Task[scala.collection.Seq[java.io.File]] [info] +-compile:sourceGenerators = List() [info] 

Links to compile:unmanagedSourceDirectories "Unmanaged source directories containing manually created sources."

 [proj1]> inspect compile:unmanagedSourceDirectories [info] Setting: scala.collection.Seq[java.io.File] = List(/Users/jacek/sandbox/stackoverflow/proj1/src/main/scala, /Users/jacek/sandbox/stackoverflow/proj1/src/main/java, /Users/jacek/sandbox/stackoverflow/proj1/other) [info] Description: [info] Unmanaged source directories, which contain manually created sources. [info] Provided by: [info] {file:/Users/jacek/sandbox/stackoverflow/proj1/}proj1/compile:unmanagedSourceDirectories [info] Defined at: [info] (sbt.Defaults) Defaults.scala:161 [info] /Users/jacek/sandbox/stackoverflow/proj1/build.sbt:1 [info] Dependencies: [info] compile:javaSource [info] compile:scalaSource [info] *:baseDirectory [info] Reverse dependencies: [info] compile:unmanagedSources [info] compile:sourceDirectories [info] Delegates: [info] compile:unmanagedSourceDirectories [info] *:unmanagedSourceDirectories [info] {.}/compile:unmanagedSourceDirectories [info] {.}/*:unmanagedSourceDirectories [info] */compile:unmanagedSourceDirectories [info] */*:unmanagedSourceDirectories [info] Related: [info] test:unmanagedSourceDirectories 

The type scala.collection.Seq[java.io.File] and the element is added to seq, you can use the += method.

Having said that the build.sbt file should be as follows:

 unmanagedSourceDirectories in Compile += (baseDirectory / "other").value 

You do not need to use the baseDirectory parameter, but it makes the main directory explicit.

+5
source

You can try the following:

 unmanagedSourceDirectories in Compile += file("other") 
+2
source

All Articles