1
1
import { inject , injectable , named } from 'inversify' ;
2
2
import * as path from 'path' ;
3
- import { DebugConfiguration , l10n , Uri , WorkspaceFolder } from 'vscode' ;
3
+ import { DebugConfiguration , l10n , Uri , WorkspaceFolder , DebugSession } from 'vscode' ;
4
4
import { IApplicationShell , IDebugService } from '../../common/application/types' ;
5
5
import { EXTENSION_ROOT_DIR } from '../../common/constants' ;
6
6
import * as internalScripts from '../../common/process/internal/scripts' ;
@@ -9,7 +9,7 @@ import { DebuggerTypeName, PythonDebuggerTypeName } from '../../debugger/constan
9
9
import { IDebugConfigurationResolver } from '../../debugger/extension/configuration/types' ;
10
10
import { DebugPurpose , LaunchRequestArguments } from '../../debugger/types' ;
11
11
import { IServiceContainer } from '../../ioc/types' ;
12
- import { traceError } from '../../logging' ;
12
+ import { traceError , traceVerbose } from '../../logging' ;
13
13
import { TestProvider } from '../types' ;
14
14
import { ITestDebugLauncher , LaunchOptions } from './types' ;
15
15
import { getConfigurationsForWorkspace } from '../../debugger/extension/configuration/launch.json/launchJsonReader' ;
@@ -34,12 +34,20 @@ export class DebugLauncher implements ITestDebugLauncher {
34
34
35
35
public async launchDebugger ( options : LaunchOptions , callback ?: ( ) => void ) : Promise < void > {
36
36
const deferred = createDeferred < void > ( ) ;
37
+ let hasCallbackBeenCalled = false ;
37
38
if ( options . token && options . token . isCancellationRequested ) {
39
+ hasCallbackBeenCalled = true ;
38
40
return undefined ;
39
41
deferred . resolve ( ) ;
40
42
callback ?.( ) ;
41
43
}
42
44
45
+ options . token ?. onCancellationRequested ( ( ) => {
46
+ deferred . resolve ( ) ;
47
+ callback ?.( ) ;
48
+ hasCallbackBeenCalled = true ;
49
+ } ) ;
50
+
43
51
const workspaceFolder = DebugLauncher . resolveWorkspaceFolder ( options . cwd ) ;
44
52
const launchArgs = await this . getLaunchArgs (
45
53
options ,
@@ -48,11 +56,23 @@ export class DebugLauncher implements ITestDebugLauncher {
48
56
) ;
49
57
const debugManager = this . serviceContainer . get < IDebugService > ( IDebugService ) ;
50
58
51
- debugManager . onDidTerminateDebugSession ( ( ) => {
52
- deferred . resolve ( ) ;
53
- callback ?.( ) ;
59
+ let activatedDebugSession : DebugSession | undefined ;
60
+ debugManager . startDebugging ( workspaceFolder , launchArgs ) . then ( ( ) => {
61
+ // Save the debug session after it is started so we can check if it is the one that was terminated.
62
+ activatedDebugSession = debugManager . activeDebugSession ;
63
+ } ) ;
64
+ debugManager . onDidTerminateDebugSession ( ( session ) => {
65
+ traceVerbose ( `Debug session terminated. sessionId: ${ session . id } ` ) ;
66
+ // Only resolve no callback has been made and the session is the one that was started.
67
+ if (
68
+ ! hasCallbackBeenCalled &&
69
+ activatedDebugSession !== undefined &&
70
+ session . id === activatedDebugSession ?. id
71
+ ) {
72
+ deferred . resolve ( ) ;
73
+ callback ?.( ) ;
74
+ }
54
75
} ) ;
55
- debugManager . startDebugging ( workspaceFolder , launchArgs ) ;
56
76
return deferred . promise ;
57
77
}
58
78
0 commit comments