@@ -26,9 +26,11 @@ export class DebugSessionFeature implements IFeature {
2626 private startDebugSession ( config : any ) {
2727
2828 let currentDocument = vscode . window . activeTextEditor . document ;
29+ let debugCurrentScript = ( config . script === "${file}" ) || ! config . request ;
30+ let generateLaunchConfig = ! config . request ;
2931
30- if ( ! config . request ) {
31- // No launch.json, create the default configuration
32+ if ( generateLaunchConfig ) {
33+ // No launch.json, create the default configuration for both unsaved (Untitled) and saved documents.
3234 config . type = 'PowerShell' ;
3335 config . name = 'PowerShell Launch Current File' ;
3436 config . request = 'launch' ;
@@ -38,19 +40,30 @@ export class DebugSessionFeature implements IFeature {
3840 currentDocument . isUntitled
3941 ? currentDocument . uri . toString ( )
4042 : currentDocument . fileName ;
43+
44+ // For a folder-less workspace, vscode.workspace.rootPath will be undefined.
45+ // PSES will convert that undefined to a reasonable working dir.
46+ config . cwd =
47+ currentDocument . isUntitled
48+ ? vscode . workspace . rootPath
49+ : currentDocument . fileName ;
4150 }
4251
4352 if ( config . request === 'launch' ) {
44- // Make sure there's a usable working directory if possible
45- config . cwd = config . cwd || vscode . workspace . rootPath ;
4653
47- // For launch of "current script", don't start the debugger if the current file
48- // is not a file that can be debugged by PowerShell
49- if ( config . script === "${file}" ) {
54+ // For debug launch of "current script" (saved or unsaved), warn before starting the debugger if either
55+ // A) the unsaved document's language type is not PowerShell or
56+ // B) the saved document's extension is a type that PowerShell can't debug.
57+ if ( debugCurrentScript ) {
5058
5159 if ( currentDocument . isUntitled ) {
5260 if ( currentDocument . languageId === 'powershell' ) {
53- config . script = currentDocument . uri . toString ( ) ;
61+ if ( ! generateLaunchConfig ) {
62+ // Cover the case of existing launch.json but unsaved (Untitled) document.
63+ // In this case, vscode.workspace.rootPath will not be undefined.
64+ config . script = currentDocument . uri . toString ( ) ;
65+ config . cwd = vscode . workspace . rootPath
66+ }
5467 }
5568 else {
5669 let msg = "To debug '" + currentDocument . fileName +
@@ -80,12 +93,6 @@ export class DebugSessionFeature implements IFeature {
8093 }
8194 }
8295 }
83- else if ( config . script ) {
84- // In this case, the user has explicitly defined a script path
85- // so make sure to set the cwd to that path if the cwd wasn't
86- // explicitly set
87- config . cwd = config . cwd || config . script ;
88- }
8996 }
9097
9198 // Prevent the Debug Console from opening
0 commit comments