Repository: joernio/joern (scala)

Issue:
`importCode` does not warn about inexistent dir
the error message is the following:
```
joern> importCode("/no/such/directory") 
io.joern.console.ConsoleException: No suitable CPG generator found for: /no/such/directory
```
it should be something like _directory does not exist_

Pull request:
Fixes: https://github.com/joernio/joern/issues/1607

Patch:
diff --git a/console/src/main/scala/io/joern/console/cpgcreation/ImportCode.scala b/console/src/main/scala/io/joern/console/cpgcreation/ImportCode.scala
index b9d75d4b5403..7c1e8b16aaa5 100644
--- a/console/src/main/scala/io/joern/console/cpgcreation/ImportCode.scala
+++ b/console/src/main/scala/io/joern/console/cpgcreation/ImportCode.scala
@@ -17,6 +17,12 @@ class ImportCode[T <: Project](console: io.joern.console.Console[T]) {
   private val workspace          = console.workspace
   protected val generatorFactory = new CpgGeneratorFactory(config)
 
+  private def checkInputPath(inputPath: String): Unit = {
+    if (!File(inputPath).exists) {
+      throw new ConsoleException(s"Input path does not exist: '$inputPath'")
+    }
+  }
+
   /** This is the `importCode(...)` method exposed on the console. It attempts to find a suitable CPG generator first by
     * looking at the `language` parameter and if no generator is found for the language, looking the contents at
     * `inputPath` to determine heuristically which generator to use.
@@ -27,6 +33,7 @@ class ImportCode[T <: Project](console: io.joern.console.Console[T]) {
     namespaces: List[String] = List(),
     language: String = ""
   ): Cpg = {
+    checkInputPath(inputPath)
     if (language != "") {
       generatorFactory.forLanguage(language) match {
         case None           => throw new ConsoleException(s"No CPG generator exists for language: $language")
@@ -125,6 +132,8 @@ class ImportCode[T <: Project](console: io.joern.console.Console[T]) {
   }
 
   private def apply(frontend: CpgGenerator, inputPath: String, projectName: String, namespaces: List[String]): Cpg = {
+    checkInputPath(inputPath)
+
     val name = Option(projectName).filter(_.nonEmpty).getOrElse(deriveNameFromInputPath(inputPath, workspace))
     report(s"Creating project `$name` for code at `$inputPath`")
 


Test patch:
diff --git a/console/src/test/scala/io/joern/console/ConsoleTests.scala b/console/src/test/scala/io/joern/console/ConsoleTests.scala
index 459a7d90f71a..84111d71640f 100644
--- a/console/src/test/scala/io/joern/console/ConsoleTests.scala
+++ b/console/src/test/scala/io/joern/console/ConsoleTests.scala
@@ -24,6 +24,22 @@ class ConsoleTests extends AnyWordSpec with Matchers {
       )
 
   "importCode" should {
+    "warn about non-existent dir" in ConsoleFixture() { (console, _) =>
+      val nonExistentDir = "/does/not/exist/"
+      the[ConsoleException] thrownBy console.importCode(
+        nonExistentDir
+      ) should have message s"Input path does not exist: '$nonExistentDir'"
+      the[ConsoleException] thrownBy console.importCode.c(
+        nonExistentDir
+      ) should have message s"Input path does not exist: '$nonExistentDir'"
+      the[ConsoleException] thrownBy console.importCode.jssrc(
+        nonExistentDir
+      ) should have message s"Input path does not exist: '$nonExistentDir'"
+      the[ConsoleException] thrownBy console.importCode.java(
+        nonExistentDir
+      ) should have message s"Input path does not exist: '$nonExistentDir'"
+    }
+
     "provide overview of available language modules" in ConsoleFixture() { (console, _) =>
       console.importCode.toString.contains("| C") shouldBe true
     }