From 3bde413da72139f4cca2dff323d9f173cb36bfe0 Mon Sep 17 00:00:00 2001 From: Hubert Plociniczak Date: Fri, 10 Jan 2025 13:43:09 +0100 Subject: [PATCH] Better detection for Enso's NI when launching LS Previosly we were wrongly relying on the presence of the file. That way, a bash script meant that NI integration was wrongly used. This change uses Tika, but it has already been present in other subprojects so no additional dependency shall be added. Follow up on #11880. --- build.sbt | 2 ++ .../runner/NativeExecCommand.scala | 22 +++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/build.sbt b/build.sbt index 9552eef688ae..09bceca9754e 100644 --- a/build.sbt +++ b/build.sbt @@ -4495,10 +4495,12 @@ lazy val `runtime-version-manager` = project libraryDependencies ++= Seq( "com.typesafe.scala-logging" %% "scala-logging" % scalaLoggingVersion, "org.apache.commons" % "commons-compress" % commonsCompressVersion, + "org.apache.tika" % "tika-core" % tikaVersion, "org.scalatest" %% "scalatest" % scalatestVersion % Test ), Compile / moduleDependencies ++= Seq( "org.apache.commons" % "commons-compress" % commonsCompressVersion, + "org.apache.tika" % "tika-core" % tikaVersion, "org.slf4j" % "slf4j-api" % slf4jVersion ), Compile / internalModuleDependencies := Seq( diff --git a/lib/scala/runtime-version-manager/src/main/scala/org/enso/runtimeversionmanager/runner/NativeExecCommand.scala b/lib/scala/runtime-version-manager/src/main/scala/org/enso/runtimeversionmanager/runner/NativeExecCommand.scala index 33beca0aa1c3..6cac2a90025b 100644 --- a/lib/scala/runtime-version-manager/src/main/scala/org/enso/runtimeversionmanager/runner/NativeExecCommand.scala +++ b/lib/scala/runtime-version-manager/src/main/scala/org/enso/runtimeversionmanager/runner/NativeExecCommand.scala @@ -4,6 +4,9 @@ import org.enso.cli.OS import org.enso.distribution.{DistributionManager, Environment} import org.enso.runtimeversionmanager.components.Engine +import org.apache.tika.config.TikaConfig +import org.apache.tika.Tika + import java.nio.file.Path case class NativeExecCommand(executablePath: Path) extends ExecCommand { @@ -26,7 +29,22 @@ object NativeExecCommand { val fullExecPath = dm.paths.engines.resolve(version).resolve("bin").resolve(execName) - if (fullExecPath.toFile.exists()) Some(NativeExecCommand(fullExecPath)) - else None + if (fullExecPath.toFile.exists() && isBinary(fullExecPath)) { + Some(NativeExecCommand(fullExecPath)) + } else None + } + + private def isBinary(path: Path): Boolean = { + try { + val config = TikaConfig.getDefaultConfig() + val tika = new Tika(config) + val mimeTypes = config.getMimeRepository + val mime = tika.detect(path); + val tpe = mimeTypes.forName(mime).getType.getType + tpe != null && tpe == "application" + } catch { + case _: Throwable => + false + } } }