Skip to content

Commit

Permalink
Fix depending on non-existing objects from imports.
Browse files Browse the repository at this point in the history
  • Loading branch information
romanowski committed Oct 7, 2017
1 parent 22623b4 commit 25734c3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,8 @@ final class Dependency(val global: CallbackGlobal) extends LocateClassFile with
case ImportSelector(name: Name, _, _, _) =>
def lookupImported(name: Name) = expr.symbol.info.member(name)
// importing a name means importing both a term and a type (if they exist)
addDependency(lookupImported(name.toTermName))
val termSymbol = lookupImported(name.toTermName)
if (termSymbol.info != NoType) addDependency(termSymbol)
addDependency(lookupImported(name.toTypeName))
}
inImportNode = false
Expand Down
4 changes: 2 additions & 2 deletions zinc/src/test/resources/sources/foo/NoopMacro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package foo

import scala.reflect.macros.blackbox.Context

class NoopMacro(val c: Context){
class NoopMacro(val c: Context) {
def noop(arg: c.Tree): c.Tree = arg
}
}
37 changes: 20 additions & 17 deletions zinc/src/test/scala/sbt/inc/BaseCompilerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ class BaseCompilerSpec extends BridgeProviderSpecification {
case class ProjectSetup(baseLocation: Path,
sources: Map[Path, Seq[Path]],
classPath: Seq[Path],
analysisForCp: Map[File, File] = Map.empty
) {
analysisForCp: Map[File, File] = Map.empty) {
private def fromResource(prefix: Path)(path: Path): File = {
val fullPath = prefix.resolve(path).toString()
Option(getClass.getClassLoader.getResource(fullPath))
Expand All @@ -64,7 +63,7 @@ class BaseCompilerSpec extends BridgeProviderSpecification {
val target = classpathBase.resolve(zippedClassesPath.toString.dropRight(4)).toFile
IO.unzip(fromResource(binPrefix)(zippedClassesPath), target)
target
case existingFile if existingFile.isAbsolute && Files.exists(existingFile) =>
case existingFile if existingFile.isAbsolute && Files.exists(existingFile) =>
existingFile.toFile
case jarPath =>
val newJar = classpathBase.resolve(jarPath).toFile
Expand All @@ -78,13 +77,12 @@ class BaseCompilerSpec extends BridgeProviderSpecification {

def createCompiler() =
CompilerSetup(defaultClassesDir,
baseLocation.toFile,
allSources.toArray,
allClasspath,
IncOptions.of(),
analysisForCp,
defaultStoreLocation
)
baseLocation.toFile,
allSources.toArray,
allClasspath,
IncOptions.of(),
analysisForCp,
defaultStoreLocation)

def update(source: Path)(change: String => String): Unit = {
import collection.JavaConverters._
Expand All @@ -95,23 +93,25 @@ class BaseCompilerSpec extends BridgeProviderSpecification {

def dependsOnJarFrom(other: ProjectSetup): ProjectSetup = {
val sources = other.defaultClassesDir ** "*.class"
val mapping = sources.get.map{
file =>
file -> other.defaultClassesDir.toPath.relativize(file.toPath).toString
val mapping = sources.get.map { file =>
file -> other.defaultClassesDir.toPath.relativize(file.toPath).toString
}
val dest = baseLocation.resolve("bin").resolve(s"${other.baseLocation.getFileName}.jar")
IO.zip(mapping, dest.toFile)

copy(
classPath = classPath :+ dest ,
classPath = classPath :+ dest,
analysisForCp = analysisForCp + (dest.toFile -> other.defaultStoreLocation)
)
}
}

object ProjectSetup {
def simple(baseLocation: Path, classes: Seq[String]): ProjectSetup =
ProjectSetup(baseLocation, Map(Paths.get("src") -> classes.map(path => Paths.get(path))), Nil, Map.empty)
ProjectSetup(baseLocation,
Map(Paths.get("src") -> classes.map(path => Paths.get(path))),
Nil,
Map.empty)
}

def scalaCompiler(instance: xsbti.compile.ScalaInstance, bridgeJar: File): AnalyzingCompiler = {
Expand Down Expand Up @@ -141,7 +141,9 @@ class BaseCompilerSpec extends BridgeProviderSpecification {
private def analysis(forEntry: File): Optional[CompileAnalysis] = {
analysisForCp.get(forEntry) match {
case Some(analysisStore) =>
FileAnalysisStore.getDefault(analysisStore).get().map(_.getAnalysis)
val content = FileAnalysisStore.getDefault(analysisStore).get()
if (content.isPresent) Optional.of(content.get().getAnalysis)
else Optional.empty()
case _ =>
Optional.empty()
}
Expand Down Expand Up @@ -184,7 +186,8 @@ class BaseCompilerSpec extends BridgeProviderSpecification {
compiler.compile(newInputs(in), log)
}

def doCompileWithStore(store: AnalysisStore = FileAnalysisStore.getDefault(analysisStoreLocation),
def doCompileWithStore(store: AnalysisStore =
FileAnalysisStore.getDefault(analysisStoreLocation),
newInputs: Inputs => Inputs = identity): CompileResult = {
import JavaInterfaceUtil.EnrichOptional
val previousResult = store.get().toOption match {
Expand Down
7 changes: 4 additions & 3 deletions zinc/src/test/scala/sbt/inc/BinaryDepSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ package sbt.inc
import sbt.internal.inc.Analysis
import sbt.io.IO

class BinaryDepSpec extends BaseCompilerSpec{
class BinaryDepSpec extends BaseCompilerSpec {
it should "not depend on non-existing objects" in {
IO.withTemporaryDirectory { tempDir =>
val basePath = tempDir.toPath.resolve("base")
val baseSetup = ProjectSetup.simple(basePath, Seq("foo/NoopMacro.scala"))
baseSetup.createCompiler().doCompileWithStore()

val projPath = tempDir.toPath.resolve("proj")
val projectSetup = ProjectSetup.simple(projPath, Seq("NoopMacroUsed.scala")).dependsOnJarFrom(baseSetup)
val projectSetup =
ProjectSetup.simple(projPath, Seq("NoopMacroUsed.scala")).dependsOnJarFrom(baseSetup)

val result = projectSetup.createCompiler().doCompile()
result.analysis() match {
case analysis: Analysis =>
// We should not depend on jar creating from project that we depend on (since we've got analysis for it)
analysis.relations.libraryDep._2s
.filter(_.toPath.startsWith(projPath)) shouldBe 'empty
.filter(_.toPath.startsWith(tempDir.toPath)) shouldBe 'empty

}
}
Expand Down

0 comments on commit 25734c3

Please sign in to comment.