Skip to content

Commit cf7e903

Browse files
committed
Add robust project working directory detection for Haskell LSP
1 parent efffb5d commit cf7e903

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

.run/Run IDE with Plugin.run.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="Run Plugin" type="GradleRunConfiguration" factoryName="Gradle">
3+
<log_file alias="idea.log" path="$PROJECT_DIR$/build/idea-sandbox/system/log/idea.log"/>
4+
<ExternalSystemSettings>
5+
<option name="executionName"/>
6+
<option name="externalProjectPath" value="$PROJECT_DIR$"/>
7+
<option name="externalSystemIdString" value="GRADLE"/>
8+
<option name="scriptParameters" value=""/>
9+
<option name="taskDescriptions">
10+
<list/>
11+
</option>
12+
<option name="taskNames">
13+
<list>
14+
<option value="runIde"/>
15+
</list>
16+
</option>
17+
<option name="vmOptions" value=""/>
18+
</ExternalSystemSettings>
19+
<ExternalSystemDebugServerProcess>true</ExternalSystemDebugServerProcess>
20+
<ExternalSystemReattachDebugProcess>true</ExternalSystemReattachDebugProcess>
21+
<DebugAllEnabled>false</DebugAllEnabled>
22+
<method v="2"/>
23+
</configuration>
24+
</component>

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)