From dd89b0d255ea5ef876d05f3f5a17a36447638bf2 Mon Sep 17 00:00:00 2001 From: Tomasz Godzik Date: Thu, 31 Oct 2024 13:28:26 +0100 Subject: [PATCH] test: Debug failing tests on linux --- .../main/scala/bloop/data/ClientInfo.scala | 8 +++- .../src/main/scala/bloop/engine/State.scala | 2 +- .../bloop/engine/caches/ResultsCache.scala | 3 ++ .../test/scala/bloop/bsp/BspCompileSpec.scala | 41 +++++++++++-------- .../bloop/logging/FlushingOutputStream.scala | 17 ++++++++ .../src/main/scala/bloop/logging/Logger.scala | 5 ++- 6 files changed, 56 insertions(+), 20 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..1f9c73748d 100644 --- a/frontend/src/main/scala/bloop/data/ClientInfo.scala +++ b/frontend/src/main/scala/bloop/data/ClientInfo.scala @@ -279,6 +279,7 @@ object ClientInfo { projectsToVisit.foreach { case (project, client) => + out.println(s"Parent ${client.parentForClientClassesDirectories(project)}") client.parentForClientClassesDirectories(project) match { case None => () // If owns build files, original generic classes dirs are used @@ -289,13 +290,18 @@ object ClientInfo { try { Paths.list(bspClientClassesDir).foreach { clientDir => try { + out.println(s"Checking for orphan directory in ${clientDir}") 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")) + out.println(s"isOldDir ${isOldDir}") + out.println(s"creationTime ${attrs.creationTime}") + out.println(s"lastModifiedTime ${attrs.lastModifiedTime()}") + out.println(s"deletionThresholdInstant ${deletionThresholdInstant}") if (isAllowed || !isOldDir) () else { out.println(s"Deleting orphan directory ${clientDir}") 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..367cde1bdc 100644 --- a/frontend/src/test/scala/bloop/bsp/BspCompileSpec.scala +++ b/frontend/src/test/scala/bloop/bsp/BspCompileSpec.scala @@ -21,6 +21,7 @@ import bloop.util.TestProject import bloop.util.TestUtil import coursierapi.Fetch +import scala.util.control.NonFatal object TcpBspCompileSpec extends BspCompileSpec(BspProtocol.Tcp) object LocalBspCompileSpec extends BspCompileSpec(BspProtocol.Local) @@ -172,7 +173,7 @@ class BspCompileSpec( } } - testMac( + testNonWindows( "create orphan client classes directory and make sure loading a BSP session cleans it up" ) { TestUtil.withinWorkspace { workspace => @@ -207,22 +208,28 @@ class BspCompileSpec( val _ = bspState.scalaOptions(`A`) } - // Wait until the extra directory is finally deleted at the end of the bsp session - TestUtil.await( - FiniteDuration(10, TimeUnit.SECONDS), - bloop.engine.ExecutionContext.ioScheduler - ) { - Task { - var check: Boolean = true - while (check) { - // The task cleaning up client classes directories should have removed the extra dir - check = orphanClientClassesDir.exists && orphanInternalClassesDir.exists - Thread.sleep(100) - } - }.timeoutTo( - FiniteDuration(5, TimeUnit.SECONDS), - Task(sys.error(s"Expected deletion of $orphanClientClassesDir")) - ) + try { + // Wait until the extra directory is finally deleted at the end of the bsp session + TestUtil.await( + FiniteDuration(10, TimeUnit.SECONDS), + bloop.engine.ExecutionContext.ioScheduler + ) { + Task { + var check: Boolean = true + while (check) { + // The task cleaning up client classes directories should have removed the extra dir + check = orphanClientClassesDir.exists && orphanInternalClassesDir.exists + Thread.sleep(100) + } + }.timeoutTo( + FiniteDuration(5, TimeUnit.SECONDS), + Task(sys.error(s"Expected deletion of $orphanClientClassesDir")) + ) + } + } catch { + case NonFatal(e) => + println(logger.render) + throw e } } } 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..06e0dddce4 --- /dev/null +++ b/shared/src/main/scala/bloop/logging/FlushingOutputStream.scala @@ -0,0 +1,17 @@ +package bloop.logging + +import java.io.ByteArrayOutputStream + +class FlushingOutputStream(write: String => Unit) extends ByteArrayOutputStream { + + override def flush(): Unit = { + // Write the contents of the internal byte array to the other OutputStream + 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 */