sbt compile yields "object casbah is not a member of package com.mongodb" - mongodb

My directory structure:
-build.sbt
-src
--main
---scala
----MongoConnect.scala
-lib
My build.sbt:
name := "mongodb-experiments"
version := "0.1"
libraryDependencies ++= Seq(
"com.mongodb.casbah" %% "casbah" % "3.0.0-SNAPSHOT"
)
resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
My MongoConnect.scala program:
import com.mongodb.casbah.Imports._
object MongoConnect{
def main(args: Array[String]){
println("Hello Mongo")
}
}
Why does sbt compile result in
object casbah is not a member of package com.mongodb
?
sbt compile
[info] Set current project to mongodb-experiments (in build file:/Users/hrishikeshparanjape/git-public/mongodb-experiments/)
[info] Updating {file:/Users/hrishikeshparanjape/git-public/mongodb-experiments/}default-fc358e...
[info] Resolving org.scala-lang#scala-library;2.9.1 ...
[info] Resolving com.mongodb.casbah#casbah_2.9.1;3.0.0-SNAPSHOT ...
[info] Resolving com.mongodb.casbah#casbah-util_2.9.1;3.0.0-SNAPSHOT ...
[info] Resolving org.slf4j#slf4j-api;1.6.0 ...
[info] Resolving org.mongodb#mongo-java-driver;2.7.2 ...
[info] Resolving org.scalaj#scalaj-collection_2.9.1;1.2 ...
[info] Resolving org.scala-tools.time#time_2.8.0;0.2 ...
[info] Resolving joda-time#joda-time;1.6 ...
[info] Resolving com.mongodb.casbah#casbah-commons_2.9.1;3.0.0-SNAPSHOT ...
[info] Resolving com.mongodb.casbah#casbah-core_2.9.1;3.0.0-SNAPSHOT ...
[info] Resolving com.mongodb.casbah#casbah-query_2.9.1;3.0.0-SNAPSHOT ...
[info] Resolving com.mongodb.casbah#casbah-gridfs_2.9.1;3.0.0-SNAPSHOT ...
[info] Done updating.
[info] Compiling 1 Scala source to /Users/hrishikeshparanjape/git-public/mongodb-experiments/target/scala-2.9.1/classes...
[error] /Users/hrishikeshparanjape/git-public/mongodb-experiments/src/main/scala/MongoConnect.scala:1: object casbah is not a member of package com.mongodb
[error] import com.mongodb.casbah.Imports._
[error] ^
[error] one error found
[error] {file:/Users/hrishikeshparanjape/git-public/mongodb-experiments/}default-fc358e/compile:compile: Compilation failed
[error] Total time: 7 s, completed Jul 26, 2012 11:53:35 PM

Why do you use snapshot repository with and old versions of casbah?
libraryDependencies ++= Seq(
"org.mongodb" %% "casbah" % "2.4.1"
)
resolvers += "typesafe" at "http://repo.typesafe.com/typesafe/releases/"
%% sign in dependency will choose configured in sbt scala version
For 3.x version there is a milestone
libraryDependencies ++= Seq(
"org.mongodb" %% "casbah" % "3.0.0-M2"
)
And as I remember in 3.x import should be changed to:
import com.mongodb.casbah._

modify your build.sbt file as:
name := "mongodb-experiments"
version := "0.1"
libraryDependencies ++= Seq(
"com.mongodb.casbah" % "casbah_2.9.0" % "2.2.0-SNAPSHOT"
)
resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
For some reason, 3.0.0 does not work.

Related

ToolBox Import Error

I'm getting the following error when compiling the following toy class:
package com.example
import scala.tools.reflect.ToolBox
import scala.reflect.runtime.{currentMirror => m}
object Hello {
def main(args: Array[String]): Unit = {
println("Hello, world!")
}
}
[info] Loading project definition from /Users/me/Temp/Bar/project
[info] Set current project to Bar (in build file:/Users/me/Temp/Bar/)
[info] Compiling 1 Scala source to /Users/me/Temp/Bar/target/scala-2.11/classes...
[error] /Users/me/Temp/Bar/src/main/scala/com/example/Hello.scala:3: object tools is not a member of package scala
[error] import scala.tools.reflect.ToolBox
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
This is my build.sbt file:
name := """Bar"""
version := "1.0"
scalaVersion := "2.11.8"
// Change this to another test framework if you prefer
libraryDependencies += "org.scalatest" %% "scalatest" % "2.2.4" % "test"
libraryDependencies += "org.scala-lang" % "scala-reflect" % "2.11.8"
// Uncomment to use Akka
//libraryDependencies += "com.typesafe.akka" %% "akka-actor" % "2.3.11"
The following dependency fixed the issue:
libraryDependencies += "org.scala-lang" % "scala-compiler" % "2.11.8"
Is this the best solution?
The ToolBox class is part of the compiler, not the public reflection API.

SBT setup for sbt-scalabuff

Can someone give me a minimal working sbt setup for sbt-scalabuff? The information that's out there seems incomplete. I'm currently trying to use addSbtPlugin("com.github.sbt" % "sbt-scalabuff" % "0.2"), but I get sbt.ResolveException: unresolved dependency: net.sandrogrzicic#scalabuff-runtime_2.9.2;1.3.6: not found. I guess I'm missing a repository.
Why is it using 2.9.2, though? I have scalaVersion := 2.10.3.
build.sbt
organization := "com.confabulous"
name := "protobuf"
version := "0.0.1-SNAPSHOT"
scalaVersion := "2.10.4"
scalacOptions += "-deprecation"
resolvers ++= Seq(
"sonatype releases" at "https://oss.sonatype.org/content/repositories/releas
"sonatype snapshots" at "https://oss.sonatype.org/content/repositories/snapsh
"typesafe repo" at "http://repo.typesafe.com/typesafe/releases/"
)
libraryDependencies += "net.sandrogrzicic" %% "scalabuff-runtime" % "1.3.6"
plugins/plugins.sbt
addSbtPlugin("com.github.sbt" % "sbt-scalabuff" % "0.2")
project/Build.scala
import sbt._
import scalabuff.ScalaBuffPlugin._
object build extends Build {
lazy val root = Project("main", file("."), settings = Defaults.defaultSetting
}
Output
$ sbt compile
Loading /usr/share/sbt/bin/sbt-launch-lib.bash
[info] Loading project definition from /home/dan/projects/confabulous/protobuf/project
[info] Updating {file:/home/dan/projects/confabulous/protobuf/project/}default-6a3ff1...
[info] Resolving org.scala-sbt#precompiled-2_10_1;0.12.4 ...
[info] Done updating.
[info] Set current project to protobuf (in build file:/home/dan/projects/confabulous/protobuf/)
[info] Compiling 1 Scala source to /home/dan/projects/confabulous/protobuf/target/scala-2.10/classes...
[error] /home/dan/projects/confabulous/protobuf/target/scala-2.10/src_managed/scala/com/confabulous/protobuf/ConfabulousProtobuf.scala:11: not found: value net
[error] with net.sandrogrzicic.scalabuff.Message[Pair] {
[error] ^
[error] /home/dan/projects/confabulous/protobuf/target/scala-2.10/src_managed/scala/com/confabulous/protobuf/ConfabulousProtobuf.scala:76: not found: value net
[error] with net.sandrogrzicic.scalabuff.Message[Notice] {
[error] ^
Add https://dl.bintray.com/actor/maven to your SBT resolver
resolvers ++= Seq("sonatype releases" at "https://oss.sonatype.org/content/repositories/releas
"sonatype snapshots" at "https://oss.sonatype.org/content/repositories/snapsh
"typesafe repo" at "http://repo.typesafe.com/typesafe/releases/"
"bintrayRepo" at "https://dl.bintray.com/actor/maven"
)

Test in Eclipse works but sbt throws MissingRequirementError: object scala.runtime in compiler mirror not found

I am messing around with parsing and scala.tools.nsc.interactive.Global in Scala and I ran into a problem while executing tests under sbt. The tests run fine from Eclipse both with JUnitRunner and the ScalaTest plugin. After long time spent on Google I can't figure out how to fix this.
When I execute sbt test the following error is thrown:
Exception encountered when attempting to run a suite with class name: compileutils.CompileTest *** ABORTED ***
[info] java.lang.ExceptionInInitializerError:
[info] at compileutils.CompileTest$$anonfun$3.apply$mcV$sp(CompileTest.scala:18)
[info] at compileutils.CompileTest$$anonfun$3.apply(CompileTest.scala:16)
[info] at compileutils.CompileTest$$anonfun$3.apply(CompileTest.scala:16)
[info] at org.scalatest.Transformer$$anonfun$apply$1.apply(Transformer.scala:22)
[info] at org.scalatest.Transformer$$anonfun$apply$1.apply(Transformer.scala:22)
[info] at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
[info] at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
[info] at org.scalatest.Transformer.apply(Transformer.scala:22)
[info] at org.scalatest.Transformer.apply(Transformer.scala:20)
[info] at org.scalatest.FunSuiteLike$$anon$1.apply(FunSuiteLike.scala:158)
[info] ...
[info] Cause: scala.reflect.internal.MissingRequirementError: object scala.runtime in compiler mirror not found.
[info] at scala.reflect.internal.MissingRequirementError$.signal(MissingRequirementError.scala:16)
[info] at scala.reflect.internal.MissingRequirementError$.notFound(MissingRequirementError.scala:17)
[info] at scala.reflect.internal.Mirrors$RootsBase.getModuleOrClass(Mirrors.scala:48)
[info] at scala.reflect.internal.Mirrors$RootsBase.getModuleOrClass(Mirrors.scala:40)
[info] at scala.reflect.internal.Mirrors$RootsBase.getModuleOrClass(Mirrors.scala:61)
[info] at scala.reflect.internal.Mirrors$RootsBase.getPackage(Mirrors.scala:172)
[info] at scala.reflect.internal.Mirrors$RootsBase.getRequiredPackage(Mirrors.scala:175)
[info] at scala.reflect.internal.Definitions$DefinitionsClass.RuntimePackage$lzycompute(Definitions.scala:183)
[info] at scala.reflect.internal.Definitions$DefinitionsClass.RuntimePackage(Definitions.scala:183)
[info] at scala.reflect.internal.Definitions$DefinitionsClass.RuntimePackageClass$lzycompute(Definitions.scala:184)
[info] ...
The class under test
package compileutils
import scala.tools.nsc.Settings
import scala.tools.nsc.interactive.Global
import scala.tools.nsc.reporters.ConsoleReporter
import scala.tools.nsc.interactive.Response
import scala.io.Source
import scala.reflect.internal.util.SourceFile
import scala.reflect.internal.util.BatchSourceFile
import scala.reflect.io.AbstractFile
import java.io.File
object Compile {
val settings = new Settings
val reporter = new ConsoleReporter(settings)
val global = new Global(settings, reporter, "Study compile")
def parse(source: String): Compile.this.global.Tree = {
val sourceFile = new BatchSourceFile(".", source)
global.askReload(List(sourceFile), new Response[Unit])
global.parseTree(sourceFile)
}
def loadTypes(source: String): Either[Compile.this.global.Tree, Throwable] = {
val sourceFile = new BatchSourceFile(".", source)
val tResponse = new Response[global.Tree]
global.askReload(List(sourceFile), new Response[Unit])
global.askLoadedTyped(sourceFile, tResponse)
tResponse.get
}
}
The test
package compileutils
import org.scalatest.BeforeAndAfter
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.FunSuite
import org.scalatest.Matchers._
#RunWith(classOf[JUnitRunner])
class CompileTest extends FunSuite with BeforeAndAfter {
val testSource = "class FromString {val s = \"dsasdsad \"}"
before {}
after {}
test("parse") {
//when
val tree = Compile.parse(testSource)
//then
tree should not be null
}
test("typer") {
//when
val typ = Compile.loadTypes(testSource)
//then
typ should be('left)
}
}
build.sbt
name := "Compiler study"
version := "0.1"
val scalaBuildVersion = "2.10.3"
scalaVersion := scalaBuildVersion
libraryDependencies += "org.scala-lang" % "scala-compiler" % scalaBuildVersion
libraryDependencies += "org.scala-lang" % "scala-library" % scalaBuildVersion
libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaBuildVersion
libraryDependencies += "org.scalatest" %% "scalatest" % "2.1.0" % "test"
libraryDependencies += "junit" % "junit" % "4.11" % "test"
Environment:
sbt launcher version 0.13.0
Scala compiler version 2.10.3 -- Copyright 2002-2013, LAMP/EPFL
javac 1.6.0_45
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=13.10
DISTRIB_CODENAME=saucy
DISTRIB_DESCRIPTION="Ubuntu 13.10"
It looks like the scala jar isn't in your classpath when running sbt - make sure to add scala-library.jar to your classpath before you run sbt.
Based on one of your comments, it looks like you're running on windows. you might be also running into runtime jar access errors there if the classpath contains strange characters or spaces, or permission errors (e.g., if eclipse is running under an admin account, while sbt isn't).
Try reordering your dependency list, to put scala-library ahead of scala-compiler. if that doesn't work, try the troubleshooting advice here.
The scala-library.jar was not missing from the sbt's classpath but from the classpath of Global. Had to set it in code.
After modifying the source to
val settings = new Settings
val scalaLibraryPath = "/home/csajka/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.10.3.jar"
settings.bootclasspath.append(scalaLibraryPath)
settings.classpath.append(scalaLibraryPath)
val reporter = new ConsoleReporter(settings)
val global = new Global(settings, reporter, "Study compile")
the problem disappeared.
Thanks for the tip #blueberryfields!

Play sub-projects do not compile

Here is the layout of my multi-project Play 2.2 application - I'm still trying to convert to build.sbt:
myApp
+ app
+ build.sbt
+ conf
| + routes
+ project
| + build.properties
| + Build.scala
| + plugin.sbt
+ modules
+ myModule
+ app
+ build.sbt
+ conf
+ routes
myApp/build.sbt:
name := "myApp"
version := "1.0-SNAPSHOT"
organization := "com.mydomain"
lazy val myModule = project.in(file("modules/myModule"))
lazy val main = project.in(file(".")).dependsOn(myModule).aggregate(myModule)
play.Project.playScalaSettings
resolvers ++= Seq(
Resolvers.typesafe,
Resolvers.sonatype
)
myApp/projects/Build.scala:
import sbt._
object Resolvers {
val sonatype = Resolver.sonatypeRepo("snapshots")
val typesafe = "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
}
myApp/modules/myModule/build.sbt:
name := "myModule"
libraryDependencies ++= Seq(
"com.typesafe.play" %% "play" % "2.2.1" % "provided",
"org.reactivemongo" %% "reactivemongo" % "0.10.0-SNAPSHOT",
"org.reactivemongo" %% "play2-reactivemongo" % "0.10.0-SNAPSHOT"
)
resolvers ++= Seq(
Resolvers.typesafe,
Resolvers.sonatype
)
I'm facing with two problems when trying to compile the project above:
1) Even if only the sub-project has dependencies (the main project is just a container for many sub-projects), I have to specify the resolvers also in the main build file myApp/build.sbt; if I don't, the project doesn't compile. This is not a blocking problem.. but I'd like to understand why.
2) Then, as soon as I try to compile the project, I always get the following error:
[error] /home/j3d/Projects/myApp/conf/routes:9: not found: value myModule
[error] -> /myModule myModule.Routes
[error] /home/j3d/Projects/myApp/conf/routes: not found: value myModule
[error] /home/j3d/Projects/myApp/conf/routes:12: not found: value myModule
[error] GET /assets/*file controllers.Assets.at(path="/public", file)
[error] /home/j3d/Projects/myApp/conf/routes:9: not found: value handler
[error] -> /myModule myModule.Routes
[error] four errors found
[error] (main/compile:compile) Compilation failed
[error] Total time: 12 s, completed Dec 1, 2013 6:34:55 PM
Here is myApp/conf/routes...
GET / controllers.Application.index
-> /myModule myModule.Routes
GET /assets/*file controllers.Assets.at(path="/public", file)
... and finally here is myApp/modules/myModule/conf/myModule.routes:
GET /myModule/greetings controllers.myModule.Greetings.hello
Am I missing something?
Figured out how to make it work and here below is my solution. First of all I've defined default settings and resolvers for all projects [myApp/project/Build.scala]:
import sbt._
import Keys._
object ApplicationBuild extends Build {
val defaultResolvers = Seq(
Resolver.sonatypeRepo("snapshots"),
"Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
)
val defaultSettings = Defaults.defaultSettings ++ Seq(
scalacOptions += "-language:reflectiveCalls",
resolvers ++= defaultResolvers
)
}
Then I've just imported ApplicationBuild.defaultSettings into every build.sbt. Here is the main project build file [myApp/build.sbt]...
name := "myApp"
version := "1.0-SNAPSHOT"
lazy val auth = project.in(file("modules/myModule"))
lazy val main = project.in(file(".")).dependsOn(myModule).aggregate(myModule)
ApplicationBuild.defaultSettings
playScalaSettings
... and here the sub-project build file [myApp/modules/myModule/build.sbt]:
name := "myModule"
ApplicationBuild.defaultSettings
playScalaSettings
libraryDependencies ++= Seq(
"org.reactivemongo" %% "play2-reactivemongo" % "0.10.0-SNAPSHOT",
"org.reactivemongo" %% "reactivemongo" % "0.10.0-SNAPSHOT"
)
It just works and hope it helps ;-)

Resolving the dependency of Scala Macros and Compiler Framework in SBT

I am trying to write a framework to make writing Scala compiler plugins easier, what I am doing is writing a framework on top of the Scala quasiquotes. So my project depends on macros from macro-paradise and both scala-compiler and scala-reflect libraries.
I wrote an SBT build script by following the instructions mentioned here: https://github.com/scalamacros/sbt-example-paradise/blob/master/project/Build.scala
And used scalaVersion 2.11.0-SNAPSHOT, 2.10.3-SNAPSHOT, 2.10.3-RC1, 2.10.2 to compile my project, but neither of them worked. Here is my sbt build script:
import sbt._
import Keys._
object LombrelloBuildSettings {
val sversion = "2.10.3-SNAPSHOT"
val buildSettings = Defaults.defaultSettings ++ Seq(
name := "lombrello",
organization := "ch.usi.inf.l3",
version := "0.1-SNAPSHOT",
scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature"),
scalaVersion := sversion,
scalaOrganization := "org.scala-lang.macro-paradise",
resolvers += Resolver.sonatypeRepo("snapshots"),
licenses := ("BSD 3-Clause", new java.net.URL("http://opensource.org/licenses/BSD-3-Clause")) :: Nil,
libraryDependencies ++= Seq("org.scala-lang.macro-paradise" % "scala-reflect" % sversion,
"org.scala-lang" % "scala-compiler" % sversion),
addCompilerPlugin("org.scala-lang.plugins" % "macro-paradise" % "2.0.0-SNAPSHOT" cross CrossVersion.full))
}
object LombrelloBuild extends Build {
import LombrelloBuildSettings._
lazy val root: Project = Project(
"root",
file("."),
settings = buildSettings ++ Seq(
run <<= run in Compile in tests
)
) aggregate (main, tests)
lazy val main: Project = Project(
"main",
file("src/main"),
settings = buildSettings
)
lazy val tests: Project = Project(
"tests",
file("src/test"),
settings = buildSettings ++ Seq(name := "tests")) dependsOn (main)
}
Using the scalaVersion 2.10-3-RC1, I get the following error:
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: UNRESOLVED DEPENDENCIES ::
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: org.scala-lang.macro-paradise#scala-library;2.10.3-RC1: not found
[warn] :: org.scala-lang.macro-paradise#scala-reflect;2.10.3-RC1: not found
[warn] :: org.scala-lang.macro-paradise#scala-compiler;2.10.3-RC1: not found
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
sbt.ResolveException: unresolved dependency: org.scala-lang.macro-paradise#scala-library;2.10.3-RC1: not found
unresolved dependency: org.scala-lang.macro-paradise#scala-reflect;2.10.3-RC1: not found
unresolved dependency: org.scala-lang.macro-paradise#scala-compiler;2.10.3-RC1: not found
at sbt.IvyActions$.sbt$IvyActions$$resolve(IvyActions.scala:213)
at sbt.IvyActions$$anonfun$update$1.apply(IvyActions.scala:122)
Using, scalaVersion 2.11.0-SNAPSHOT, I got the following error:
java.lang.NoClassDefFoundError: scala/tools/nsc/typechecker/TypersTracking$class
at org.scalalang.macroparadise.Plugin$$anon$1.<init>(Plugin.scala:20)
at org.scalalang.macroparadise.Plugin.<init>(Plugin.scala:20)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
While using the version 2.10.3-SNAPSHOT I got the following:
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: UNRESOLVED DEPENDENCIES ::
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: org.scala-lang.plugins#macro-paradise_2.10.3-SNAPSHOT;2.0.0-SNAPSHOT: not found
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
sbt.ResolveException: unresolved dependency: org.scala-lang.plugins#macro-paradise_2.10.3-SNAPSHOT;2.0.0-SNAPSHOT: not found
And version 2.10.2 couldn't resolve the dependencies of scala-library, scala-reflect and 2.10.2 at all (like 2.10.3-RC1)!
My question is, is it at all possible to mix both compiler API and Macro API and make them work under SBT, if yes what exactly is wrong with my build script?
It appeared that I used some wrong settings in my SBT configuration. I didn't need to change the scalaOrganization, neither needed to add macro-paradise to my library dependencies. so the settings should become like:
val sversion = "2.10.2"
val buildSettings = Defaults.defaultSettings ++ Seq(
name := "lombrello",
organization := "ch.usi.inf.l3",
version := "0.1-SNAPSHOT",
scalacOptions ++= Seq("-unchecked", "-deprecation", "-feature"),
scalaVersion := sversion,
resolvers += Resolver.sonatypeRepo("snapshots"),
licenses := ("BSD 3-Clause", new java.net.URL("http://opensource.org/licenses/BSD-3-Clause")) :: Nil,
libraryDependencies ++= Seq("org.scala-lang" % "scala-reflect" % sversion,
"org.scala-lang" % "scala-compiler" % sversion),
addCompilerPlugin("org.scala-lang.plugins" % "macro-paradise" % "2.0.0-SNAPSHOT" cross CrossVersion.full)
)
All credits go to Eugene Burmako in this comment.