Skip to content

Commit 54aa1a6

Browse files
committed
fix(compiler): add timeout and error handling for file lookup
- Add a 10-second timeout to `currentTask.get()` in `ProjectFileUtil.kt` to prevent indefinite blocking. - Wrap `findFile` in a try-catch block in `FileInsCommand.kt` to handle exceptions gracefully and return a user-friendly error message.
1 parent 6ce29ba commit 54aa1a6

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/compiler/exec/FileInsCommand.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@ class FileInsCommand(private val myProject: Project, private val prop: String) :
3232

3333
if (virtualFile == null) {
3434
val filename = filepath.split("/").last()
35-
virtualFile = myProject.findFile(filename, false)
35+
try {
36+
virtualFile = myProject.findFile(filename, false)
37+
} catch (e: Exception) {
38+
return "File not found: $prop"
39+
}
3640
}
3741

3842
val content = virtualFile?.readText()
3943
if (content == null) {
4044
AutoDevNotifications.warn(myProject, "File not found: $prop")
41-
/// not show error message to just notify
4245
return "File not found: $prop"
4346
}
4447

exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/utils/ProjectFileUtil.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.intellij.openapi.vfs.VirtualFile
99
import com.intellij.openapi.vfs.VirtualFileManager
1010
import com.intellij.psi.search.FilenameIndex
1111
import com.intellij.psi.search.ProjectScope
12+
import java.util.concurrent.TimeUnit
1213

1314
fun Project.lookupFile(path: String): VirtualFile? {
1415
val projectPath = this.guessProjectDir()?.toNioPath()
@@ -25,7 +26,7 @@ fun Project.findFile(filename: String, caseSensitively: Boolean = true): Virtual
2526
return@executeOnPooledThread searchedFiles.firstOrNull()
2627
}
2728

28-
return currentTask.get()
29+
return currentTask.get(10, TimeUnit.SECONDS)
2930
}
3031

3132
// getVirtualFilesByNamesIgnoringCase

0 commit comments

Comments
 (0)