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

Revert creating .scala-build in temporary directory #5918

Merged
merged 3 commits into from
Dec 6, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import java.net.URI
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
import java.util.concurrent.atomic.AtomicBoolean
Expand All @@ -22,7 +21,6 @@ import scala.util.Properties
import scala.util.Success
import scala.util.control.NonFatal

import scala.meta.internal.async.ConcurrentQueue
import scala.meta.internal.bsp.BuildChange
import scala.meta.internal.metals.Buffers
import scala.meta.internal.metals.BuildInfo
Expand Down Expand Up @@ -65,31 +63,16 @@ class ScalaCli(
)(implicit ec: ExecutionContextExecutorService)
extends Cancelable {

private val createdFiles = new ConcurrentLinkedQueue[AbsolutePath]()
private def registerFile(path: AbsolutePath) = {
createdFiles.add(path)
path
}
private def localTmpWorkspace(path: AbsolutePath) = {
val root = if (path.isDirectory) path else path.parent
root.resolve(s".metals-scala-cli/")
}
private val scalaCliBuildDirectory =
new AtomicReference[Option[AbsolutePath]](None)

import ScalaCli.ConnectionState

private val isCancelled = new AtomicBoolean(false)
def cancel(): Unit =
if (isCancelled.compareAndSet(false, true))
try {
scalaCliBuildDirectory.set(None)
disconnectOldBuildServer().asJava.get(100, TimeUnit.MILLISECONDS)
} catch {
case NonFatal(_) =>
case _: TimeoutException =>
} finally {
ConcurrentQueue.pollAll(createdFiles).foreach(_.deleteRecursively())
}

private val state =
Expand Down Expand Up @@ -226,44 +209,13 @@ class ScalaCli(
ifConnectedOrElse(st => Option(st.path))(None)

def start(path: AbsolutePath): Future[Unit] = {
val workspace = {
val globalTmpDir =
scalaCliBuildDirectory.get() match {
case Some(workspace) => workspace
case None =>
val tmpFile = registerFile {
AbsolutePath(
Files.createTempDirectory(s"metals-scala-cli")
)
}
val Some(workspace) =
scalaCliBuildDirectory.updateAndGet {
case None => Some(tmpFile)
case some => some
}
workspace
}

// When path and workspace have different roots on Windows `scala-cli` throws an error,
// so we fallback to creating a tmp dir relative to `path`.
if (globalTmpDir.toNIO.getRoot() == path.toNIO.getRoot()) globalTmpDir
else
registerFile {
val workspace = localTmpWorkspace(path)
if (!workspace.exists) Files.createDirectory(workspace.toNIO)
workspace
}
}

disconnectOldBuildServer().onComplete {
case Failure(e) =>
scribe.warn("Error disconnecting old Scala CLI server", e)
case Success(()) =>
}

val command =
cliCommand :+ "bsp" :+ "--workspace" :+ workspace.toString :+ path
.toString()
val command = cliCommand :+ "bsp" :+ path.toString()

val connDir = if (path.isDirectory) path else path.parent

Expand Down