@@ -25,7 +25,7 @@ namespace Microsoft.PowerShell.EditorServices.Engine.Services
2525 /// Provides a high-level service for interacting with the
2626 /// PowerShell debugger in the runspace managed by a PowerShellContext.
2727 /// </summary>
28- public class DebugService
28+ internal class DebugService
2929 {
3030 #region Fields
3131
@@ -34,14 +34,14 @@ public class DebugService
3434
3535 private readonly ILogger logger ;
3636 private readonly PowerShellContextService powerShellContext ;
37- // private RemoteFileManagerService remoteFileManager;
37+ private RemoteFileManagerService remoteFileManager ;
3838
3939 // TODO: This needs to be managed per nested session
4040 private readonly Dictionary < string , List < Breakpoint > > breakpointsPerFile =
4141 new Dictionary < string , List < Breakpoint > > ( ) ;
4242
4343 private int nextVariableId ;
44- private readonly string temporaryScriptListingPath ;
44+ private string temporaryScriptListingPath ;
4545 private List < VariableDetailsBase > variables ;
4646 private VariableContainerDetails globalScopeVariables ;
4747 private VariableContainerDetails scriptScopeVariables ;
@@ -98,12 +98,12 @@ public class DebugService
9898 /// The PowerShellContext to use for all debugging operations.
9999 /// </param>
100100 //// <param name = "remoteFileManager" >
101- //// A RemoteFileManager instance to use for accessing files in remote sessions.
101+ //// A RemoteFileManagerService instance to use for accessing files in remote sessions.
102102 //// </param>
103103 /// <param name="logger">An ILogger implementation used for writing log messages.</param>
104104 public DebugService (
105105 PowerShellContextService powerShellContext ,
106- //RemoteFileManager remoteFileManager,
106+ RemoteFileManagerService remoteFileManager ,
107107 ILoggerFactory factory )
108108 {
109109 Validate . IsNotNull ( nameof ( powerShellContext ) , powerShellContext ) ;
@@ -115,7 +115,7 @@ public DebugService(
115115
116116 this . powerShellContext . BreakpointUpdated += this . OnBreakpointUpdated ;
117117
118- // this.remoteFileManager = remoteFileManager;
118+ this . remoteFileManager = remoteFileManager ;
119119
120120 this . invocationTypeScriptPositionProperty =
121121 typeof ( InvocationInfo )
@@ -148,16 +148,13 @@ public async Task<BreakpointDetails[]> SetLineBreakpointsAsync(
148148 . GetCapability < DscBreakpointCapability > ( ) ;
149149
150150 string scriptPath = scriptFile . FilePath ;
151- //TODO: BRING THIS BACK
152151 // Make sure we're using the remote script path
153- /*
154152 if ( this . powerShellContext . CurrentRunspace . Location == RunspaceLocation . Remote &&
155153 this . remoteFileManager != null )
156154 {
157155 if ( ! this . remoteFileManager . IsUnderRemoteTempPath ( scriptPath ) )
158156 {
159- this.logger.Write(
160- LogLevel.Verbose,
157+ this . logger . LogTrace (
161158 $ "Could not set breakpoints for local path '{ scriptPath } ' in a remote session.") ;
162159
163160 return resultBreakpointDetails . ToArray ( ) ;
@@ -170,8 +167,7 @@ public async Task<BreakpointDetails[]> SetLineBreakpointsAsync(
170167
171168 scriptPath = mappedPath ;
172169 }
173- else */
174- if (
170+ else if (
175171 this . temporaryScriptListingPath != null &&
176172 this . temporaryScriptListingPath . Equals ( scriptPath , StringComparison . CurrentCultureIgnoreCase ) )
177173 {
@@ -974,16 +970,15 @@ private async Task FetchStackFramesAsync(string scriptNameOverride)
974970 {
975971 this . stackFrameDetails [ i ] . ScriptPath = scriptNameOverride ;
976972 }
977- // TODO: BRING THIS BACK
978- //else if (this.powerShellContext.CurrentRunspace.Location == RunspaceLocation.Remote &&
979- // this.remoteFileManager != null &&
980- // !string.Equals(stackFrameScriptPath, StackFrameDetails.NoFileScriptPath))
981- //{
982- // this.stackFrameDetails[i].ScriptPath =
983- // this.remoteFileManager.GetMappedPath(
984- // stackFrameScriptPath,
985- // this.powerShellContext.CurrentRunspace);
986- //}
973+ else if ( this . powerShellContext . CurrentRunspace . Location == RunspaceLocation . Remote &&
974+ this . remoteFileManager != null &&
975+ ! string . Equals ( stackFrameScriptPath , StackFrameDetails . NoFileScriptPath ) )
976+ {
977+ this . stackFrameDetails [ i ] . ScriptPath =
978+ this . remoteFileManager . GetMappedPath (
979+ stackFrameScriptPath ,
980+ this . powerShellContext . CurrentRunspace ) ;
981+ }
987982 }
988983 }
989984
@@ -1201,63 +1196,61 @@ private async void OnDebuggerStopAsync(object sender, DebuggerStopEventArgs e)
12011196 bool noScriptName = false ;
12021197 string localScriptPath = e . InvocationInfo . ScriptName ;
12031198
1204- // TODO: BRING THIS BACK
12051199 // If there's no ScriptName, get the "list" of the current source
1206- // if (this.remoteFileManager != null && string.IsNullOrEmpty(localScriptPath))
1207- // {
1208- // // Get the current script listing and create the buffer
1209- // PSCommand command = new PSCommand();
1210- // command.AddScript($"list 1 {int.MaxValue}");
1211-
1212- // IEnumerable<PSObject> scriptListingLines =
1213- // await this.powerShellContext.ExecuteCommandAsync<PSObject>(
1214- // command, false, false);
1215-
1216- // if (scriptListingLines != null)
1217- // {
1218- // int linePrefixLength = 0;
1219-
1220- // string scriptListing =
1221- // string.Join(
1222- // Environment.NewLine,
1223- // scriptListingLines
1224- // .Select(o => this.TrimScriptListingLine(o, ref linePrefixLength))
1225- // .Where(s => s != null));
1226-
1227- // this.temporaryScriptListingPath =
1228- // this.remoteFileManager.CreateTemporaryFile(
1229- // $"[{this.powerShellContext.CurrentRunspace.SessionDetails.ComputerName}] {TemporaryScriptFileName}",
1230- // scriptListing,
1231- // this.powerShellContext.CurrentRunspace);
1232-
1233- // localScriptPath =
1234- // this.temporaryScriptListingPath
1235- // ?? StackFrameDetails.NoFileScriptPath;
1236-
1237- // noScriptName = localScriptPath != null;
1238- // }
1239- // else
1240- // {
1241- // this.logger.LogWarning($"Could not load script context");
1242- // }
1243- // }
1200+ if ( this . remoteFileManager != null && string . IsNullOrEmpty ( localScriptPath ) )
1201+ {
1202+ // Get the current script listing and create the buffer
1203+ PSCommand command = new PSCommand ( ) ;
1204+ command . AddScript ( $ "list 1 { int . MaxValue } ") ;
1205+
1206+ IEnumerable < PSObject > scriptListingLines =
1207+ await this . powerShellContext . ExecuteCommandAsync < PSObject > (
1208+ command , false , false ) ;
1209+
1210+ if ( scriptListingLines != null )
1211+ {
1212+ int linePrefixLength = 0 ;
1213+
1214+ string scriptListing =
1215+ string . Join (
1216+ Environment . NewLine ,
1217+ scriptListingLines
1218+ . Select ( o => this . TrimScriptListingLine ( o , ref linePrefixLength ) )
1219+ . Where ( s => s != null ) ) ;
1220+
1221+ this . temporaryScriptListingPath =
1222+ this . remoteFileManager . CreateTemporaryFile (
1223+ $ "[{ this . powerShellContext . CurrentRunspace . SessionDetails . ComputerName } ] { TemporaryScriptFileName } ",
1224+ scriptListing ,
1225+ this . powerShellContext . CurrentRunspace ) ;
1226+
1227+ localScriptPath =
1228+ this . temporaryScriptListingPath
1229+ ?? StackFrameDetails . NoFileScriptPath ;
1230+
1231+ noScriptName = localScriptPath != null ;
1232+ }
1233+ else
1234+ {
1235+ this . logger . LogWarning ( $ "Could not load script context") ;
1236+ }
1237+ }
12441238
12451239 // Get call stack and variables.
12461240 await this . FetchStackFramesAndVariablesAsync (
12471241 noScriptName ? localScriptPath : null ) ;
12481242
1249- // TODO: BRING THIS BACK
12501243 // If this is a remote connection and the debugger stopped at a line
12511244 // in a script file, get the file contents
1252- // if (this.powerShellContext.CurrentRunspace.Location == RunspaceLocation.Remote &&
1253- // this.remoteFileManager != null &&
1254- // !noScriptName)
1255- // {
1256- // localScriptPath =
1257- // await this.remoteFileManager.FetchRemoteFileAsync(
1258- // e.InvocationInfo.ScriptName,
1259- // this.powerShellContext.CurrentRunspace);
1260- // }
1245+ if ( this . powerShellContext . CurrentRunspace . Location == RunspaceLocation . Remote &&
1246+ this . remoteFileManager != null &&
1247+ ! noScriptName )
1248+ {
1249+ localScriptPath =
1250+ await this . remoteFileManager . FetchRemoteFileAsync (
1251+ e . InvocationInfo . ScriptName ,
1252+ this . powerShellContext . CurrentRunspace ) ;
1253+ }
12611254
12621255 if ( this . stackFrameDetails . Length > 0 )
12631256 {
@@ -1305,25 +1298,24 @@ private void OnBreakpointUpdated(object sender, BreakpointUpdatedEventArgs e)
13051298 if ( e . Breakpoint is LineBreakpoint lineBreakpoint )
13061299 {
13071300 string scriptPath = lineBreakpoint . Script ;
1308- // TODO: BRING THIS BACK
1309- //if (this.powerShellContext.CurrentRunspace.Location == RunspaceLocation.Remote &&
1310- // this.remoteFileManager != null)
1311- //{
1312- // string mappedPath =
1313- // this.remoteFileManager.GetMappedPath(
1314- // scriptPath,
1315- // this.powerShellContext.CurrentRunspace);
1316-
1317- // if (mappedPath == null)
1318- // {
1319- // this.logger.LogError(
1320- // $"Could not map remote path '{scriptPath}' to a local path.");
1321-
1322- // return;
1323- // }
1324-
1325- // scriptPath = mappedPath;
1326- //}
1301+ if ( this . powerShellContext . CurrentRunspace . Location == RunspaceLocation . Remote &&
1302+ this . remoteFileManager != null )
1303+ {
1304+ string mappedPath =
1305+ this . remoteFileManager . GetMappedPath (
1306+ scriptPath ,
1307+ this . powerShellContext . CurrentRunspace ) ;
1308+
1309+ if ( mappedPath == null )
1310+ {
1311+ this . logger . LogError (
1312+ $ "Could not map remote path '{ scriptPath } ' to a local path.") ;
1313+
1314+ return ;
1315+ }
1316+
1317+ scriptPath = mappedPath ;
1318+ }
13271319
13281320 // Normalize the script filename for proper indexing
13291321 string normalizedScriptName = scriptPath . ToLower ( ) ;
0 commit comments