Skip to content

Commit a5d47e6

Browse files
cursoragentrockofox
andcommitted
Add robust project working directory detection for Haskell LSP
Co-authored-by: kranich.felix <[email protected]>
1 parent efffb5d commit a5d47e6

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

src/main/kotlin/boo/fox/haskelllsp/HaskellLanguageServerFactory.kt

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package boo.fox.haskelllsp
33
import com.intellij.notification.NotificationGroupManager
44
import com.intellij.notification.NotificationType
55
import com.intellij.openapi.project.Project
6+
import com.intellij.openapi.roots.ProjectRootManager
67
import com.intellij.openapi.util.SystemInfo
78
import com.intellij.util.EnvironmentUtil
89
import com.redhat.devtools.lsp4ij.LanguageServerFactory
@@ -25,6 +26,38 @@ class HaskellLanguageServer(project: Project) : ProcessStreamConnectionProvider(
2526
Paths.get(path, HLS_EXECUTABLE_NAME).toFile().takeIf { it.canExecute() }
2627
}?.path
2728

29+
private fun getProjectWorkingDirectory(project: Project): String? {
30+
// Try multiple methods to get a valid project directory
31+
// 1. Try project.basePath (most common case)
32+
project.basePath?.let { path ->
33+
val dir = File(path)
34+
if (dir.exists() && dir.isDirectory) {
35+
return path
36+
}
37+
}
38+
39+
// 2. Try project.baseDir (VirtualFile-based)
40+
project.baseDir?.let { baseDir ->
41+
val path = baseDir.path
42+
val dir = File(path)
43+
if (dir.exists() && dir.isDirectory) {
44+
return path
45+
}
46+
}
47+
48+
// 3. Try content roots (for multi-module projects)
49+
ProjectRootManager.getInstance(project).contentRoots.firstOrNull()?.let { root ->
50+
val path = root.path
51+
val dir = File(path)
52+
if (dir.exists() && dir.isDirectory) {
53+
return path
54+
}
55+
}
56+
57+
// 4. If no valid directory found, return null (let system use default)
58+
return null
59+
}
60+
2861
init {
2962
val settings = boo.fox.haskelllsp.settings.HaskellLspSettings.getInstance()
3063
val configuredPath = settings.hlsPath.takeIf { it.isNotEmpty() }
@@ -35,7 +68,11 @@ class HaskellLanguageServer(project: Project) : ProcessStreamConnectionProvider(
3568

3669
if (!hlsPath.isNullOrEmpty()) {
3770
super.setCommands(listOf(hlsPath, "--lsp"))
38-
super.setWorkingDirectory(project.basePath)
71+
val workingDir = getProjectWorkingDirectory(project)
72+
if (workingDir != null) {
73+
super.setWorkingDirectory(workingDir)
74+
}
75+
// If workingDir is null, don't set it - let the system use the default
3976
} else {
4077
val message = if (configuredPath != null) {
4178
"Configured Haskell Language Server path is invalid or not executable. Please check the path in Settings | Tools | Haskell LSP."

0 commit comments

Comments
 (0)