{"record":{"id":"9b7de871c13c15b2","repo":"dotnet/runtime","slug":"method-instrumentation-is-not-enabled-please-enab-9b7de8","errorCode":null,"errorMessage":"method instrumentation is not enabled, please enable it with WasmPerformanceInstrumentation MSBuild property","messagePattern":"method instrumentation is not enabled, please enable it with WasmPerformanceInstrumentation MSBuild property","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/native/libs/System.Native.Browser/diagnostics/dotnet-cpu-profiler.ts","lineNumber":17,"sourceCode":"// Licensed to the .NET Foundation under one or more agreements.\n// The .NET Foundation licenses this file to you under the MIT license.\n\nimport type { DiagnosticCommandOptions } from \"../types\";\n\nimport { commandResumeRuntime, commandStopTracing, commandSampleProfiler } from \"./client-commands\";\nimport { dotnetApi, dotnetLoaderExports, Module } from \"./cross-module\";\nimport { serverSession, setupJsClient } from \"./diagnostic-server-js\";\nimport { IDiagnosticSession } from \"./types\";\n\nexport function collectCpuSamples(options?: DiagnosticCommandOptions, startup?: boolean): Promise<Uint8Array[]> {\n    if (!options) options = {};\n    if (!startup && !serverSession) {\n        throw new Error(\"No active JS diagnostic session\");\n    }\n    if (!dotnetApi.getConfig().environmentVariables![\"DOTNET_WasmPerformanceInstrumentation\"]) {\n        throw new Error(\"method instrumentation is not enabled, please enable it with WasmPerformanceInstrumentation MSBuild property\");\n    }\n\n    const onClosePromise = dotnetLoaderExports.createPromiseCompletionSource<Uint8Array[]>();\n    function onSessionStart(session: IDiagnosticSession): void {\n        session.sendCommand(commandResumeRuntime());\n        // stop tracing after period of monitoring\n        Module.safeSetTimeout(() => {\n            session.sendCommand(commandStopTracing(session.sessionId));\n        }, 1000 * (options?.durationSeconds ?? 60));\n    }\n\n    setupJsClient({\n        onClosePromise: onClosePromise,\n        skipDownload: options.skipDownload,\n        commandOnAdvertise: () => commandSampleProfiler(options),\n        onSessionStart,\n    }, startup);\n    return onClosePromise.promise;","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/dotnet/runtime/blob/290d5ab72cc1102813fbc0406fb186ceadabc340/src/native/libs/System.Native.Browser/diagnostics/dotnet-cpu-profiler.ts#L1-L35","documentation":"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.","triggerScenarios":"collectCpuSamples() called on a runtime build produced without <WasmPerformanceInstrumentation>true</WasmPerformanceInstrumentation>, so the env var is absent from the runtime config.","commonSituations":"Profiling a Release build that was never instrumented; profiling a default dev build; env var stripped by a custom config.","solutions":["Add <WasmPerformanceInstrumentation>true</WasmPerformanceInstrumentation> to the .csproj and republish.","Verify at runtime that DOTNET_WasmPerformanceInstrumentation is present in the runtime environment variables."],"exampleFix":"// before: profiling a non-instrumented build\ncollectCpuSamples(); // throws\n// after: enable instrumentation in the project file\n// <PropertyGroup>\n//   <WasmPerformanceInstrumentation>true</WasmPerformanceInstrumentation>\n// </PropertyGroup>\ncollectCpuSamples();","handlingStrategy":"validation","validationCode":"// Check the instrumentation env var before profiling\nconst env = dotnetApi.getConfig().environmentVariables ?? {};\nif (!env.DOTNET_WasmPerformanceInstrumentation) {\n  throw new Error('Set <WasmPerformanceInstrumentation>true</WasmPerformanceInstrumentation> in the .csproj and republish before profiling.');\n}\ncollectCpuSamples();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Build with <WasmPerformanceInstrumentation>true</WasmPerformanceInstrumentation> for any profiled configuration.","Assert the env var at runtime before calling collectCpuSamples.","Keep a separate instrumented publish for profiling rather than reusing a Release drop."],"tags":["diagnostics","profiling","msbuild","configuration"],"analyzedSha":"290d5ab72cc1102813fbc0406fb186ceadabc340","analyzedAt":"2026-08-06T19:57:01.276Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}