async function show()

in src/goTest/profile.ts [148:225]


async function show(profile: string) {
	const foundDot = await new Promise<boolean>((resolve, reject) => {
		const proc = spawn(correctBinname('dot'), ['-V']);

		proc.on('error', (err) => {
			// eslint-disable-next-line @typescript-eslint/no-explicit-any
			if ((err as any).code === 'ENOENT') resolve(false);
			else reject(err);
		});

		proc.on('exit', (code, signal) => {
			if (signal) reject(new Error(`Received signal ${signal}`));
			else if (code) reject(new Error(`Exited with code ${code}`));
			else resolve(true);
		});
	});
	if (!foundDot) {
		const r = await vscode.window.showErrorMessage(
			'Failed to execute dot. Is Graphviz installed?',
			'Open graphviz.org'
		);
		if (r) await vscode.env.openExternal(vscode.Uri.parse('https://graphviz.org/'));
		return;
	}

	const proc = spawn(getBinPath('go'), ['tool', 'pprof', '-http=:', '-no_browser', profile]);
	pprofProcesses.add(proc);

	const port = await new Promise<string | undefined>((resolve, reject) => {
		proc.on('error', (err) => {
			pprofProcesses.delete(proc);
			reject(err);
		});

		proc.on('exit', (code, signal) => {
			pprofProcesses.delete(proc);
			reject(signal || code);
		});

		let stderr = '';
		function captureStdout(b: Buffer) {
			stderr += b.toString('utf-8');

			const m = stderr.match(/^Serving web UI on http:\/\/localhost:(?<port>\d+)\n/);
			if (!m) return;

			resolve(m.groups?.port);
			proc.stdout.off('data', captureStdout);
		}

		proc.stderr.on('data', captureStdout);
	});

	const panel = vscode.window.createWebviewPanel('go.profile', 'Profile', ViewColumn.Active);
	panel.webview.options = { enableScripts: true };
	panel.webview.html = `<html>
		<head>
			<style>
				body {
					padding: 0;
					background: white;
					overflow: hidden;
				}

				iframe {
					border: 0;
					width: 100%;
					height: 100vh;
				}
			</style>
		</head>
		<body>
			<iframe src="http://localhost:${port}"></iframe>
		</body>
	</html>`;

	panel.onDidDispose(() => killProcessTree(proc));
}