Skip to content

Commit 111b4dc

Browse files
committed
debug: more async / await
1 parent 0634ecc commit 111b4dc

1 file changed

Lines changed: 95 additions & 84 deletions

File tree

src/vs/workbench/contrib/debug/browser/debugService.ts

Lines changed: 95 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ export class DebugService implements IDebugService {
424424
/**
425425
* instantiates the new session, initializes the session, registers session listeners and reports telemetry
426426
*/
427-
private doCreateSession(root: IWorkspaceFolder | undefined, configuration: { resolved: IConfig, unresolved: IConfig | undefined }, options?: IDebugSessionOptions): Promise<boolean> {
427+
private async doCreateSession(root: IWorkspaceFolder | undefined, configuration: { resolved: IConfig, unresolved: IConfig | undefined }, options?: IDebugSessionOptions): Promise<boolean> {
428428

429429
const session = this.instantiationService.createInstance(DebugSession, configuration, root, this.model, options);
430430
this.model.addSession(session);
@@ -438,10 +438,11 @@ export class DebugService implements IDebugService {
438438
const openDebug = this.configurationService.getValue<IDebugConfiguration>('debug').openDebug;
439439
// Open debug viewlet based on the visibility of the side bar and openDebug setting. Do not open for 'run without debug'
440440
if (!configuration.resolved.noDebug && (openDebug === 'openOnSessionStart' || (openDebug === 'openOnFirstSessionStart' && this.viewModel.firstSessionStart))) {
441-
this.viewletService.openViewlet(VIEWLET_ID).then(undefined, errors.onUnexpectedError);
441+
await this.viewletService.openViewlet(VIEWLET_ID);
442442
}
443443

444-
return this.launchOrAttachToSession(session).then(() => {
444+
try {
445+
await this.launchOrAttachToSession(session);
445446

446447
const internalConsoleOptions = session.configuration.internalConsoleOptions || this.configurationService.getValue<IDebugConfiguration>('debug').internalConsoleOptions;
447448
if (internalConsoleOptions === 'openOnSessionStart' || (this.viewModel.firstSessionStart && internalConsoleOptions === 'openOnFirstSessionStart')) {
@@ -459,12 +460,14 @@ export class DebugService implements IDebugService {
459460
// since the initialized response has arrived announce the new Session (including extensions)
460461
this._onDidNewSession.fire(session);
461462

462-
return this.telemetryDebugSessionStart(root, session.configuration.type);
463-
}).then(() => true, (error: Error | string) => {
463+
await this.telemetryDebugSessionStart(root, session.configuration.type);
464+
465+
return true;
466+
} catch (error) {
464467

465468
if (errors.isPromiseCanceledError(error)) {
466469
// don't show 'canceled' error messages to the user #7906
467-
return Promise.resolve(false);
470+
return false;
468471
}
469472

470473
// Show the repl if some error got logged there #5870
@@ -474,13 +477,15 @@ export class DebugService implements IDebugService {
474477

475478
if (session.configuration && session.configuration.request === 'attach' && session.configuration.__autoAttach) {
476479
// ignore attach timeouts in auto attach mode
477-
return Promise.resolve(false);
480+
return false;
478481
}
479482

480483
const errorMessage = error instanceof Error ? error.message : error;
481484
this.telemetryDebugMisconfiguration(session.configuration ? session.configuration.type : undefined, errorMessage);
482-
return this.showError(errorMessage, isErrorWithActions(error) ? error.actions : []).then(() => false);
483-
});
485+
486+
await this.showError(errorMessage, isErrorWithActions(error) ? error.actions : []);
487+
return false;
488+
}
484489
}
485490

486491
private async launchOrAttachToSession(session: IDebugSession, forceFocus = false): Promise<void> {
@@ -557,87 +562,93 @@ export class DebugService implements IDebugService {
557562
}));
558563
}
559564

560-
restartSession(session: IDebugSession, restartData?: any): Promise<any> {
561-
return this.textFileService.saveAll().then(() => {
562-
const isAutoRestart = !!restartData;
563-
const runTasks: () => Promise<TaskRunResult> = () => {
564-
if (isAutoRestart) {
565-
// Do not run preLaunch and postDebug tasks for automatic restarts
566-
return Promise.resolve(TaskRunResult.Success);
567-
}
565+
async restartSession(session: IDebugSession, restartData?: any): Promise<any> {
566+
await this.textFileService.saveAll();
567+
const isAutoRestart = !!restartData;
568568

569-
return this.runTask(session.root, session.configuration.postDebugTask)
570-
.then(() => this.runTaskAndCheckErrors(session.root, session.configuration.preLaunchTask));
571-
};
569+
const runTasks: () => Promise<TaskRunResult> = async () => {
570+
if (isAutoRestart) {
571+
// Do not run preLaunch and postDebug tasks for automatic restarts
572+
return Promise.resolve(TaskRunResult.Success);
573+
}
572574

573-
if (session.capabilities.supportsRestartRequest) {
574-
return runTasks().then(taskResult => taskResult === TaskRunResult.Success ? session.restart() : undefined);
575+
await this.runTask(session.root, session.configuration.postDebugTask);
576+
return this.runTaskAndCheckErrors(session.root, session.configuration.preLaunchTask);
577+
};
578+
579+
if (session.capabilities.supportsRestartRequest) {
580+
const taskResult = await runTasks();
581+
if (taskResult === TaskRunResult.Success) {
582+
await session.restart();
575583
}
576584

577-
if (isExtensionHostDebugging(session.configuration)) {
578-
return runTasks().then(taskResult => taskResult === TaskRunResult.Success ? this.extensionHostDebugService.reload(session.getId()) : undefined);
585+
return;
586+
}
587+
588+
if (isExtensionHostDebugging(session.configuration)) {
589+
const taskResult = await runTasks();
590+
if (taskResult === TaskRunResult.Success) {
591+
this.extensionHostDebugService.reload(session.getId());
579592
}
580593

581-
const shouldFocus = !!this.viewModel.focusedSession && session.getId() === this.viewModel.focusedSession.getId();
582-
// If the restart is automatic -> disconnect, otherwise -> terminate #55064
583-
return (isAutoRestart ? session.disconnect(true) : session.terminate(true)).then(() => {
584-
585-
return new Promise<void>((c, e) => {
586-
setTimeout(() => {
587-
runTasks().then(taskResult => {
588-
if (taskResult !== TaskRunResult.Success) {
589-
return;
590-
}
591-
592-
// Read the configuration again if a launch.json has been changed, if not just use the inmemory configuration
593-
let needsToSubstitute = false;
594-
let unresolved: IConfig | undefined;
595-
const launch = session.root ? this.configurationManager.getLaunch(session.root.uri) : undefined;
596-
if (launch) {
597-
unresolved = launch.getConfiguration(session.configuration.name);
598-
if (unresolved && !equals(unresolved, session.unresolvedConfiguration)) {
599-
// Take the type from the session since the debug extension might overwrite it #21316
600-
unresolved.type = session.configuration.type;
601-
unresolved.noDebug = session.configuration.noDebug;
602-
needsToSubstitute = true;
603-
}
604-
}
605-
606-
let substitutionThenable: Promise<IConfig | null | undefined> = Promise.resolve(session.configuration);
607-
if (launch && needsToSubstitute && unresolved) {
608-
this.initCancellationToken = new CancellationTokenSource();
609-
substitutionThenable = this.configurationManager.resolveConfigurationByProviders(launch.workspace ? launch.workspace.uri : undefined, unresolved.type, unresolved, this.initCancellationToken.token)
610-
.then(resolved => {
611-
if (resolved) {
612-
// start debugging
613-
return this.substituteVariables(launch, resolved);
614-
} else if (resolved === null) {
615-
// abort debugging silently and open launch.json
616-
return Promise.resolve(null);
617-
} else {
618-
// abort debugging silently
619-
return Promise.resolve(undefined);
620-
}
621-
});
622-
}
623-
substitutionThenable.then(resolved => {
624-
625-
if (!resolved) {
626-
return c(undefined);
627-
}
628-
629-
session.setConfiguration({ resolved, unresolved });
630-
session.configuration.__restart = restartData;
631-
632-
this.launchOrAttachToSession(session, shouldFocus).then(() => {
633-
this._onDidNewSession.fire(session);
634-
c(undefined);
635-
}, err => e(err));
636-
});
637-
});
638-
}, 300);
639-
});
640-
});
594+
return;
595+
}
596+
597+
const shouldFocus = !!this.viewModel.focusedSession && session.getId() === this.viewModel.focusedSession.getId();
598+
// If the restart is automatic -> disconnect, otherwise -> terminate #55064
599+
if (isAutoRestart) {
600+
await session.disconnect(true);
601+
} else {
602+
await session.terminate(true);
603+
}
604+
605+
return new Promise<void>((c, e) => {
606+
setTimeout(async () => {
607+
const taskResult = await runTasks();
608+
if (taskResult !== TaskRunResult.Success) {
609+
return;
610+
}
611+
612+
// Read the configuration again if a launch.json has been changed, if not just use the inmemory configuration
613+
let needsToSubstitute = false;
614+
let unresolved: IConfig | undefined;
615+
const launch = session.root ? this.configurationManager.getLaunch(session.root.uri) : undefined;
616+
if (launch) {
617+
unresolved = launch.getConfiguration(session.configuration.name);
618+
if (unresolved && !equals(unresolved, session.unresolvedConfiguration)) {
619+
// Take the type from the session since the debug extension might overwrite it #21316
620+
unresolved.type = session.configuration.type;
621+
unresolved.noDebug = session.configuration.noDebug;
622+
needsToSubstitute = true;
623+
}
624+
}
625+
626+
let resolved: IConfig | undefined | null = session.configuration;
627+
if (launch && needsToSubstitute && unresolved) {
628+
this.initCancellationToken = new CancellationTokenSource();
629+
const resolvedByProviders = await this.configurationManager.resolveConfigurationByProviders(launch.workspace ? launch.workspace.uri : undefined, unresolved.type, unresolved, this.initCancellationToken.token);
630+
if (resolvedByProviders) {
631+
resolved = await this.substituteVariables(launch, resolvedByProviders);
632+
} else {
633+
resolved = resolvedByProviders;
634+
}
635+
}
636+
637+
if (!resolved) {
638+
return c(undefined);
639+
}
640+
641+
session.setConfiguration({ resolved, unresolved });
642+
session.configuration.__restart = restartData;
643+
644+
try {
645+
await this.launchOrAttachToSession(session, shouldFocus);
646+
this._onDidNewSession.fire(session);
647+
c(undefined);
648+
} catch (error) {
649+
e(error);
650+
}
651+
}, 300);
641652
});
642653
}
643654

0 commit comments

Comments
 (0)