Skip to content

Commit

Permalink
Add a timeout to the BSP process
Browse files Browse the repository at this point in the history
  • Loading branch information
fthomas committed Dec 12, 2023
1 parent 7e30a9b commit 7f81790
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ object Context {
implicit val updateAlg: UpdateAlg[F] = new UpdateAlg[F]
implicit val mavenAlg: MavenAlg[F] = new MavenAlg[F](config)
implicit val sbtAlg: SbtAlg[F] = new SbtAlg[F](config)
implicit val bspExtractor: BspExtractor[F] = new BspExtractor[F](config.defaultResolver)
implicit val bspExtractor: BspExtractor[F] =
new BspExtractor[F](config.defaultResolver, config.processCfg.processTimeout)
implicit val bazelAlg: BazelAlg[F] = new BazelAlg[F]
implicit val bleepAlg: BleepAlg[F] = new BleepAlg[F]
implicit val millAlg: MillAlg[F] = new MillAlg[F](config.defaultResolver)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ import java.util.concurrent.CompletableFuture
import org.scalasteward.core.buildtool.BuildRoot
import org.scalasteward.core.data._
import org.scalasteward.core.io.{FileAlg, ProcessAlg, WorkspaceAlg}
import scala.concurrent.duration.FiniteDuration
import scala.jdk.CollectionConverters._

final class BspExtractor[F[_]](defaultResolver: Resolver)(implicit
final class BspExtractor[F[_]](defaultResolver: Resolver, processTimeout: FiniteDuration)(implicit
fileAlg: FileAlg[F],
processAlg: ProcessAlg[F],
workspaceAlg: WorkspaceAlg[F],
Expand Down Expand Up @@ -67,38 +68,43 @@ final class BspExtractor[F[_]](defaultResolver: Resolver)(implicit
case Right(connectionDetails) => F.pure(connectionDetails)
case Left(error) => F.raiseError(error)

Check warning on line 69 in modules/core/src/main/scala/org/scalasteward/core/buildtool/bsp/BspExtractor.scala

View check run for this annotation

Codecov / codecov/patch

modules/core/src/main/scala/org/scalasteward/core/buildtool/bsp/BspExtractor.scala#L67-L69

Added lines #L67 - L69 were not covered by tests
}
case None => F.raiseError(new Throwable)
case None =>
F.raiseError(new Throwable(s"${connectionDetailsFile.pathAsString} does not exist"))

Check warning on line 72 in modules/core/src/main/scala/org/scalasteward/core/buildtool/bsp/BspExtractor.scala

View check run for this annotation

Codecov / codecov/patch

modules/core/src/main/scala/org/scalasteward/core/buildtool/bsp/BspExtractor.scala#L72

Added line #L72 was not covered by tests
}

private def getBspDependencies(
buildRootDir: File,
details: BspConnectionDetails
): F[DependencyModulesResult] =
BspProcess.run(details.argv, buildRootDir).use { p =>
val initBuildParams = new InitializeBuildParams(
"Scala Steward",
org.scalasteward.core.BuildInfo.version,
org.scalasteward.core.BuildInfo.bsp4jVersion,
buildRootDir.uri.toString,
new BuildClientCapabilities(Collections.emptyList())
)
(for {
initBuildResult <- lift(p.buildInitialize(initBuildParams))
_ <- F.blocking(p.onBuildInitialized())
isDependencyModulesProvider =
Option(initBuildResult.getCapabilities.getDependencyModulesProvider)
.exists(_.booleanValue())
_ <- F.raiseWhen(!isDependencyModulesProvider) {
new Throwable(s"${initBuildResult.getDisplayName} is not a dependency modules provider")
}
buildTargetsResult <- lift(p.workspaceBuildTargets())
dependencyModulesParams =
new DependencyModulesParams(buildTargetsResult.getTargets.asScala.map(_.getId).asJava)
dependencyModulesResult <- lift(p.buildTargetDependencyModules(dependencyModulesParams))
} yield dependencyModulesResult).guarantee {
lift(p.buildShutdown()) >> F.blocking(p.onBuildExit())
BspProcess
.run(details.argv, buildRootDir)
.use { p =>

Check warning on line 81 in modules/core/src/main/scala/org/scalasteward/core/buildtool/bsp/BspExtractor.scala

View check run for this annotation

Codecov / codecov/patch

modules/core/src/main/scala/org/scalasteward/core/buildtool/bsp/BspExtractor.scala#L80-L81

Added lines #L80 - L81 were not covered by tests
val result = for {
initBuildResult <- lift(p.buildInitialize(initBuildParams(buildRootDir)))
_ <- F.blocking(p.onBuildInitialized())

Check warning on line 84 in modules/core/src/main/scala/org/scalasteward/core/buildtool/bsp/BspExtractor.scala

View check run for this annotation

Codecov / codecov/patch

modules/core/src/main/scala/org/scalasteward/core/buildtool/bsp/BspExtractor.scala#L83-L84

Added lines #L83 - L84 were not covered by tests
isDependencyModulesProvider =
Option(initBuildResult.getCapabilities.getDependencyModulesProvider)
.exists(_.booleanValue())
_ <- F.raiseWhen(!isDependencyModulesProvider) {
new Throwable(s"${initBuildResult.getDisplayName} is not a dependency modules provider")

Check warning on line 89 in modules/core/src/main/scala/org/scalasteward/core/buildtool/bsp/BspExtractor.scala

View check run for this annotation

Codecov / codecov/patch

modules/core/src/main/scala/org/scalasteward/core/buildtool/bsp/BspExtractor.scala#L86-L89

Added lines #L86 - L89 were not covered by tests
}
buildTargetsResult <- lift(p.workspaceBuildTargets())

Check warning on line 91 in modules/core/src/main/scala/org/scalasteward/core/buildtool/bsp/BspExtractor.scala

View check run for this annotation

Codecov / codecov/patch

modules/core/src/main/scala/org/scalasteward/core/buildtool/bsp/BspExtractor.scala#L91

Added line #L91 was not covered by tests
dependencyModulesParams =
new DependencyModulesParams(buildTargetsResult.getTargets.asScala.map(_.getId).asJava)
dependencyModulesResult <- lift(p.buildTargetDependencyModules(dependencyModulesParams))

Check warning on line 94 in modules/core/src/main/scala/org/scalasteward/core/buildtool/bsp/BspExtractor.scala

View check run for this annotation

Codecov / codecov/patch

modules/core/src/main/scala/org/scalasteward/core/buildtool/bsp/BspExtractor.scala#L93-L94

Added lines #L93 - L94 were not covered by tests
} yield dependencyModulesResult
result.guarantee(lift(p.buildShutdown()) >> F.blocking(p.onBuildExit()))

Check warning on line 96 in modules/core/src/main/scala/org/scalasteward/core/buildtool/bsp/BspExtractor.scala

View check run for this annotation

Codecov / codecov/patch

modules/core/src/main/scala/org/scalasteward/core/buildtool/bsp/BspExtractor.scala#L96

Added line #L96 was not covered by tests
}
}
.timeoutAndForget(processTimeout)

Check warning on line 98 in modules/core/src/main/scala/org/scalasteward/core/buildtool/bsp/BspExtractor.scala

View check run for this annotation

Codecov / codecov/patch

modules/core/src/main/scala/org/scalasteward/core/buildtool/bsp/BspExtractor.scala#L98

Added line #L98 was not covered by tests

private def initBuildParams(buildRootDir: File): InitializeBuildParams =
new InitializeBuildParams(
"Scala Steward",
org.scalasteward.core.BuildInfo.version,
org.scalasteward.core.BuildInfo.bsp4jVersion,
buildRootDir.uri.toString,
new BuildClientCapabilities(Collections.emptyList())

Check warning on line 106 in modules/core/src/main/scala/org/scalasteward/core/buildtool/bsp/BspExtractor.scala

View check run for this annotation

Codecov / codecov/patch

modules/core/src/main/scala/org/scalasteward/core/buildtool/bsp/BspExtractor.scala#L101-L106

Added lines #L101 - L106 were not covered by tests
)

private def lift[A](fut: => CompletableFuture[A]): F[A] =
F.fromCompletableFuture(F.blocking(fut))

Check warning on line 110 in modules/core/src/main/scala/org/scalasteward/core/buildtool/bsp/BspExtractor.scala

View check run for this annotation

Codecov / codecov/patch

modules/core/src/main/scala/org/scalasteward/core/buildtool/bsp/BspExtractor.scala#L110

Added line #L110 was not covered by tests
Expand All @@ -115,6 +121,9 @@ final class BspExtractor[F[_]](defaultResolver: Resolver)(implicit
case _ => None

Check warning on line 121 in modules/core/src/main/scala/org/scalasteward/core/buildtool/bsp/BspExtractor.scala

View check run for this annotation

Codecov / codecov/patch

modules/core/src/main/scala/org/scalasteward/core/buildtool/bsp/BspExtractor.scala#L116-L121

Added lines #L116 - L121 were not covered by tests
}
}
// The BSP does not yet provide resolvers, so we use the default resolver here.
// See https://github.com/build-server-protocol/build-server-protocol/discussions/500
// for a proposal to add resolvers to BSP.
Scope(dependencies, List(defaultResolver))

Check warning on line 127 in modules/core/src/main/scala/org/scalasteward/core/buildtool/bsp/BspExtractor.scala

View check run for this annotation

Codecov / codecov/patch

modules/core/src/main/scala/org/scalasteward/core/buildtool/bsp/BspExtractor.scala#L127

Added line #L127 was not covered by tests
}
}

0 comments on commit 7f81790

Please sign in to comment.