2014-03-26 15 views
2

我與解析瞎搞和斯卡拉scala.tools.nsc.interactive.Global並同時在SBT執行測試中,我遇到了一個問題。 Eclipse的測試運行良好,包括JUnitRunnerScalaTest插件。在Google上花了很長時間之後,我無法弄清楚如何解決這個問題。測試在Eclipse的作品,但SBT拋出MissingRequirementError:在編譯器鏡對象scala.runtime沒有找到

當我執行sbt test拋出以下錯誤:

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] ... 

類被測

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 
    } 

} 

測試

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" 

環境:

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" 

回答

2

scala-library.jar不是從sbt的classpath中丟失的,而是從Global的類路徑中丟失的。必須在代碼中設置它。

修改源到

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") 

後問題消失。

感謝提示@blueberryfields!

2

它看起來像斯卡拉罐子運行SBT時,是不是在你的類路徑 - 確保斯卡拉 - library.jar之前添加到您的類路徑你運行sbt。


根據你的一條評論,它看起來像你在windows上運行。如果類路徑包含奇怪的字符或空格,或許可錯誤(例如,如果eclipse在管理員帳戶下運行,而sbt不在),那麼您可能也會在那裏運行jar運行時訪問錯誤。


嘗試對您的依賴列表重新排序,以將scala庫放在scala編譯器之前。如果這不起作用,請嘗試使用故障排除建議here

+1

編譯和測試類路徑中包含Scala的庫-2.10.3.jar與SBT檢查它顯示測試:依賴性的類路徑「和SBT「顯示編譯:依賴關係的類路徑」還是我做錯了什麼? – csferi27

+0

我看到scala-reflect,而不是scala-library。這是一個運行時間,反射錯誤 - 當文件編譯時你不會看到它。 – blueberryfields

+0

(顯示測試:依賴關係類路徑應顯示它) – blueberryfields

相關問題