From ec7dffe7f282d5800976c88404e6e7d64f830f2f Mon Sep 17 00:00:00 2001 From: Katarzyna Marek Date: Mon, 20 Nov 2023 11:21:29 +0100 Subject: [PATCH] improvement: create `scala-cli` `.scala-build` in a tmp directory --- .../internal/metals/scalacli/ScalaCli.scala | 32 +++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/metals/src/main/scala/scala/meta/internal/metals/scalacli/ScalaCli.scala b/metals/src/main/scala/scala/meta/internal/metals/scalacli/ScalaCli.scala index 2c2bcf854d4..f46383951b8 100644 --- a/metals/src/main/scala/scala/meta/internal/metals/scalacli/ScalaCli.scala +++ b/metals/src/main/scala/scala/meta/internal/metals/scalacli/ScalaCli.scala @@ -60,14 +60,23 @@ class ScalaCli( parseTreesAndPublishDiags: Seq[AbsolutePath] => Future[Unit], )(implicit ec: ExecutionContextExecutorService) extends Cancelable { + 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 disconnectOldBuildServer() - catch { + try { + disconnectOldBuildServer().map { _ => + scalaCliBuildDirectory.get() match { + case Some(dir) => + dir.deleteRecursively() + case _ => + } + } + } catch { case NonFatal(_) => } @@ -205,13 +214,30 @@ class ScalaCli( ifConnectedOrElse(st => Option(st.path))(None) def start(path: AbsolutePath): Future[Unit] = { + val workspace = + scalaCliBuildDirectory.get() match { + case Some(workspace) => workspace + case None => + val tmpFile = AbsolutePath( + Files.createTempDirectory(s"metals-scala-cli") + ) + val Some(workspace) = + scalaCliBuildDirectory.updateAndGet { + case None => Some(tmpFile) + case some => some + } + workspace + } + disconnectOldBuildServer().onComplete { case Failure(e) => scribe.warn("Error disconnecting old Scala CLI server", e) case Success(()) => } - val command = cliCommand :+ "bsp" :+ path.toString() + val command = + cliCommand :+ "bsp" :+ "--workspace" :+ workspace.toString :+ path + .toString() val connDir = if (path.isDirectory) path else path.parent