Diagnose extension behavior with the right debugging mode
A defect report rarely tells you which debugging mode to use. The right
choice depends on whether you can trigger the problem yourself, whether
you're allowed to interrupt the session that hits it, and whether the
issue happens immediately or after a delay. This unit walks through the
three modes and how to configure each one in launch.json.
Start with interactive debugging when you control the trigger
Interactive debugging is the default Run and Debug (F5) session: it launches or restarts the client, runs your extension, and stops execution the moment a breakpoint is hit. Use it when you can reproduce the problem yourself on demand, for example while you're actively writing or fixing the object involved:
{
"name": "Contoso sandbox (interactive)",
"type": "al",
"request": "launch",
"environmentType": "Sandbox",
"environmentName": "contoso-dev",
"startupObjectId": 22,
"startupObjectType": "Page",
"breakOnError": "All"
}
Set breakpoints directly in the AL editor. When execution stops, you inspect the call stack, step through code, and examine variables just as you would with any local debugger. If a breakpoint shows as an unbound, hollow circle instead of a solid one, the debugger couldn't match it to code that actually ran; the most common causes are a stale build (republish after your last code change) or a breakpoint placed on a line that the compiler optimized away, such as a comment or a declaration with no executable statement.
Attach to an existing session when you can't restart it
Attach debugging connects to a session that's already running, instead of launching a new one. Choose it when restarting the client would lose the state that reproduces the problem, or when you need to debug a session started by another user or a non-interactive process such as a web service call:
{
"name": "Contoso sandbox (attach)",
"type": "al",
"request": "attach",
"environmentType": "Sandbox",
"environmentName": "contoso-dev",
"breakOnNext": "WebServiceClient"
}
breakOnNext specifies the type of session the debugger attaches to
next: WebServiceClient for OData or SOAP calls, WebClient for the
standard web client, Background for job queue and other background
sessions, or Agent for agent sessions. On a sandbox environment, you
can only attach to and break on the next WebServiceClient or Agent
session. Add userId to target a specific user's next session, and add
sessionId to target one ongoing session of that type belonging to
that user. Attaching to a session initiated by a different user than
the one signed in to Visual Studio Code requires the D365 ATTACH
DEBUG permission set.
Use snapshot debugging for delayed or production-only reproduction
Snapshot debugging records AL execution on the server without stopping it, so you can debug afterward, offline, in Visual Studio Code. It's the right choice when the defect only reproduces in a production environment, or when the triggering event happens later than when you start debugging, such as a scheduled job:
{
"name": "Contoso production (snapshot)",
"type": "al",
"request": "snapshotInitialize",
"environmentType": "Production",
"environmentName": "contoso-prod",
"breakOnNext": "Background",
"userId": " 00001111-aaaa-2222-bbbb-3333cccc4444",
"executionContext": "DebugAndProfile"
}
Instead of breakpoints, snapshot debugging uses snappoints: markers you set before recording starts that tell the server to log variable state at that line, without pausing execution. Debugging a snapshot afterward is similar to a regular session, with these differences:
- Variable data is available only at snappoints and at AL exceptions, not at every stepped line.
- Navigating with Continue (F5) walks through the recorded snappoints; the order may not exactly match server execution order, because some server calls aren't AL calls with walkable stacks.
- An armed snapshot session closes automatically if nothing attaches to it within 30 minutes, and a started session must finish within 10 minutes.
- Creating and downloading a snapshot requires the D365 Snapshot Debug permission set, and the symbols on your Visual Studio Code project must match the symbols on the tenant, since a mismatch prevents the debugger from resolving the recording correctly.
The optional userId is the Business Central user's GUID, not an email
address. If you omit both userId and sessionId, the recording attaches
to the next tenant session that matches breakOnNext.
Match the mode to the constraint, not to habit
When you triage a new defect, ask three questions in order:
- Can I trigger the problem myself, right now? If yes, interactive debugging is simplest and gives you full step-through control.
- Is the problem already happening in a session I can't restart, or in a non-interactive call I can't launch directly? If yes, attach to that session instead of trying to reproduce it from a fresh launch.
- Does the problem only happen in an environment I shouldn't interrupt, or after a delay I can't wait through interactively? If yes, arm a snapshot, wait for the event to occur, then debug the recording offline.
Choosing based on these constraints, rather than defaulting to whichever mode you used last, keeps you from wasting a debugging session trying to interactively reproduce something that was never likely to occur that way.
Review the current configuration and permission details in Debugging, Attach and debug next, and Snapshot debugging.