You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add .vscode/ to .gitignore, add launch configuration samples elsewhere in repo, update debugging docs (#36527)
* Remove .vscode/ from version control
* Add section about debugging automated tests
* Update existing debugging advice to reference example file, deduplicate text about automated tests
* Add details about launching TF ni debug mode from within VS Code
* Respond to review feedback, other small edits like full-stops
-[Debugging automated tests in VSCode](#debugging-automated-tests-in-vscode)
6
+
-[Debugging Terraform operations that use real Terraform configurations](#debugging-terraform-operations-that-use-real-terraform-configurations)
7
+
-[Launch Terraform with the `dlv` CLI tool](#launch-terraform-with-the-dlv-cli-tool)
8
+
-[Launch Terraform with VS Code's debugging tool](#launch-terraform-with-vs-codes-debugging-tool)
9
+
10
+
3
11
As Terraform is written in Go you may use [Delve](https://github.com/go-delve/delve) to debug it.
4
12
5
-
## 1. Compile & Start Debug Server
13
+
GoLand includes [debugging features](https://www.jetbrains.com/help/go/debugging-code.html), and the [Go extension for VS Code](https://code.visualstudio.com/docs/languages/go#_debugging) makes it easy to use Delve when debugging Go codebases in VS Code.
14
+
15
+
## Debugging automated tests
16
+
17
+
Debugging an automated test is often the most straightforward workflow for debugging a section of the codebase. For example, the Go extension for VS Code](https://code.visualstudio.com/docs/languages/go#_debugging) adds `run test | debug test` options above all tests in a `*_test.go` file. These allow debugging without any prior configuration.
18
+
19
+
### Debugging automated tests in VSCode
20
+
21
+
As described above, debugging tests in VS Code is easily achieved through the Go extension.
22
+
23
+
If you need more control over how tests are run while debugging, e.g. environment variable values, look at the [example debugger launch configuration 'Run selected test'](./debugging-configs/vscode/debug-automated-tests/launch.json). You can adapt this example to create your own [launch configuration file](https://code.visualstudio.com/docs/editor/debugging#_launch-configurations).
24
+
25
+
When using this launch configuration you must highlight a test's name before starting the debugger:
26
+
27
+
<palign="center">
28
+
<img width="75%" alt="Debugging a single test using the example 'Run selected test' debugger configuration shared in this repository" src="./images/vscode-debugging-test.png"/>
29
+
</p>
30
+
31
+
32
+
## Debugging Terraform operations that use real Terraform configurations
33
+
34
+
### Launch Terraform with the `dlv` CLI tool
35
+
36
+
In this workflow you:
37
+
* Build Terraform using compiler flags.
38
+
* Start a debug server with a command containing the terraform command you want to debug.
39
+
* This command is run in the working directory that contains your Terraform configuration.
40
+
* Connect to the debug server to monitor progress through breakpoints.
41
+
42
+
#### 1. Compile & Start Debug Server
6
43
7
44
One way to do it is to compile a binary with the [appropriate compiler flags](https://pkg.go.dev/cmd/compile#hdr-Command_Line):
8
45
@@ -13,21 +50,48 @@ go install -gcflags="all=-N -l"
13
50
This enables you to then execute the compiled binary via Delve, pass any arguments and spin up a debug server which you can then connect to:
You may connect to the headless debug server via Delve CLI
22
60
23
61
```sh
24
62
dlv connect :2345
25
63
```
26
64
27
-
## 2b. Connect from VS Code
65
+
#### 2b. Connect from VS Code
66
+
67
+
The repository provides [an example 'Connect to dlv server' launch configuration](./debugging-configs/vscode/launch-via-cli/launch.json), making it possible to use VS Code's native debugging integration via the [Go extension for VS Code](https://code.visualstudio.com/docs/languages/go#_debugging):
### Launch Terraform with VS Code's debugging tool
75
+
76
+
In this workflow you:
77
+
* Update the debugger's launch configuration to point at the directory containing your Terraform configuration.
78
+
* Start the debugger through VS Code and monitor progress through breakpoints.
79
+
80
+
#### 1. Update the debugger's launch configuration
81
+
82
+
Look at the [example debugger launch configuration 'Run Terraform in debug mode'](./debugging-configs/vscode/launch-from-vscode-debugger/launch.json). You can adapt this example to create your own [launch configuration file](https://code.visualstudio.com/docs/editor/debugging#_launch-configurations).
83
+
84
+
To use this launch configuration:
85
+
* Prepare a local Terraform project.
86
+
* Get the absolute path to that directory.
87
+
* Update the launch configuration to use that path, either in the `-chdir` argument or as a `cwd` attribute in the launch configuration.
88
+
* Make sure the `args` array's element reflect the command you'd like to debug.
89
+
* Provide any required environment variables through the `env` or `envFile` attributes.
90
+
91
+
#### 2. Run the launch configuration in VS Code
28
92
29
-
The repository provides a launch configuration, making it possible to use VS Code's native debugging integration:
93
+
Navigate to the Run and Debug view in the left-side Activity Bar. After selecting the `Run Terraform in debug mode`configuration in the Run and Debug view from the left-side, press the green arrow.
30
94
31
-

95
+
This is equivalent to running a Terraform CLI command in the local Terraform project's directory. For example, if you run and debug a plan command that saves a plan file, that plan file will be created.
32
96
33
-
Note that this debugging workflow is different from the test-based one, which itself shouldn't require any of the above steps above nor the mentioned launch configuration. Meaning, that if you already have a test that hits the right lines of code you want to be debugging, or you can write one, then that may be an easier workflow.
97
+
This workflow is useful if you need to set up a complicated prior state to replicate a bug or if you want to debug code behaviour given a specific configuration.
0 commit comments