Skip to content

Commit 649a4c1

Browse files
committed
Changes from review
1 parent ba8d36c commit 649a4c1

File tree

3 files changed

+62
-64
lines changed

3 files changed

+62
-64
lines changed

src/PowerShellEditorServices/Services/DebugAdapter/DebugService.cs

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ internal class DebugService
4848
private VariableContainerDetails scriptScopeVariables;
4949
private VariableContainerDetails localScopeVariables;
5050
private StackFrameDetails[] stackFrameDetails;
51-
private PathMapping[] _pathMappings;
5251

5352
private readonly SemaphoreSlim debugInfoHandle = AsyncUtils.CreateSimpleLockingSemaphore();
5453
#endregion
@@ -87,6 +86,11 @@ public bool IsDebuggingRemoteRunspace
8786
set => _debugContext.IsDebuggingRemoteRunspace = value;
8887
}
8988

89+
/// <summary>
90+
/// Gets or sets an array of path mappings for the current debug session.
91+
/// </summary>
92+
public PathMapping[] PathMappings { get; set; } = [];
93+
9094
#endregion
9195

9296
#region Constructors
@@ -603,27 +607,20 @@ public VariableScope[] GetVariableScopes(int stackFrameId)
603607
};
604608
}
605609

606-
internal void SetPathMappings(PathMapping[] pathMappings) => _pathMappings = pathMappings;
607-
608-
internal void UnsetPathMappings() => _pathMappings = null;
609-
610610
internal bool TryGetMappedLocalPath(string remotePath, out string localPath)
611611
{
612-
if (_pathMappings is not null)
612+
foreach (PathMapping mapping in PathMappings)
613613
{
614-
foreach (PathMapping mapping in _pathMappings)
614+
if (string.IsNullOrWhiteSpace(mapping.LocalRoot) || string.IsNullOrWhiteSpace(mapping.RemoteRoot))
615615
{
616-
if (string.IsNullOrWhiteSpace(mapping.LocalRoot) || string.IsNullOrWhiteSpace(mapping.RemoteRoot))
617-
{
618-
// If either path mapping is null, we can't map the path.
619-
continue;
620-
}
616+
// If either path mapping is null, we can't map the path.
617+
continue;
618+
}
621619

622-
if (remotePath.StartsWith(mapping.RemoteRoot, StringComparison.OrdinalIgnoreCase))
623-
{
624-
localPath = mapping.LocalRoot + remotePath.Substring(mapping.RemoteRoot.Length);
625-
return true;
626-
}
620+
if (remotePath.StartsWith(mapping.RemoteRoot, StringComparison.OrdinalIgnoreCase))
621+
{
622+
localPath = mapping.LocalRoot + remotePath.Substring(mapping.RemoteRoot.Length);
623+
return true;
627624
}
628625
}
629626

@@ -633,22 +630,19 @@ internal bool TryGetMappedLocalPath(string remotePath, out string localPath)
633630

634631
internal bool TryGetMappedRemotePath(string localPath, out string remotePath)
635632
{
636-
if (_pathMappings is not null)
633+
foreach (PathMapping mapping in PathMappings)
637634
{
638-
foreach (PathMapping mapping in _pathMappings)
635+
if (string.IsNullOrWhiteSpace(mapping.LocalRoot) || string.IsNullOrWhiteSpace(mapping.RemoteRoot))
639636
{
640-
if (string.IsNullOrWhiteSpace(mapping.LocalRoot) || string.IsNullOrWhiteSpace(mapping.RemoteRoot))
641-
{
642-
// If either path mapping is null, we can't map the path.
643-
continue;
644-
}
637+
// If either path mapping is null, we can't map the path.
638+
continue;
639+
}
645640

646-
if (localPath.StartsWith(mapping.LocalRoot, StringComparison.OrdinalIgnoreCase))
647-
{
648-
// If the local path starts with the local path mapping, we can replace it with the remote path.
649-
remotePath = mapping.RemoteRoot + localPath.Substring(mapping.LocalRoot.Length);
650-
return true;
651-
}
641+
if (localPath.StartsWith(mapping.LocalRoot, StringComparison.OrdinalIgnoreCase))
642+
{
643+
// If the local path starts with the local path mapping, we can replace it with the remote path.
644+
remotePath = mapping.RemoteRoot + localPath.Substring(mapping.LocalRoot.Length);
645+
return true;
652646
}
653647
}
654648

src/PowerShellEditorServices/Services/DebugAdapter/Handlers/DisconnectHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public async Task<DisconnectResponse> Handle(DisconnectArguments request, Cancel
5050
// We should instead ensure that the debugger is in some valid state, lock it and then tear things down
5151

5252
_debugEventHandlerService.UnregisterEventHandlers();
53-
_debugService.UnsetPathMappings();
53+
_debugService.PathMappings = [];
5454

5555
if (!_debugStateService.ExecutionCompleted)
5656
{

src/PowerShellEditorServices/Services/DebugAdapter/Handlers/LaunchAndAttachHandler.cs

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,14 @@ public LaunchAndAttachHandler(
140140

141141
public async Task<LaunchResponse> Handle(PsesLaunchRequestArguments request, CancellationToken cancellationToken)
142142
{
143-
_debugService.SetPathMappings(request.PathMappings);
143+
_debugService.PathMappings = request.PathMappings;
144144
try
145145
{
146146
return await HandleImpl(request, cancellationToken).ConfigureAwait(false);
147147
}
148148
catch
149149
{
150-
_debugService.UnsetPathMappings();
150+
_debugService.PathMappings = [];
151151
throw;
152152
}
153153
}
@@ -230,38 +230,11 @@ await _executionService.ExecutePSCommandAsync(
230230

231231
// Store the launch parameters so that they can be used later
232232
_debugStateService.NoDebug = request.NoDebug;
233-
_debugStateService.ScriptToLaunch = request.Script;
233+
_debugStateService.ScriptToLaunch = GetLaunchScript(request);
234234
_debugStateService.Arguments = request.Args;
235235
_debugStateService.IsUsingTempIntegratedConsole = request.CreateTemporaryIntegratedConsole;
236236
_debugStateService.ExecuteMode = request.ExecuteMode;
237237

238-
if (request.CreateTemporaryIntegratedConsole
239-
&& !string.IsNullOrEmpty(request.Script)
240-
&& ScriptFile.IsUntitledPath(request.Script))
241-
{
242-
throw new RpcErrorException(0, null, "Running an Untitled file in a temporary Extension Terminal is currently not supported!");
243-
}
244-
245-
// If the current session is remote, map the script path to the remote
246-
// machine if necessary
247-
if (_debugStateService.ScriptToLaunch != null
248-
&& _runspaceContext.CurrentRunspace.IsOnRemoteMachine)
249-
{
250-
if (_debugService.TryGetMappedRemotePath(_debugStateService.ScriptToLaunch, out string remoteMappedPath))
251-
{
252-
_debugStateService.ScriptToLaunch = remoteMappedPath;
253-
}
254-
else
255-
{
256-
// If the script is not mapped, we will map it to the remote path
257-
// using the RemoteFileManagerService.
258-
_debugStateService.ScriptToLaunch =
259-
_remoteFileManagerService.GetMappedPath(
260-
_debugStateService.ScriptToLaunch,
261-
_runspaceContext.CurrentRunspace);
262-
}
263-
}
264-
265238
// If no script is being launched, mark this as an interactive
266239
// debugging session
267240
_debugStateService.IsInteractiveDebugSession = string.IsNullOrEmpty(_debugStateService.ScriptToLaunch);
@@ -284,13 +257,13 @@ public async Task<AttachResponse> Handle(PsesAttachRequestArguments request, Can
284257
_debugService.IsDebuggingRemoteRunspace = true;
285258
try
286259
{
287-
_debugService.SetPathMappings(request.PathMappings);
260+
_debugService.PathMappings = request.PathMappings;
288261
return await HandleImpl(request, cancellationToken).ConfigureAwait(false);
289262
}
290263
catch
291264
{
292265
_debugService.IsDebuggingRemoteRunspace = false;
293-
_debugService.UnsetPathMappings();
266+
_debugService.PathMappings = [];
294267
throw;
295268
}
296269
}
@@ -522,7 +495,7 @@ private async Task OnExecutionCompletedAsync(Task executeTask)
522495
_debugEventHandlerService.UnregisterEventHandlers();
523496

524497
_debugService.IsDebuggingRemoteRunspace = false;
525-
_debugService.UnsetPathMappings();
498+
_debugService.PathMappings = [];
526499

527500
if (!isRunspaceClosed && _debugStateService.IsAttachSession)
528501
{
@@ -552,5 +525,36 @@ await _executionService.ExecutePSCommandAsync(
552525
_debugService.IsClientAttached = false;
553526
_debugAdapterServer.SendNotification(EventNames.Terminated);
554527
}
528+
529+
private string GetLaunchScript(PsesLaunchRequestArguments request)
530+
{
531+
string scriptToLaunch = request.Script;
532+
if (request.CreateTemporaryIntegratedConsole
533+
&& !string.IsNullOrEmpty(scriptToLaunch)
534+
&& ScriptFile.IsUntitledPath(scriptToLaunch))
535+
{
536+
throw new RpcErrorException(0, null, "Running an Untitled file in a temporary Extension Terminal is currently not supported!");
537+
}
538+
539+
// If the current session is remote, map the script path to the remote
540+
// machine if necessary
541+
if (scriptToLaunch is not null && _runspaceContext.CurrentRunspace.IsOnRemoteMachine)
542+
{
543+
if (_debugService.TryGetMappedRemotePath(scriptToLaunch, out string remoteMappedPath))
544+
{
545+
scriptToLaunch = remoteMappedPath;
546+
}
547+
else
548+
{
549+
// If the script is not mapped, we will map it to the remote path
550+
// using the RemoteFileManagerService.
551+
scriptToLaunch = _remoteFileManagerService.GetMappedPath(
552+
scriptToLaunch,
553+
_runspaceContext.CurrentRunspace);
554+
}
555+
}
556+
557+
return scriptToLaunch;
558+
}
555559
}
556560
}

0 commit comments

Comments
 (0)