fix(debug): guarded v8.setFlagsFromString call so CPU profiler works on Bun

The CPU profiler called node:v8 setFlagsFromString("--allow-natives-syntax")
unconditionally and crashed on Bun (oven-sh/bun#1702), surfacing as
"Failed to start profiler: node:v8 setFlagsFromString is not yet
implemented in Bun". The flag is only needed for ad-hoc V8 natives such
as %GetOptimizationStatus, not for the CDP Profiler session that actually
collects samples — so swallow the error and let the inspector path run.

Added a Bun-runtime regression test that would have caught the crash:
without the guard, startCpuProfile() throws before returning a session.

Fixes #3897
This commit is contained in:
roboomp
2026-06-30 12:47:07 +00:00
parent 5bc68f57cd
commit 0ef430a39f
3 changed files with 35 additions and 1 deletions
+7 -1
View File
@@ -114,7 +114,13 @@ function formatProfileAsMarkdown(profileJson: string): string {
*/
export async function startCpuProfile(): Promise<ProfilerSession> {
const v8 = await import("node:v8");
v8.setFlagsFromString("--allow-natives-syntax");
try {
// Enables `%GetOptimizationStatus` and friends when V8 natives are needed
// for ad-hoc profiling. Best-effort: Bun does not implement
// `setFlagsFromString` (oven-sh/bun#1702) but the CPU profiler itself
// works without it, so swallow the error and continue.
v8.setFlagsFromString("--allow-natives-syntax");
} catch {}
const { Session } = await import("node:inspector/promises");
const session = new Session();