@@ -3,6 +3,7 @@ package boo.fox.haskelllsp
33import com.intellij.notification.NotificationGroupManager
44import com.intellij.notification.NotificationType
55import com.intellij.openapi.project.Project
6+ import com.intellij.openapi.roots.ProjectRootManager
67import com.intellij.openapi.util.SystemInfo
78import com.intellij.util.EnvironmentUtil
89import 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