---
title: "Debug a script using Visual Studio Code"
slug: "debug-a-script-using-visual-studio-code"
abstract: "How to pull and debug a script using Visual Studio Code. Known limitations when inspecting global variables or debugging the action script Context.ChangeTracker."
---
# Debug a script using Visual Studio Code

> [!note]
> **Before you begin**
> -   [Initialize your working folder](https://doc.sitecore.com/ch/en/developers/cloud-dev/initialize-your-working-folder.html "Initialize your working folder")

To debug a script:

1. To pull the script from Content Hub into your working directory, run the following command:

    ```shell
    ch-cli scripting pull -n <YOUR_SCRIPT_NAME> -f <YOUR_FOLDER_PATH>
    ```

2. Open your working directory.
3. On the activity bar, click the **Run and Debug** icon or use the keyboard shortcut Ctrl+Shift+D.
4. From the **Configuration** list in the **Run** view, click the **Debug Script** configuration.

    ![CLI Script Debug select VS Code debug configuration](https://doc.sitecore.com/api/markdown-image/ch/en/developers/cloud-dev/images/cli_script_debug_select_vs_code_debug_configurationpng.png)

5. Set the launch configuration and breakpoints.
6. Press F5 to start the debug session.
7. When prompted to attach to a process, select the process ID that is shown in the terminal output of the CLI command.

    ![CLI Script Debug attach to process](https://doc.sitecore.com/api/markdown-image/ch/en/developers/cloud-dev/images/cli_script_debug_attach_to_processpng.png)

8. In your Content Hub instance, trigger the script to start debugging.

    ![CLI Script Debug process attached](https://doc.sitecore.com/api/markdown-image/ch/en/developers/cloud-dev/images/cli_script_debug_process_attachedpng.png)

> [!note]
> To learn more about Visual Studio Code's built-in debugger, refer to Microsoft's [knowledge base article](https://code.visualstudio.com/docs/editor/debugging).

> [!note]
> When initiating a debugging session for a script, a lock entity of entity definition `M.ScriptDebugLock` is created to indicate that a debug session is in progress. After the script runs to completion, the lock is removed to allow processing of further executions by Content Hub itself.
>
> Certain events on the local machine, including but not limited to process termination and network or connectivity issues can cause the lock entity to be not properly cleaned up. In this case, you can either find and remove the entity manually or use the following command:
>
> `ch-cli scripting cancel-debug -n <YOUR_SCRIPT_NAME>`

## Known limitations

### Inspecting global variables

When you debug a script, the global variables `Context` and `MClient` are populated. However, due to a limitation in Roslyn, it is currently not possible to inspect those variables in [the Variables or Watch sections of Visual Studio Code](https://code.visualstudio.com/docs/editor/debugging#_data-inspection).

If you need to inspect the `Context` or `MClient` variables, one option is to re-assign them and use them throughout your script, as shown in the following example.

```shell
var context = Context;
var client = MClient;

client.Logger.Warn("Hello world!");

```

> [!note]
> When debugging an action script, `Context.ChangeTracker` is not supported.