Debug Terraform (Azure Devops) Provider with VSCode

Introduction

In an effort to save someone the pain of having to go through what I did when trying to get debugging to work on the Terraform Azure Devops Provider acceptance tests, here’s the solution.

Prior to starting I was already able to run, debug the unit tests and run the acceptance tests. This article is only about enabling debugging for terraform acceptance tests.

Update: Checkout the Issues section for how to enable codelens debugging.

Environment

The environment is also set up in the Azure Devops devcontainer. The code below can also be found in the repository.

Set up

Add the launch.json and .env below. Edit the .env file as needed for your terraform provider secrets.

NB: The buildFlags attribute was only needed for Azure Devops provider, for example, the Databricks provider work without it.

launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch a test",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${file}",
            "args": [
                "-test.v",
                "-test.run",
                "^${selectedText}$"
            ],
            "env": {
                "TF_ACC": "1",
            },
            "buildFlags": "-v -tags=all",
            "showLog": true,
            "envFile": "${workspaceFolder}/.env"
        }
    ]
}

.env

AZDO_PERSONAL_ACCESS_TOKEN=<your_token>
AZDO_ORG_SERVICE_URL=<your_azdo_org>

Make sure Launch a test is selected in the VSCode debug window

Launch debug

Debug a test

Highlight the name of the test you wish to run test in the test file and press F5 or select Start debugging.

Debugging in action

Run debug test

Issues

The only issue with this solution is that you have to highlight the test name and press F5 instead of being able to just select the debug test option above each test in VSCode. You’re able to run non-integration tests via that option and run all tests via the corresponding run test option.

Debug test

I’d love it if anyone could share a solution where they’ve got it to work with that.

Fixed: Enable codelens debugging

The missing piece to enable codelens debugging is to add the below flags to go.testFlags in settings.json

settings.json

{
    "go.testFlags": [
        "-v",
        "-tags=all",
        "-args",
        "-test.v"
    ],
}

Huge thanks to Thomas Meckel for figuring this out!

Credit

Inspired by https://blog.gripdev.xyz/2019/09/12/easily-debugging-terraform-provider-for-azure-in-vscode/