FIR: do not call toRealPath on non-existing files

Checking if files exist should have happened earlier when parsing CLI
arguments. For some arguments, such as `-Xfriend-paths`, compiler
performs no checking. So it's incorrect to call `toRealPath()` on such
paths because it throws exception if the file does not exist.

 #KT-70991 Fixed
diff --git a/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/AbstractFirDeserializedSymbolProvider.kt b/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/AbstractFirDeserializedSymbolProvider.kt
index a543419..358bfaa 100644
--- a/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/AbstractFirDeserializedSymbolProvider.kt
+++ b/compiler/fir/fir-deserialization/src/org/jetbrains/kotlin/fir/deserialization/AbstractFirDeserializedSymbolProvider.kt
@@ -30,6 +30,7 @@
 import org.jetbrains.kotlin.serialization.deserialization.getName
 import org.jetbrains.kotlin.utils.mapToSetOrEmpty
 import java.nio.file.Path
+import kotlin.io.path.exists
 
 class PackagePartsCacheData(
     val proto: ProtoBuf.Package,
@@ -78,7 +79,7 @@
             return libs.any {
                 when {
                     it.isAbsolute && !isPathAbsolute -> realPath.startsWith(it)
-                    !it.isAbsolute && isPathAbsolute -> path.startsWith(it.toRealPath())
+                    !it.isAbsolute && isPathAbsolute && it.exists() -> path.startsWith(it.toRealPath())
                     else -> path.startsWith(it)
                 }
             }
diff --git a/compiler/tests-integration/tests/org/jetbrains/kotlin/cli/FriendPathsTest.kt b/compiler/tests-integration/tests/org/jetbrains/kotlin/cli/FriendPathsTest.kt
index 433b7f9..027897fe 100644
--- a/compiler/tests-integration/tests/org/jetbrains/kotlin/cli/FriendPathsTest.kt
+++ b/compiler/tests-integration/tests/org/jetbrains/kotlin/cli/FriendPathsTest.kt
@@ -16,8 +16,8 @@
 
 package org.jetbrains.kotlin.cli
 
-import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
 import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
+import org.jetbrains.kotlin.codegen.forTestCompile.ForTestCompileRuntime
 import org.jetbrains.kotlin.test.CompilerTestUtil
 import org.jetbrains.kotlin.test.TestCaseWithTmpdir
 import java.io.File
@@ -43,9 +43,26 @@
         doTestFriendPaths(File(tmpdir, "lib").relativeTo(File("").absoluteFile))
     }
 
-    private fun doTestFriendPaths(libDest: File, messageRenderer: MessageRenderer? = null) {
+    // This is a regression test for KT-70991.
+    // It's not a generated CLI test because the issue only reproduced when any path in the classpath is absolute, while CliTestGenerated
+    // passes relative paths of libraries to the classpath.
+    fun testNonExistingPath() {
         val libSrc = File(getTestDataDirectory(), "lib.kt")
-        CompilerTestUtil.executeCompilerAssertSuccessful(K2JVMCompiler(), listOf("-d", libDest.path, libSrc.path), messageRenderer)
+        CompilerTestUtil.executeCompilerAssertSuccessful(
+            K2JVMCompiler(),
+            listOf(
+                libSrc.path,
+                "-d", File(tmpdir, "output").path,
+                "-Xfriend-paths=non-existing-path",
+                "-no-stdlib",
+                "-cp", ForTestCompileRuntime.runtimeJarForTests().absolutePath,
+            ),
+        )
+    }
+
+    private fun doTestFriendPaths(libDest: File) {
+        val libSrc = File(getTestDataDirectory(), "lib.kt")
+        CompilerTestUtil.executeCompilerAssertSuccessful(K2JVMCompiler(), listOf("-d", libDest.path, libSrc.path))
 
         CompilerTestUtil.executeCompilerAssertSuccessful(
             K2JVMCompiler(),
@@ -53,7 +70,6 @@
                 "-d", tmpdir.path, "-cp", libDest.path, File(getTestDataDirectory(), "usage.kt").path,
                 "-Xfriend-paths=${libDest.path}"
             ),
-            messageRenderer,
         )
     }
 }