From 84ee5875546c1604e5966b8d72f0bacf8961cbdb Mon Sep 17 00:00:00 2001 From: Tomasz Godzik Date: Thu, 31 Oct 2024 13:28:26 +0100 Subject: [PATCH] bugfix: Use last modified time instead of creation time for detecting orphaned directories Somehow this was an issue on linux and JDK 17, but not sure why not others. I also fixed getting stdout to get it redirected to the logger, my last change did not fully fix it. --- .../src/main/scala/bloop/data/ClientInfo.scala | 2 +- frontend/src/main/scala/bloop/engine/State.scala | 2 +- .../scala/bloop/engine/caches/ResultsCache.scala | 3 +++ .../test/scala/bloop/bsp/BspCompileSpec.scala | 2 +- .../bloop/logging/FlushingOutputStream.scala | 16 ++++++++++++++++ shared/src/main/scala/bloop/logging/Logger.scala | 5 ++++- 6 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 shared/src/main/scala/bloop/logging/FlushingOutputStream.scala diff --git a/frontend/src/main/scala/bloop/data/ClientInfo.scala b/frontend/src/main/scala/bloop/data/ClientInfo.scala index ff27c766fc..ace6af0d32 100644 --- a/frontend/src/main/scala/bloop/data/ClientInfo.scala +++ b/frontend/src/main/scala/bloop/data/ClientInfo.scala @@ -292,7 +292,7 @@ object ClientInfo { val dirName = clientDir.underlying.getFileName().toString val attrs = Files.readAttributes(clientDir.underlying, classOf[BasicFileAttributes]) - val isOldDir = attrs.creationTime.toInstant.isBefore(deletionThresholdInstant) + val isOldDir = attrs.lastModifiedTime.toInstant.isBefore(deletionThresholdInstant) val isAllowed = CliClientInfo.isStableDirName(dirName) || connectedBspClientIds.exists(clientId => dirName.endsWith(s"-$clientId")) diff --git a/frontend/src/main/scala/bloop/engine/State.scala b/frontend/src/main/scala/bloop/engine/State.scala index 2ca0e1ecd9..a5364e5ded 100644 --- a/frontend/src/main/scala/bloop/engine/State.scala +++ b/frontend/src/main/scala/bloop/engine/State.scala @@ -67,7 +67,7 @@ object State { val opts = CommonOptions.default val cwd = opts.workingPath val clientInfo = ClientInfo.CliClientInfo(useStableCliDirs = true, () => true) - val results = ResultsCache.load(build, cwd, cleanOrphanedInternalDirs = false, logger) + val results = ResultsCache.load(build, cwd, cleanOrphanedInternalDirs = true, logger) State( build, results, diff --git a/frontend/src/main/scala/bloop/engine/caches/ResultsCache.scala b/frontend/src/main/scala/bloop/engine/caches/ResultsCache.scala index b0b15efb68..0b375a5a67 100644 --- a/frontend/src/main/scala/bloop/engine/caches/ResultsCache.scala +++ b/frontend/src/main/scala/bloop/engine/caches/ResultsCache.scala @@ -208,6 +208,9 @@ object ResultsCache { fileName.startsWith(genericClassesName) && path != analysisClassesDir.underlying if (isOrphan) { + logger.debug( + s"Discovered orphan directory $path" + )(DebugFilter.All) orphanInternalDirs.+=(path) } } diff --git a/frontend/src/test/scala/bloop/bsp/BspCompileSpec.scala b/frontend/src/test/scala/bloop/bsp/BspCompileSpec.scala index c65218da57..d73348a88b 100644 --- a/frontend/src/test/scala/bloop/bsp/BspCompileSpec.scala +++ b/frontend/src/test/scala/bloop/bsp/BspCompileSpec.scala @@ -172,7 +172,7 @@ class BspCompileSpec( } } - testMac( + testNonWindows( "create orphan client classes directory and make sure loading a BSP session cleans it up" ) { TestUtil.withinWorkspace { workspace => diff --git a/shared/src/main/scala/bloop/logging/FlushingOutputStream.scala b/shared/src/main/scala/bloop/logging/FlushingOutputStream.scala new file mode 100644 index 0000000000..f238756b59 --- /dev/null +++ b/shared/src/main/scala/bloop/logging/FlushingOutputStream.scala @@ -0,0 +1,16 @@ +package bloop.logging + +import java.io.ByteArrayOutputStream + +class FlushingOutputStream(write: String => Unit) extends ByteArrayOutputStream { + + override def flush(): Unit = { + write(this.toByteArray().toString()) + } + + override def close(): Unit = { + flush(); + super.close(); + } + +} diff --git a/shared/src/main/scala/bloop/logging/Logger.scala b/shared/src/main/scala/bloop/logging/Logger.scala index dcaa554049..62927589a6 100644 --- a/shared/src/main/scala/bloop/logging/Logger.scala +++ b/shared/src/main/scala/bloop/logging/Logger.scala @@ -9,7 +9,10 @@ abstract class Logger extends xsbti.Logger with BaseSbtLogger { // Duplicate the standard output so that we get printlns from the compiler protected def redirectOutputToLogs(out: PrintStream) = { - System.setOut(new TeeOutputStream(out)) + val baos = new FlushingOutputStream(info) + val tee = new TeeOutputStream(out) + tee.addListener(baos) + System.setOut(tee) } /** The name of the logger */