@@ -26,9 +26,11 @@ export class DebugSessionFeature implements IFeature {
26
26
private startDebugSession ( config : any ) {
27
27
28
28
let currentDocument = vscode . window . activeTextEditor . document ;
29
+ let debugCurrentScript = ( config . script === "${file}" ) || ! config . request ;
30
+ let generateLaunchConfig = ! config . request ;
29
31
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.
32
34
config . type = 'PowerShell' ;
33
35
config . name = 'PowerShell Launch Current File' ;
34
36
config . request = 'launch' ;
@@ -38,19 +40,30 @@ export class DebugSessionFeature implements IFeature {
38
40
currentDocument . isUntitled
39
41
? currentDocument . uri . toString ( )
40
42
: 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 ;
41
50
}
42
51
43
52
if ( config . request === 'launch' ) {
44
- // Make sure there's a usable working directory if possible
45
- config . cwd = config . cwd || vscode . workspace . rootPath ;
46
53
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 ) {
50
58
51
59
if ( currentDocument . isUntitled ) {
52
60
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
+ }
54
67
}
55
68
else {
56
69
let msg = "To debug '" + currentDocument . fileName +
@@ -80,12 +93,6 @@ export class DebugSessionFeature implements IFeature {
80
93
}
81
94
}
82
95
}
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
- }
89
96
}
90
97
91
98
// Prevent the Debug Console from opening
0 commit comments