@@ -254,55 +254,56 @@ protected async Task HandleLaunchRequest(
254
254
{
255
255
this . RegisterEventHandlers ( ) ;
256
256
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 )
270
260
{
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 ;
273
264
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 ) )
278
268
{
279
- if ( ( File . GetAttributes ( workingDir ) & FileAttributes . Directory ) != FileAttributes . Directory )
269
+ workingDir = PowerShellContext . UnescapePath ( workingDir ) ;
270
+ try
280
271
{
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 } ") ;
282
283
}
283
284
}
284
- catch ( Exception ex )
285
- {
286
- Logger . Write ( LogLevel . Error , "cwd path is invalid: " + ex . Message ) ;
287
-
288
- workingDir = null ;
289
- }
290
- }
291
285
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
+ {
294
290
#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 ;
296
293
#else
297
- workingDir = Environment . CurrentDirectory ;
294
+ workingDir = Environment . CurrentDirectory ;
298
295
#endif
299
- }
296
+ }
300
297
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 } '") ) ;
306
307
}
307
308
308
309
// Prepare arguments to the script - if specified
@@ -315,9 +316,7 @@ protected async Task HandleLaunchRequest(
315
316
316
317
// Store the launch parameters so that they can be used later
317
318
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 ;
321
320
this . arguments = arguments ;
322
321
this . IsUsingTempIntegratedConsole = launchParams . CreateTemporaryIntegratedConsole ;
323
322
0 commit comments