Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: Redownload artifacts if missing #2604

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions backend/src/main/scala/bloop/DependencyResolution.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ object DependencyResolution {
* @param module The module's name.
* @param version The module's version.
*/
case class Artifact(organization: String, module: String, version: String)
case class Artifact(
organization: String,
module: String,
version: String,
classifier: Option[String] = None
)

/**
* Resolve the specified modules and get all the files. By default, the local Ivy
Expand Down Expand Up @@ -59,8 +64,13 @@ object DependencyResolution {
import artifact._
logger.debug(s"Resolving $organization:$module:$version")(DebugFilter.All)
val baseDep = coursierapi.Dependency.of(organization, module, version)
if (resolveSources) baseDep.withClassifier("sources")
else baseDep
classifier match {
case Some(c) => baseDep.withClassifier(c)
case None =>
if (resolveSources) baseDep.withClassifier("sources")
else baseDep
}

}
resolveDependenciesWithErrors(dependencies, resolveSources, additionalRepositories)
}
Expand Down
19 changes: 18 additions & 1 deletion frontend/src/main/scala/bloop/data/Project.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bloop.data

import java.nio.charset.StandardCharsets
import java.nio.file.Files

import scala.collection.mutable
import scala.util.Properties
Expand All @@ -10,6 +11,7 @@ import scala.util.control.NonFatal

import ch.epfl.scala.{bsp => Bsp}

import bloop.DependencyResolution
import bloop.ScalaInstance
import bloop.bsp.ProjectUris
import bloop.config.Config
Expand Down Expand Up @@ -271,6 +273,22 @@ object Project {
val project = file.project
val scala = project.`scala`

// Resolve missing artifacts if they don't exist in the file system
file.project.resolution.toIterable
.flatMap(_.modules)
.map { module =>
val missingArtifacts = module.artifacts.filter(art => !Files.exists(art.path))
missingArtifacts.foreach { artifact =>
DependencyResolution.resolve(
List(
DependencyResolution
.Artifact(module.organization, module.name, module.version, artifact.classifier)
),
logger
)
}
}

// Use the default Bloop scala instance if it's not a Scala project or if Scala jars are empty
val instance = scala
.flatMap { scala =>
Expand All @@ -289,7 +307,6 @@ object Project {
)
}
}

val setup = project.`scala`.flatMap(_.setup).getOrElse(Config.CompileSetup.empty)
val compileClasspath = project.classpath.map(AbsolutePath.apply)
val compileResources = project.resources.toList.flatten.map(AbsolutePath.apply)
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
trait A
trait A {
def hello(implicit file: sourcecode.File, line: sourcecode.Line): Unit = {
println(s"Hello from ${file.value}:${line.value}")
}
}
4 changes: 3 additions & 1 deletion frontend/src/test/resources/simple-build/build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
bloopExportJarClassifiers in Global := Some(Set("sources"))
bloopConfigDir in Global := baseDirectory.value / "bloop-config"

val a = project
val a = project.settings(
libraryDependencies += "com.lihaoyi" %% "sourcecode" % "0.4.2"
)
val b = project.dependsOn(a)
41 changes: 41 additions & 0 deletions frontend/src/test/scala/bloop/bsp/BspCompileSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,47 @@ class BspCompileSpec(
}
}

test("reconnect and redownload dependencies") {
TestUtil.withinWorkspace { workspace =>
object Sources {
val `App.scala` =
"""/main/scala/App.scala
|object App {
| def main(args: Array[String]): Unit = {}
|}
""".stripMargin
}

val cliLogger = new RecordingLogger(ansiCodesSupported = false)
val bspLogger = new RecordingLogger(ansiCodesSupported = false)
val `A` = TestProject(workspace, "a", List(Sources.`App.scala`))
val projects = List(`A`)
val cliState = loadState(workspace, projects, cliLogger)

val cliCompiledState = cliState.compile(`A`)
assertExitStatus(cliCompiledState, ExitStatus.Ok)
assertValidCompilationState(cliCompiledState, projects)
assertNoDiff(
cliLogger.compilingInfos.mkString(lineSeparator),
"Compiling a (1 Scala source)"
)

cliCompiledState.build.loadedProjects.foreach { project =>
println(s"Deleting ${project.project.scalaInstance}")
project.project.resolution.toIterable.flatMap(_.modules).flatMap(_.artifacts).foreach {
artifact =>
println(s"Deleting ${artifact.path}")
artifact.path.toFile().delete()
}
}

loadBspState(workspace, projects, bspLogger) { state =>
val compiledState = state.compile(`A`)
assertExitStatus(compiledState, ExitStatus.Ok)
}
}
}

test("compile incrementally and clear old errors fixed in previous CLI compilations") {
TestUtil.withinWorkspace { workspace =>
object Sources {
Expand Down
25 changes: 25 additions & 0 deletions frontend/src/test/scala/bloop/bsp/BspProtocolSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,31 @@ class BspProtocolSpec(
}
}

test("find test classes") {
TestUtil.withinWorkspace { workspace =>
val logger = new RecordingLogger(ansiCodesSupported = false)
loadBspBuildFromResources("simple-build", workspace, logger) { build =>
val project = build.projectFor("a")
val compiledState = build.state.compile(project, timeout = 120)

compiledState.state.build.loadedProjects.foreach { project =>
assert(compiledState.status == ExitStatus.Ok)
project.project.resolution.toIterable.flatMap(_.modules).flatMap(_.artifacts).foreach {
artifact =>
if (artifact.path.toString.contains("sourcecode")) {
artifact.path.toFile().delete()
}
}
}
}
loadBspBuildFromResources("simple-build", workspace, logger) { build =>
val project = build.projectFor("a")
val compiledState = build.state.compile(project, timeout = 120)
assert(compiledState.status == ExitStatus.Ok)
}
}
}

test("build targets request works on complicated build") {
TestUtil.withinWorkspace { workspace =>
val logger = new RecordingLogger(ansiCodesSupported = false)
Expand Down
Loading