|
1 | 1 | package types |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/aquasecurity/trivy/pkg/iac/terraform" |
4 | 7 | "github.com/hashicorp/hcl/v2" |
5 | 8 | "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 9 | + "github.com/zclconf/go-cty/cty" |
| 10 | + hcty "github.com/hashicorp/go-cty/cty" |
| 11 | + hctyjson "github.com/hashicorp/go-cty/cty/json" |
| 12 | + ctyjson "github.com/zclconf/go-cty/cty/json" |
6 | 13 |
|
7 | 14 | "github.com/coder/terraform-provider-coder/v2/provider" |
8 | 15 | ) |
@@ -44,27 +51,42 @@ func providerOption(opt *ParameterOption) provider.Option { |
44 | 51 | } |
45 | 52 | } |
46 | 53 |
|
47 | | -func hclDiagnostics(diagnostics diag.Diagnostics) hcl.Diagnostics { |
| 54 | +func hclDiagnostics(diagnostics diag.Diagnostics, source *terraform.Block) hcl.Diagnostics { |
48 | 55 | cpy := make(hcl.Diagnostics, 0, len(diagnostics)) |
49 | 56 | for _, d := range diagnostics { |
50 | | - cpy = append(cpy, hclDiagnostic(d)) |
| 57 | + cpy = append(cpy, hclDiagnostic(d, source)) |
51 | 58 | } |
52 | 59 | return cpy |
53 | 60 | } |
54 | 61 |
|
55 | | -func hclDiagnostic(d diag.Diagnostic) *hcl.Diagnostic { |
| 62 | +func hclDiagnostic(d diag.Diagnostic, source *terraform.Block) *hcl.Diagnostic { |
56 | 63 | sev := hcl.DiagInvalid |
57 | 64 | switch d.Severity { |
58 | 65 | case diag.Error: |
59 | 66 | sev = hcl.DiagError |
60 | 67 | case diag.Warning: |
61 | 68 | sev = hcl.DiagWarning |
62 | 69 | } |
| 70 | + |
| 71 | + // This is an imperfect way to finding the source code of the error. There is 2 |
| 72 | + // different `cty` types at place here, the hashicorp fork and the original. So a |
| 73 | + // more general solution is difficult. This is good enough for now to add more |
| 74 | + // context to an error. |
| 75 | + var subject *hcl.Range |
| 76 | + if len(d.AttributePath) == 1 && source != nil { |
| 77 | + if attr, ok := d.AttributePath[0].(hcty.GetAttrStep); ok { |
| 78 | + src := source.GetAttribute(attr.Name) |
| 79 | + if src != nil { |
| 80 | + subject = &(src.HCLAttribute().Range) |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
63 | 85 | return &hcl.Diagnostic{ |
64 | 86 | Severity: sev, |
65 | 87 | Summary: d.Summary, |
66 | 88 | Detail: d.Detail, |
67 | | - Subject: nil, |
| 89 | + Subject: subject, |
68 | 90 | Context: nil, |
69 | 91 | Expression: nil, |
70 | 92 | EvalContext: nil, |
|
0 commit comments