Skip to content

Commit 899020f

Browse files
authored
Add instructions on how to debug provider (#955)
* Add instructions on how to debug provider * Fix typo
1 parent e9de238 commit 899020f

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

CONTRIBUTING.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,35 @@ Note that some resources still use a previous format that is incompatible with a
5959

6060
Also note that there is no build / `terraform init` / `terraform plan` sequence here. It is uncommon to run into a bug or feature that requires iteration without using tests. When these cases arise, the `examples/` directory is used to approach the problem, which is detailed in the next section.
6161

62+
### Debugging the terraform provider
63+
64+
Println debugging can easily be used to obtain information about how code changes perform. If the `TF_LOG=DEBUG` level is set, calls to `log.Printf("[DEBUG] your message here")` will be printed in the program's output.
65+
66+
If a full debugger is desired, VSCode may be used. In order to do so,
67+
68+
1. create a launch.json file with this configuration:
69+
```json
70+
{
71+
"name": "Attach to Process",
72+
"type": "go",
73+
"request": "attach",
74+
"mode": "local",
75+
"processId": 0,
76+
}
77+
```
78+
Setting a `processId` of 0 allows a dropdown to select the process of the provider.
79+
80+
2. Add a sleep call (e.g. `time.Sleep(15 * time.Second)`) in the [func providerConfigure(p *schema.Provider](https://github.com/integrations/terraform-provider-github/blob/main/github/provider.go#L176) before the immediate `return` call. This will allow time to connect the debugger while the provider is initializing, before any critical logic happens.
81+
82+
2. Build the terraform provider with debug flags enabled and copy it to a bin folder with a command like `go build -gcflags="all=-N -l" -o ~/go/bin`.
83+
84+
3. Create or edit a `dev.tfrc` that points toward the newly-built binary, and export the `TF_CLI_CONFIG_FILE` variable to point to it. Further instructions on this process may be found in the [Building the provider](#building-the-provider) section.
85+
86+
4. Run a terraform command (e.g. `terraform apply`). While the provider pauses on initialization, go to VSCode and click "Attach to Process". In the search box that appears, type `terraform-provi` and select the terraform provider process.
87+
88+
5. The debugger is now connected! During a typical terraform command, the plugin may be invoked multiple times. If the debugger disconnects and the plugin is invoked again later in the run, the developer will have to re-attach each time as the process ID changes.
89+
90+
6291
## Automated And Manual Testing
6392

6493
### Overview

0 commit comments

Comments
 (0)