From 4e3507f8f427e290c14f5742fba4c544e8dccbe8 Mon Sep 17 00:00:00 2001 From: Katy Moe Date: Thu, 17 Feb 2022 11:41:05 +0000 Subject: [PATCH] improve formatting of path in error message --- internal/terraform/node_resource_validate.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/internal/terraform/node_resource_validate.go b/internal/terraform/node_resource_validate.go index 821252119528..92d6bb13203c 100644 --- a/internal/terraform/node_resource_validate.go +++ b/internal/terraform/node_resource_validate.go @@ -2,12 +2,12 @@ package terraform import ( "fmt" + "strings" "github.com/hashicorp/hcl/v2" "github.com/hashicorp/terraform/internal/addrs" "github.com/hashicorp/terraform/internal/configs" "github.com/hashicorp/terraform/internal/configs/configschema" - "github.com/hashicorp/terraform/internal/configs/hcl2shim" "github.com/hashicorp/terraform/internal/didyoumean" "github.com/hashicorp/terraform/internal/providers" "github.com/hashicorp/terraform/internal/provisioners" @@ -395,10 +395,15 @@ func (n *NodeValidatableResource) validateResource(ctx EvalContext) tfdiags.Diag attrSchema := schema.AttributeByPath(path) if attrSchema != nil && !attrSchema.Optional && attrSchema.Computed { + // ignore_changes uses absolute traversal syntax in config despite + // using relative traversals, so we strip the leading "." added by + // FormatCtyPath for a better error message. + attrDisplayPath := strings.TrimPrefix(tfdiags.FormatCtyPath(path), ".") + diags = diags.Append(&hcl.Diagnostic{ Severity: hcl.DiagWarning, Summary: "Invalid ignore_changes", - Detail: fmt.Sprintf("ignore_changes cannot be used with non-Optional Computed attributes. Please remove \"%s\" from ignore_changes.", hcl2shim.FlatmapKeyFromPath(path)), + Detail: fmt.Sprintf("ignore_changes cannot be used with non-Optional Computed attributes. Please remove \"%s\" from ignore_changes.", attrDisplayPath), Subject: &n.Config.TypeRange, })