export async function bazelDebugTestAtCursor()

in src/bazel/bazelTestUtils.ts [385:455]


export async function bazelDebugTestAtCursor(
	editorOrDocument: vscode.TextEditor | vscode.TextDocument,
	testFunctionName: string,
	testFunctions: vscode.DocumentSymbol[],
	goConfig: vscode.WorkspaceConfiguration,
	sessionID?: string
) {
	const doc = 'document' in editorOrDocument ? editorOrDocument.document : editorOrDocument;
	const workspaceFolder = vscode.workspace.getWorkspaceFolder(doc.uri);

	const debugPort = await portfinder.getPortPromise();
	const debugConfig: vscode.DebugConfiguration = {
		name: `Bazel Debug ${testFunctionName}`,
		type: 'go',
		request: 'attach',
		debugAdapter: 'dlv-dap',
		mode: 'remote',
		port: debugPort,
		fileName: doc.fileName.replace(path.dirname(doc.fileName) + '/', ''),
		host: '127.0.0.1',
		substitutePath: [
			{
				from: '${env:GOPATH}/src',
				to: 'src'
			},
			{
				from: '${env:GOPATH}/bazel-go-code/external/',
				to: 'external/'
			},
			{
				from: '${env:GOPATH}/bazel-out/',
				to: 'bazel-out/'
			},
			{
				from: '${env:GOPATH}/bazel-go-code/external/go_sdk',
				to: 'GOROOT/'
			}
		],
		sessionID
	};

	// Start the debug server for the required function.
	const runResult = await goTestWithBazel(
		{
			dir: path.dirname(doc.fileName) ?? '',
			functions: [testFunctionName],
			goConfig: goConfig,
			flags: [],
			isMod: false
		},
		debugConfig
	);

	// Start the VS Code debug session and direct the user to the debug UI.
	if (runResult.debugReady) {
		vscode.commands.executeCommand('workbench.debug.action.focusRepl');

		// Listener to terminate the debug server after completion of the debug session.
		vscode.debug.onDidTerminateDebugSession(async (session) => {
			if (session.configuration.sessionID === debugConfig.sessionID) {
				await cp.exec(`kill $(lsof -t -i:${debugConfig.port})`);
				// Return the user to the testing view once the debug session is complete.
				testOutputChannel.show();
				vscode.commands.executeCommand('workbench.view.testing.focus');
			}
		});
		return await vscode.debug.startDebugging(workspaceFolder, debugConfig);
	}

	return false;
}