-
Notifications
You must be signed in to change notification settings - Fork 376
Description
Introduction
Currently, TFLint will stop immediately and return an error if an error occurs on the way:
$ tflint
Failed to check ruleset; Failed to check `azurerm_lb_probe_invalid_protocol` rule: main.tf:6,53-72: Invalid template interpolation value; The expression result is null. Cannot include a null value in a string template.
However, if multiple problems exist, you need to run TFLint multiple times until all errors are resolved. This is inefficient.
Fortunately, HCL parse errors (hcl.Diagnostics
) already support multiple error handling:
Lines 93 to 97 in 16e0218
if errors.As(err, &diags) { | |
fmt.Fprintf(f.Stderr, "%s:\n\n", err) | |
writer := hcl.NewDiagnosticTextWriter(f.Stderr, parseSources(sources), 0, !f.NoColor) | |
_ = writer.WriteDiagnostics(diags) |
This proposal mainly focuses on multiple error handling otherwise (especially errors returned by plugins).
See also #1665
Proposal
Change the plugin interface to allow multiple errors to be returned. google.golang.org/grpc/status.WithDetails
allows us to attach multiple pieces of information to the status code, so we might be able to take advantage of this. FYI, the SDK already uses this mechanism to propagate application errors:
https://github.com/terraform-linters/tflint-plugin-sdk/blob/v0.15.0/plugin/toproto/toproto.go#L226-L254
https://github.com/terraform-linters/tflint-plugin-sdk/blob/v0.15.0/plugin/fromproto/fromproto.go#L283-L321
When the Check
returns an error, the SDK follows a convention to split it into multiple errors and turn them into status details. As the convention, errors.Join
, introduced in Go 1.20, is a good option.
https://github.com/terraform-linters/tflint-plugin-sdk/blob/v0.15.0/plugin/host2plugin/server.go#L119
Consideration should be given to how hcl.Diagnostics
should be treated. hcl.Diagnostics
can already handle multiple errors, but not all contexts over the wire protocol (e.g. EvalContext
, Extra
). Maybe we should invent our own error representation like tfdiags
.
References
- https://pkg.go.dev/github.com/terraform-linters/tflint-plugin-sdk@v0.15.0/plugin/toproto
- https://pkg.go.dev/github.com/terraform-linters/tflint-plugin-sdk@v0.15.0/plugin/fromproto
- https://pkg.go.dev/google.golang.org/grpc@v1.52.3/internal/status#Status.WithDetails
- https://pkg.go.dev/errors#Join
- https://github.com/hashicorp/hcl/blob/v2.16.0/diagnostic.go
- https://github.com/hashicorp/terraform/tree/v1.3.7/internal/tfdiags