From 7fd7b844d0407ba4305842be4eecc07d80976193 Mon Sep 17 00:00:00 2001 From: Daniel Schmidt Date: Thu, 17 Jul 2025 18:37:45 +0200 Subject: [PATCH] use hcl diagnostic Co-authored-by: Liam Cervante --- internal/lang/eval.go | 7 +++- .../terraform/context_plan_actions_test.go | 35 +++++++++++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/internal/lang/eval.go b/internal/lang/eval.go index 65e3a13260..706a552a6b 100644 --- a/internal/lang/eval.go +++ b/internal/lang/eval.go @@ -440,7 +440,12 @@ func (s *Scope) evalContext(refs []*addrs.Reference, selfAddr addrs.Referenceabl // Actions can not be accessed. case addrs.Action: - return nil, diags.Append(tfdiags.Sourceless(tfdiags.Error, "Can not access actions", fmt.Sprintf("Actions can not be accessed, they have no state and can only be referenced with a resources lifecycle action_trigger events list. Tried to access %s.", subj.String()))) + return nil, diags.Append(&hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "Invalid reference", + Detail: "Actions can't be referenced in this context, they can only be referenced from within a resources lifecycle events list.", + Subject: rng.ToHCL().Ptr(), + }) default: // Should never happen diff --git a/internal/terraform/context_plan_actions_test.go b/internal/terraform/context_plan_actions_test.go index 92fad63806..55417a7b57 100644 --- a/internal/terraform/context_plan_actions_test.go +++ b/internal/terraform/context_plan_actions_test.go @@ -581,7 +581,17 @@ resource "test_object" "a" { `, }, expectValidateDiagnostics: func(m *configs.Config) tfdiags.Diagnostics { - return tfdiags.Diagnostics{tfdiags.Sourceless(tfdiags.Error, "Can not access actions", "Actions can not be accessed, they have no state and can only be referenced with a resources lifecycle action_trigger events list. Tried to access action.test_unlinked.my_action.")} + return tfdiags.Diagnostics{}.Append( + &hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "Invalid reference", + Detail: "Actions can't be referenced in this context, they can only be referenced from within a resources lifecycle events list.", + Subject: &hcl.Range{ + Filename: filepath.Join(m.Module.SourceDir, "main.tf"), + Start: hcl.Pos{Line: 9, Column: 10, Byte: 150}, + End: hcl.Pos{Line: 9, Column: 40, Byte: 180}, + }, + }) }, }, @@ -613,7 +623,28 @@ output "my_output2" { `, }, expectValidateDiagnostics: func(m *configs.Config) tfdiags.Diagnostics { - return tfdiags.Diagnostics{tfdiags.Sourceless(tfdiags.Error, "Can not access actions", "Actions can not be accessed, they have no state and can only be referenced with a resources lifecycle action_trigger events list. Tried to access action.test_unlinked.my_action."), tfdiags.Sourceless(tfdiags.Error, "Can not access actions", "Actions can not be accessed, they have no state and can only be referenced with a resources lifecycle action_trigger events list. Tried to access action.test_unlinked.my_action.")} + return tfdiags.Diagnostics{}.Append( + &hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "Invalid reference", + Detail: "Actions can't be referenced in this context, they can only be referenced from within a resources lifecycle events list.", + Subject: &hcl.Range{ + Filename: filepath.Join(m.Module.SourceDir, "main.tf"), + Start: hcl.Pos{Line: 22, Column: 13, Byte: 375}, + End: hcl.Pos{Line: 22, Column: 43, Byte: 405}, + }, + }).Append( + &hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "Invalid reference", + Detail: "Actions can't be referenced in this context, they can only be referenced from within a resources lifecycle events list.", + Subject: &hcl.Range{ + Filename: filepath.Join(m.Module.SourceDir, "main.tf"), + Start: hcl.Pos{Line: 18, Column: 13, Byte: 302}, + End: hcl.Pos{Line: 18, Column: 43, Byte: 332}, + }, + }, + ) }, }, } {