| 32 | } |
| 33 | |
| 34 | export function createDebugClient(debuggerType: DebuggerType) { |
| 35 | const descriptor = privateApi.debugAdapterDescriptorFactory.descriptorForType(debuggerType); |
| 36 | const trackerFactories = privateApi.trackerFactories as DebugAdapterTrackerFactory[]; |
| 37 | const dc = descriptor instanceof DebugAdapterExecutable |
| 38 | ? new DartDebugClient( |
| 39 | debuggerType, |
| 40 | { |
| 41 | args: descriptor.args.slice(1), |
| 42 | executable: descriptor.args[0], |
| 43 | runtime: descriptor.command, |
| 44 | }, |
| 45 | privateApi.debugCommands, |
| 46 | privateApi.testCoordinator, |
| 47 | trackerFactories, |
| 48 | privateApi.dartCapabilities, |
| 49 | ) |
| 50 | : undefined; |
| 51 | if (!dc) |
| 52 | throw Error(`Unknown debug descriptor type ${descriptor.constructor.name}`); |
| 53 | |
| 54 | dc.defaultTimeout = 120000; |
| 55 | const thisDc = dc; |
| 56 | defer("Terminate and clean up debug client/adapter", async () => { |
| 57 | if (!thisDc.hasStarted) { |
| 58 | privateApi.logger.info(`Skipping shutdown because it never started`); |
| 59 | return; |
| 60 | } |
| 61 | if (!thisDc.hasTerminated) { |
| 62 | // Wait for a terminated event with a timeout. |
| 63 | const terminatedEvent = new Promise((resolve) => thisDc.on("terminated", resolve)); |
| 64 | try { |
| 65 | thisDc.terminateRequest().catch((e) => logger.warn(e)); |
| 66 | // Tests may require a second terminateRequest because they first print "waiting for test to finish...". |
| 67 | if (debuggerType === DebuggerType.DartTest || debuggerType === DebuggerType.FlutterTest || debuggerType === DebuggerType.WebTest) { |
| 68 | await Promise.race([delay(300), terminatedEvent]); |
| 69 | // If we still hasn't termianted, send the second. |
| 70 | if (!thisDc.hasTerminated) { |
| 71 | thisDc.terminateRequest().catch((e) => logger.warn(e)); |
| 72 | await Promise.race([delay(300), terminatedEvent]); |
| 73 | } |
| 74 | } |
| 75 | } catch (e) { |
| 76 | logger.warn(e); |
| 77 | } |
| 78 | await withTimeout(terminatedEvent, "Timed out terminating and cleaning up!", 50); |
| 79 | } |
| 80 | |
| 81 | try { |
| 82 | thisDc.stop().catch((e) => logger.warn(e)); |
| 83 | } catch (e) { |
| 84 | logger.warn(e); |
| 85 | } |
| 86 | }); |
| 87 | return thisDc; |
| 88 | } |
| 89 | |
| 90 | export function startFakeDebugSession(options?: { |
| 91 | debuggerType?: DebuggerType; |