Skip to content

Commit 37d0597

Browse files
committed
(#291) Debugger: use canonical paths for all files
1 parent c889015 commit 37d0597

File tree

3 files changed

+27
-26
lines changed

3 files changed

+27
-26
lines changed

src/main/kotlin/com/intellij/plugin/powershell/ide/debugger/PowerShellDebugSession.kt

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.eclipse.lsp4j.debug.*
2222
import org.eclipse.lsp4j.debug.services.IDebugProtocolServer
2323
import org.jetbrains.concurrency.await
2424
import java.nio.file.Path
25+
import kotlin.io.path.pathString
2526

2627
class PowerShellDebugSession(
2728
client: PSDebugClient, val server: IDebugProtocolServer,
@@ -50,21 +51,23 @@ class PowerShellDebugSession(
5051
}
5152

5253
fun setBreakpoint(filePath: Path, breakpoint: XLineBreakpoint<XBreakpointProperties<*>>) {
54+
val path = filePath.toRealPath()
5355
coroutineScope.launch(start = CoroutineStart.UNDISPATCHED) {
5456
breakpointsMapMutex.withLock {
55-
if (!breakpointMap.containsKey(filePath))
56-
breakpointMap[filePath] = mutableMapOf()
57-
val bpMap = breakpointMap[filePath]!!
57+
if (!breakpointMap.containsKey(path))
58+
breakpointMap[path] = mutableMapOf()
59+
val bpMap = breakpointMap[path]!!
5860
bpMap[breakpoint.line] = breakpoint
5961
sendBreakpointRequest()
6062
}
6163
}
6264
}
6365

6466
fun removeBreakpoint(filePath: Path, breakpoint: XLineBreakpoint<XBreakpointProperties<*>>) {
67+
val path = filePath.toRealPath()
6568
coroutineScope.launch(start = CoroutineStart.UNDISPATCHED) {
6669
breakpointsMapMutex.withLock {
67-
val bpMap = breakpointMap[filePath] ?: return@launch
70+
val bpMap = breakpointMap[path] ?: return@launch
6871
if (!bpMap.containsKey(breakpoint.line))
6972
return@launch
7073
bpMap.remove(breakpoint.line)
@@ -124,35 +127,35 @@ class PowerShellDebugSession(
124127
}
125128

126129
private suspend fun sendBreakpointRequest(breakpointMap: Map<Path, MutableMap<Int, XLineBreakpoint<XBreakpointProperties<*>>>>) {
127-
for (breakpointMapEntry in breakpointMap) {
130+
for ((file, breakpointsInFile) in breakpointMap) {
128131
val breakpointArgs = SetBreakpointsArguments()
129132
val source = Source()
130-
source.path = breakpointMapEntry.key.toString()
133+
source.path = file.pathString
131134
breakpointArgs.source = source
132135

133-
breakpointArgs.breakpoints = breakpointMapEntry.value.map {
134-
val bp = it.value
136+
breakpointArgs.breakpoints = breakpointsInFile.map {
137+
val breakpoint = it.value
135138
SourceBreakpoint().apply {
136-
line = bp.line + 1 // ide breakpoints line numbering starts from 0, while PSES start from 1
137-
condition = bp.conditionExpression?.expression
138-
logMessage = bp.logExpressionObject?.expression
139+
line = breakpoint.line + 1 // ide breakpoints line numbering starts from 0, while PSES start from 1
140+
condition = breakpoint.conditionExpression?.expression
141+
logMessage = breakpoint.logExpressionObject?.expression
139142
}
140143
}.toTypedArray()
141144
try {
142145
val setBreakpointsResponse = server.setBreakpoints(breakpointArgs).await()
143146
val responseMap = setBreakpointsResponse.breakpoints.associateBy { x -> x.line - 1 }
144-
breakpointMapEntry.value.forEach {
147+
breakpointsInFile.forEach {
145148
val bp = responseMap[it.value.line]
146149
if (bp?.isVerified == true) {
147150
session.setBreakpointVerified(it.value)
148-
logger.info("Set breakpoint at ${breakpointMapEntry.key}:${bp.line} successfully.")
151+
logger.info("Set breakpoint at $file:${bp.line} successfully.")
149152
} else {
150153
session.setBreakpointInvalid(
151154
it.value,
152155
bp?.message.nullize(nullizeSpaces = true)
153156
?: MessagesBundle.message("powershell.debugger.breakpoints.invalidBreakPoint")
154157
)
155-
logger.info("Invalid breakpoint at ${breakpointMapEntry.key}:${bp?.line}: ${bp?.message}")
158+
logger.info("Invalid breakpoint at $file:${bp?.line}: ${bp?.message}")
156159
}
157160
}
158161
} catch (e: Throwable) {

src/main/kotlin/com/intellij/plugin/powershell/ide/run/PowerShellProgramDebugRunner.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import com.intellij.openapi.diagnostic.logger
1616
import com.intellij.openapi.fileEditor.FileDocumentManager
1717
import com.intellij.openapi.project.Project
1818
import com.intellij.openapi.rd.util.toPromise
19-
import com.intellij.openapi.vfs.VfsUtil
2019
import com.intellij.plugin.powershell.ide.MessagesBundle
2120
import com.intellij.plugin.powershell.ide.PluginProjectRoot
2221
import com.intellij.plugin.powershell.ide.debugger.EditorServicesDebuggerHostStarter
@@ -43,6 +42,7 @@ import org.jetbrains.concurrency.Promise
4342
import java.nio.file.Path
4443
import java.util.concurrent.atomic.AtomicBoolean
4544
import kotlin.io.path.Path
45+
import kotlin.io.path.pathString
4646

4747
class PowerShellProgramDebugRunner : AsyncProgramRunner<RunnerSettings>() {
4848

@@ -158,15 +158,13 @@ private suspend fun initializeBreakpoints(
158158
) {
159159
if (!debugSession.areBreakpointsMuted()) {
160160
breakpoints.filter { it.sourcePosition != null && it.sourcePosition!!.file.exists() && it.sourcePosition!!.file.isValid && it.isEnabled }
161-
.groupBy { VfsUtil.virtualToIoFile(it.sourcePosition!!.file).toURI().toASCIIString() }
162-
.forEach { entry ->
163-
val fileURL = entry.key
161+
.groupBy { it.sourcePosition!!.file.toNioPath().toRealPath() }
162+
.forEach { (file, breakpoints) ->
164163
val breakpointArgs = SetBreakpointsArguments()
165164
val source = Source()
166-
source.path = fileURL
165+
source.path = file.pathString
167166
breakpointArgs.source = source
168-
val bps = entry.value
169-
breakpointArgs.breakpoints = bps.map {
167+
breakpointArgs.breakpoints = breakpoints.map {
170168
val bp = it
171169
SourceBreakpoint().apply {
172170
line = bp.line + 1 // ide breakpoints line numbering starts from 0, while PSES start from 1
@@ -185,12 +183,13 @@ private suspend fun launchDebuggee(scriptPath: Path, remoteProxy: IDebugProtocol
185183

186184
val launchArgs: MutableMap<String, Any> = HashMap()
187185
launchArgs["terminal"] = "none"
188-
launchArgs["script"] = scriptPath.toString()
186+
val scriptPathString = scriptPath.toRealPath().pathString
187+
launchArgs["script"] = scriptPathString
189188
launchArgs["noDebug"] = false
190189
launchArgs["__sessionId"] = "sessionId"
191190
launchArgs["Args"] = runtimeArgs
192191
launchArgs["Env"] = envs
193-
logger.info("Starting script file \"$scriptPath\" in a debug session.")
192+
logger.info("Starting script file \"$scriptPathString\" in a debug session.")
194193
remoteProxy.launch(launchArgs).await()
195194
}
196195

src/test/kotlin/com/intellij/plugin/powershell/debugger/BreakpointTest.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class BreakpointTest : DebuggerTestBase() {
6262
)
6363
val suspendContext = debugSession.suspendContext as PowerShellSuspendContext
6464
Assertions.assertEquals(
65-
psiSecondFile.virtualFile.toNioPath(),
65+
psiSecondFile.virtualFile.canonicalFile?.toNioPath(),
6666
suspendContext.activeExecutionStack.topFrame?.sourcePosition?.file?.toNioPath()
6767
)
6868
Assertions.assertEquals(line, suspendContext.activeExecutionStack.topFrame?.sourcePosition?.line)
@@ -71,8 +71,7 @@ class BreakpointTest : DebuggerTestBase() {
7171
}
7272

7373
@Test
74-
fun testConditionalBreakpoint()
75-
{
74+
fun testConditionalBreakpoint() {
7675
runInEdt {
7776
val psiFile = copyAndOpenFile("debugger/testBreakpoint.ps1")
7877
val file = psiFile.virtualFile

0 commit comments

Comments
 (0)