dotnet/runtime · error · Error

method instrumentation is not enabled, please enable it with

Error message

method instrumentation is not enabled, please enable it with WasmPerformanceInstrumentation MSBuild property

What it means

Thrown by collectCpuSamples() in diagnostics/dotnet-cpu-profiler.ts when the runtime configuration does not have the environment variable DOTNET_WasmPerformanceInstrumentation set. The WASM CPU profiler needs method-level instrumentation emitted into the assemblies at build time; that instrumentation is only produced when the WasmPerformanceInstrumentation MSBuild property is true, which also sets the corresponding env var. Without it there is nothing for the profiler to sample, so the call is rejected up front.

Source

Thrown at src/mono/browser/runtime/diagnostics/dotnet-cpu-profiler.ts:17

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

import type { DiagnosticCommandOptions } from "../types";

import { commandStopTracing, commandSampleProfiler } from "./client-commands";
import { loaderHelpers, Module, runtimeHelpers } from "./globals";
import { serverSession, setupJsClient } from "./diagnostics-js";
import { IDiagnosticSession } from "./common";

export function collectCpuSamples (options?:DiagnosticCommandOptions):Promise<Uint8Array[]> {
    if (!options) options = {};
    if (!serverSession) {
        throw new Error("No active JS diagnostic session");
    }
    if (!runtimeHelpers.config.environmentVariables!["DOTNET_WasmPerformanceInstrumentation"]) {
        throw new Error("method instrumentation is not enabled, please enable it with WasmPerformanceInstrumentation MSBuild property");
    }

    const onClosePromise = loaderHelpers.createPromiseController<Uint8Array[]>();
    function onSessionStart (session: IDiagnosticSession): void {
        // stop tracing after period of monitoring
        Module.safeSetTimeout(() => {
            session.sendCommand(commandStopTracing(session.session_id));
        }, 1000 * (options?.durationSeconds ?? 60));
    }

    setupJsClient({
        onClosePromise:onClosePromise.promise_control,
        skipDownload:options.skipDownload,
        commandOnAdvertise: () => commandSampleProfiler(options),
        onSessionStart,
    });
    return onClosePromise.promise;
}

View on GitHub (pinned to 290d5ab72c)

Solutions

  1. Add <WasmPerformanceInstrumentation>true</WasmPerformanceInstrumentation> to your .csproj (inside a PropertyGroup) and republish.
  2. Confirm runtimeHelpers.config.environmentVariables['DOTNET_WasmPerformanceInstrumentation'] is set after load (proves the build emitted it).
  3. Rebuild/reload the app so the instrumented assemblies and env var are served.

Example fix

<!-- before: .csproj has no instrumentation -->
<PropertyGroup>
  <TargetFramework>net9.0</TargetFramework>
</PropertyGroup>

<!-- after -->
<PropertyGroup>
  <TargetFramework>net9.0</TargetFramework>
  <WasmPerformanceInstrumentation>true</WasmPerformanceInstrumentation>
</PropertyGroup>
Defensive patterns

Strategy: validation

Validate before calling

function canProfileCpu(): boolean {
  return !!runtimeHelpers.config.environmentVariables?.["DOTNET_WasmPerformanceInstrumentation"];
}
// publish with <WasmPerformanceInstrumentation>true</WasmPerformanceInstrumentation> before relying on this

Type guard

null

Try / catch

null

Prevention

When it happens

Trigger: Calling API.collectCpuSamples() on an app that was published without <WasmPerformanceInstrumentation>true</WasmPerformanceInstrumentation> in the project file. Profiling a release build where instrumentation was stripped.

Common situations: Trying to profile a normally-published WASM app. Forgetting the MSBuild property in the .csproj. Publishing in Release which may omit instrumentation.

Related errors


AI-assisted analysis of dotnet/runtime@290d5ab72c (2026-08-06). Data as JSON: /api/errors/b07e0c17f27ce5d7. Report an issue: GitHub.