Skip to content

Commit

Permalink
improvement: Timeout when resolving mtags for a long time
Browse files Browse the repository at this point in the history
Otherwise this timeouts tests and can actually hang metals totally.
  • Loading branch information
tgodzik committed Jul 30, 2024
1 parent ab9b8e8 commit e81e5db
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package scala.meta.internal.metals

import java.util.concurrent.ConcurrentHashMap

import scala.concurrent.Await
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.concurrent.duration._
import scala.util.control.NonFatal

Expand All @@ -14,14 +17,18 @@ trait MtagsResolver {
* Try and resolve mtags module for a given version of Scala.
* @return information to use and load the presentation compiler implementation
*/
def resolve(scalaVersion: String): Option[MtagsBinaries]
def resolve(scalaVersion: String)(implicit
ec: ExecutionContext
): Option[MtagsBinaries]

/**
* Check if a given Scala version is supported in Metals.
*
* @param version Scala version to check
*/
def isSupportedScalaVersion(version: String): Boolean =
def isSupportedScalaVersion(version: String)(implicit
ec: ExecutionContext
): Boolean =
resolve(version).isDefined

/**
Expand Down Expand Up @@ -102,7 +109,9 @@ object MtagsResolver {
def isSupportedInOlderVersion(version: String): Boolean =
removedScalaVersions.contains(version)

def resolve(scalaVersion: String): Option[MtagsBinaries] = {
def resolve(
scalaVersion: String
)(implicit ec: ExecutionContext): Option[MtagsBinaries] = {
if (hasStablePresentationCompiler(scalaVersion))
resolve(
scalaVersion,
Expand Down Expand Up @@ -130,7 +139,7 @@ object MtagsResolver {
scalaVersion: String,
original: Option[String],
resolveType: ResolveType.Value,
): Option[MtagsBinaries] = {
)(implicit ec: ExecutionContext): Option[MtagsBinaries] = {

def fetch(fetchResolveType: ResolveType.Value, tries: Int = 5): State =
try {
Expand All @@ -143,12 +152,16 @@ object MtagsResolver {
s"$scalaVersion is no longer supported in the current Metals versions, using the last known supported version $metalsVersion"
)
}
val jars = fetchResolveType match {
case ResolveType.StablePC =>
Embedded.downloadScala3PresentationCompiler(scalaVersion)
case _ => Embedded.downloadMtags(scalaVersion, metalsVersion)
val jarsFuture = Future {
fetchResolveType match {
case ResolveType.StablePC =>
Embedded.downloadScala3PresentationCompiler(scalaVersion)
case _ => Embedded.downloadMtags(scalaVersion, metalsVersion)
}
}

val jars = Await.result(jarsFuture, 60.seconds)

State.Success(
MtagsBinaries.Artifacts(
scalaVersion,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scala.meta.internal.metals.doctor

import scala.collection.mutable.ListBuffer
import scala.concurrent.ExecutionContext

import scala.meta.internal.bsp.BspSession
import scala.meta.internal.metals.BloopServers
Expand All @@ -23,7 +24,7 @@ class ProblemResolver(
currentBuildServer: () => Option[BspSession],
isTestExplorerProvider: () => Boolean,
javaInfo: () => Option[JavaInfo],
) {
)(implicit ec: ExecutionContext) {

def isUnsupportedBloopVersion(): Boolean = {
currentBuildServer() match {
Expand Down
6 changes: 5 additions & 1 deletion tests/unit/src/main/scala/tests/TestMtagsResolver.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tests

import scala.concurrent.ExecutionContext

import scala.meta.internal.metals.MtagsBinaries
import scala.meta.internal.metals.MtagsResolver
import scala.meta.internal.metals.ScalaVersions
Expand All @@ -19,7 +21,9 @@ class TestMtagsResolver(checkCoursier: Boolean) extends MtagsResolver {
Some(MtagsBinaries.BuildIn)
else None

override def resolve(scalaVersion: String): Option[MtagsBinaries] = {
override def resolve(
scalaVersion: String
)(implicit ec: ExecutionContext): Option[MtagsBinaries] = {
if (checkCoursier)
default.resolve(scalaVersion).orElse(localCheck(scalaVersion))
else localCheck(scalaVersion)
Expand Down

0 comments on commit e81e5db

Please sign in to comment.