Skip to content

Commit 941a3ae

Browse files
author
Oron Port
committed
prevent running windows programs from wsl
1 parent 5dc0048 commit 941a3ae

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

internals/src/main/scala/dfhdl/internals/helpers.scala

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ extension (using quotes: Quotes)(lhs: quotes.reflect.TypeRepr)
8686
case '[head *: tail] =>
8787
recur(TypeRepr.of[tail], TypeRepr.of[head] :: args)
8888
case '[EmptyTuple] => args.reverse
89-
case '[t] =>
89+
case '[t] =>
9090
report.errorAndAbort(s"Expecting a tuple, but found ${Type.show[t]}")
9191
if (lhs.show == "null")
9292
report.errorAndAbort(s"Expecting a tuple, but found null")
@@ -332,7 +332,7 @@ extension [T](seq: Iterable[T])
332332
f: T => P,
333333
res: List[(P, Iterable[T])]
334334
): Seq[(P, Iterable[T])] = seq.headOption match
335-
case None => res.reverse
335+
case None => res.reverse
336336
case Some(h) =>
337337
val key = f(h)
338338
val subseq = seq.takeWhile(f(_) equals key)
@@ -460,7 +460,32 @@ object AnnotatedWith:
460460
end annotWithMacro
461461
end AnnotatedWith
462462

463-
def osIsWindows: Boolean = sys.props("os.name").toLowerCase.contains("windows")
463+
lazy val osIsWindows: Boolean = sys.props("os.name").toLowerCase.contains("windows")
464+
lazy val osIsLinux: Boolean = sys.props("os.name").toLowerCase.contains("linux")
465+
lazy val osIsWSL: Boolean =
466+
if (osIsLinux)
467+
import scala.io.Source
468+
try
469+
val procVersion = Source.fromFile("/proc/version").getLines().mkString
470+
procVersion.contains("microsoft-standard")
471+
catch
472+
case _: Exception => false
473+
else false
474+
// checks if the program is accessible to the current shell
475+
def programIsAccessible(cmd: String): Boolean =
476+
import sys.process.*
477+
try
478+
if (osIsWindows)
479+
s"where $cmd".!!.nonEmpty
480+
else
481+
val result = s"which $cmd".!!.trim()
482+
// reject windows programs running from WSL
483+
if (result.nonEmpty && osIsWSL)
484+
!result.matches("""/mnt/[a-zA-Z]/.*""")
485+
else
486+
result.nonEmpty
487+
catch
488+
case _: Exception => false
464489

465490
// trait CompiletimeErrorPos[M <: String, S <: Int, E <: Int]
466491
// object CompiletimeErrorPos:

lib/src/main/scala/dfhdl/tools/toolsCore/Tool.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@ trait Tool:
3131
protected[dfhdl] def cleanUpBeforeFileRestore()(using MemberGetSet, CompilerOptions): Unit = {}
3232

3333
private[dfhdl] lazy val installedVersion: Option[String] =
34-
val getVersionFullCmd =
35-
Process(s"$runExec $versionCmd", new java.io.File(System.getProperty("java.io.tmpdir")))
36-
try extractVersion(getVersionFullCmd.!!)
37-
catch case e: Exception => None
34+
if (!programIsAccessible(runExec)) None
35+
else
36+
val getVersionFullCmd =
37+
Process(s"$runExec $versionCmd", new java.io.File(System.getProperty("java.io.tmpdir")))
38+
try extractVersion(getVersionFullCmd.!!)
39+
catch case e: Exception => None
3840
final def isAvailable: Boolean = installedVersion.nonEmpty
3941
protected def getInstalledVersion(using to: ToolOptions): String =
4042
preCheck()

0 commit comments

Comments
 (0)