@@ -254,55 +254,56 @@ protected async Task HandleLaunchRequest(
254254 {
255255 this . RegisterEventHandlers ( ) ;
256256
257- // Set the working directory for the PowerShell runspace to the cwd passed in via launch.json.
258- // In case that is null, use the the folder of the script to be executed. If the resulting
259- // working dir path is a file path then extract the directory and use that.
260- string workingDir =
261- launchParams . Cwd ??
262- launchParams . Script ??
263- #pragma warning disable 618
264- launchParams . Program ;
265- #pragma warning restore 618
266-
267- // When debugging an "untitled" (unsaved) file - the working dir can't be derived
268- // from the Script path. OTOH, if the launch params specifies a Cwd, use it.
269- if ( ScriptFile . IsUntitledPath ( workingDir ) && string . IsNullOrEmpty ( launchParams . Cwd ) )
257+ // Determine whether or not the working directory should be set in the PowerShellContext.
258+ if ( ( this . editorSession . PowerShellContext . CurrentRunspace . Location == RunspaceLocation . Local ) &&
259+ ! this . editorSession . DebugService . IsDebuggerStopped )
270260 {
271- workingDir = null ;
272- }
261+ // Get the working directory that was passed via the debug config
262+ // (either via launch.json or generated via no-config debug).
263+ string workingDir = launchParams . Cwd ;
273264
274- if ( ! string . IsNullOrEmpty ( workingDir ) )
275- {
276- workingDir = PowerShellContext . UnescapePath ( workingDir ) ;
277- try
265+ // Assuming we have a non-empty/null working dir, unescape the path and verify
266+ // the path exists and is a directory.
267+ if ( ! string . IsNullOrEmpty ( workingDir ) )
278268 {
279- if ( ( File . GetAttributes ( workingDir ) & FileAttributes . Directory ) != FileAttributes . Directory )
269+ workingDir = PowerShellContext . UnescapePath ( workingDir ) ;
270+ try
280271 {
281- workingDir = Path . GetDirectoryName ( workingDir ) ;
272+ if ( ( File . GetAttributes ( workingDir ) & FileAttributes . Directory ) != FileAttributes . Directory )
273+ {
274+ workingDir = Path . GetDirectoryName ( workingDir ) ;
275+ }
276+ }
277+ catch ( Exception ex )
278+ {
279+ workingDir = null ;
280+ Logger . Write (
281+ LogLevel . Error ,
282+ $ "The specified 'cwd' path is invalid: '{ launchParams . Cwd } '. Error: { ex . Message } ") ;
282283 }
283284 }
284- catch ( Exception ex )
285- {
286- Logger . Write ( LogLevel . Error , "cwd path is invalid: " + ex . Message ) ;
287-
288- workingDir = null ;
289- }
290- }
291285
292- if ( string . IsNullOrEmpty ( workingDir ) )
293- {
286+ // If we have no working dir by this point and we are running in a temp console,
287+ // pick some reasonable default.
288+ if ( string . IsNullOrEmpty ( workingDir ) && launchParams . CreateTemporaryIntegratedConsole )
289+ {
294290#if CoreCLR
295- workingDir = AppContext . BaseDirectory ;
291+ //TODO: RKH 2018-06-26 .NET standard 2.0 has added Environment.CurrentDirectory - let's use it.
292+ workingDir = AppContext . BaseDirectory ;
296293#else
297- workingDir = Environment . CurrentDirectory ;
294+ workingDir = Environment . CurrentDirectory ;
298295#endif
299- }
296+ }
300297
301- if ( this . editorSession . PowerShellContext . CurrentRunspace . Location == RunspaceLocation . Local &&
302- ! this . editorSession . DebugService . IsDebuggerStopped )
303- {
304- await editorSession . PowerShellContext . SetWorkingDirectory ( workingDir , isPathAlreadyEscaped : false ) ;
305- Logger . Write ( LogLevel . Verbose , "Working dir set to: " + workingDir ) ;
298+ // At this point, we will either have a working dir that should be set to cwd in
299+ // the PowerShellContext or the user has requested (via an empty/null cwd) that
300+ // the working dir should not be changed.
301+ if ( ! string . IsNullOrEmpty ( workingDir ) )
302+ {
303+ await editorSession . PowerShellContext . SetWorkingDirectory ( workingDir , isPathAlreadyEscaped : false ) ;
304+ }
305+
306+ Logger . Write ( LogLevel . Verbose , $ "Working dir " + ( string . IsNullOrEmpty ( workingDir ) ? "not set." : $ "set to '{ workingDir } '") ) ;
306307 }
307308
308309 // Prepare arguments to the script - if specified
@@ -315,9 +316,7 @@ protected async Task HandleLaunchRequest(
315316
316317 // Store the launch parameters so that they can be used later
317318 this . noDebug = launchParams . NoDebug ;
318- #pragma warning disable 618
319- this . scriptToLaunch = launchParams . Script ?? launchParams . Program ;
320- #pragma warning restore 618
319+ this . scriptToLaunch = launchParams . Script ;
321320 this . arguments = arguments ;
322321 this . IsUsingTempIntegratedConsole = launchParams . CreateTemporaryIntegratedConsole ;
323322
0 commit comments