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 when the DOTNET_WasmPerformanceInstrumentation environment variable is not set. CPU sampling requires method-level instrumentation that is compiled into the runtime only when the WasmPerformanceInstrumentation MSBuild property is enabled; without it there is nothing to sample.

Source

Thrown at src/native/libs/System.Native.Browser/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 { commandResumeRuntime, commandStopTracing, commandSampleProfiler } from "./client-commands";
import { dotnetApi, dotnetLoaderExports, Module } from "./cross-module";
import { serverSession, setupJsClient } from "./diagnostic-server-js";
import { IDiagnosticSession } from "./types";

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

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

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

View on GitHub (pinned to 290d5ab72c)

Solutions

  1. Add <WasmPerformanceInstrumentation>true</WasmPerformanceInstrumentation> to the .csproj and republish.
  2. Verify at runtime that DOTNET_WasmPerformanceInstrumentation is present in the runtime environment variables.

Example fix

// before: profiling a non-instrumented build
collectCpuSamples(); // throws
// after: enable instrumentation in the project file
// <PropertyGroup>
//   <WasmPerformanceInstrumentation>true</WasmPerformanceInstrumentation>
// </PropertyGroup>
collectCpuSamples();
Defensive patterns

Strategy: validation

Validate before calling

// Check the instrumentation env var before profiling
const env = dotnetApi.getConfig().environmentVariables ?? {};
if (!env.DOTNET_WasmPerformanceInstrumentation) {
  throw new Error('Set <WasmPerformanceInstrumentation>true</WasmPerformanceInstrumentation> in the .csproj and republish before profiling.');
}
collectCpuSamples();

Prevention

When it happens

Trigger: collectCpuSamples() called on a runtime build produced without <WasmPerformanceInstrumentation>true</WasmPerformanceInstrumentation>, so the env var is absent from the runtime config.

Common situations: Profiling a Release build that was never instrumented; profiling a default dev build; env var stripped by a custom config.

Related errors


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