Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vscode): detach from debug session on premature application exit #1043

Merged
merged 8 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 72 additions & 1 deletion extensions/vscode/src/commands/debug-dev-server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { DartFrogApplication, DartFrogDaemon } from "../daemon";
import {
DartFrogApplication,
DartFrogApplicationRegistry,
DartFrogApplicationRegistryEventEmitterTypes,
DartFrogDaemon,
} from "../daemon";
import { Uri, commands, debug, extensions, window } from "vscode";
import {
isDartFrogCLIInstalled,
Expand Down Expand Up @@ -113,6 +118,10 @@ export const debugDevServer = async (
}
}

onApplicationDettached(daemon.applicationRegistry, application.id!).then(() =>
detachFromDebugSession(application.id!)
);

await attachToDebugSession(application);
};

Expand All @@ -135,8 +144,70 @@ async function attachToDebugSession(
request: "attach",
type: "dart",
vmServiceUri: application.vmServiceUri,
source: "dart-frog",
applicationId: application.id,
});
}
);
}

/**
* Detaches from the current Dart debug session.
*
* @param applicationId The application attached to the debug session to.
*/
async function detachFromDebugSession(applicationId: string): Promise<void> {
const debugSession = debug.activeDebugSession;

if (
debugSession &&
debugSession.configuration.source === "dart-frog" &&
debugSession.configuration.applicationId === applicationId
) {
debugSession.customRequest("disconnect");
}
}

/**
* Waits for a {@link DartFrogApplication} to be detached from the active
* debug session.
*
* This may happen if the user manually detaches from the debug session, or if
* the application is exited prematurely.
*
* @param registry The {@link DartFrogApplicationRegistry} to listen to.
* @param applicationId The application to wait for.
* @returns A promise that resolves when the application has been deregistered.
*/
function onApplicationDettached(
registry: DartFrogApplicationRegistry,
applicationId: string
): Promise<void> {
return new Promise<void>((resolve) => {
const dispose = () => {
registry.off(
DartFrogApplicationRegistryEventEmitterTypes.remove,
listener
);
resolve();
};

const listener = (application: DartFrogApplication) => {
if (application.id === applicationId) {
dispose();
}
};

debug.onDidTerminateDebugSession((session) => {
if (
session &&
session.configuration.source === "dart-frog" &&
session.configuration.applicationId === applicationId
) {
dispose();
}
});

registry.on(DartFrogApplicationRegistryEventEmitterTypes.remove, listener);
});
}