From fa44937977ddf29f7fe443fb97dcb20b3e9e821e Mon Sep 17 00:00:00 2001 From: Daniel Schmidt Date: Mon, 4 Nov 2024 15:09:24 +0100 Subject: [PATCH] ephemeral: improve diagnostic message through compactValueStr --- internal/backend/local/backend_apply.go | 3 ++- internal/command/views/json/diagnostic.go | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/internal/backend/local/backend_apply.go b/internal/backend/local/backend_apply.go index 5c2fa59b24..60b63bcf50 100644 --- a/internal/backend/local/backend_apply.go +++ b/internal/backend/local/backend_apply.go @@ -16,6 +16,7 @@ import ( "github.com/hashicorp/terraform/internal/addrs" "github.com/hashicorp/terraform/internal/backend/backendrun" "github.com/hashicorp/terraform/internal/command/views" + viewsjson "github.com/hashicorp/terraform/internal/command/views/json" "github.com/hashicorp/terraform/internal/configs" "github.com/hashicorp/terraform/internal/logging" "github.com/hashicorp/terraform/internal/plans" @@ -356,7 +357,7 @@ func (b *Local) opApply( diags = diags.Append(&hcl.Diagnostic{ Severity: hcl.DiagError, Summary: "Can't set variable when applying a saved plan", - Detail: fmt.Sprintf("The variable %s cannot be set using the -var and -var-file options when applying a saved plan file, because a saved plan includes the variable values that were set when it was created. The saved plan specifies %q as the value whereas during apply the value %q was %s. To declare an ephemeral variable which is not saved in the plan file, use ephemeral = true.", varName, v.Value.GoString(), val.GoString(), v.SourceType.DiagnosticLabel()), + Detail: fmt.Sprintf("The variable %s cannot be set using the -var and -var-file options when applying a saved plan file, because a saved plan includes the variable values that were set when it was created. The saved plan specifies %s as the value whereas during apply the value %s was %s. To declare an ephemeral variable which is not saved in the plan file, use ephemeral = true.", varName, viewsjson.CompactValueStr(v.Value), viewsjson.CompactValueStr(val), v.SourceType.DiagnosticLabel()), Subject: rng, }) } diff --git a/internal/command/views/json/diagnostic.go b/internal/command/views/json/diagnostic.go index 51f987b618..4d4032814a 100644 --- a/internal/command/views/json/diagnostic.go +++ b/internal/command/views/json/diagnostic.go @@ -438,13 +438,17 @@ func parseRange(src []byte, rng hcl.Range) (*hcl.File, int) { return file, offset } -// compactValueStr produces a compact, single-line summary of a given value +// CompactValueStr produces a compact, single-line summary of a given value // that is suitable for display in the UI. // // For primitives it returns a full representation, while for more complex // types it instead summarizes the type, size, etc to produce something // that is hopefully still somewhat useful but not as verbose as a rendering // of the entire data structure. +func CompactValueStr(val cty.Value) string { + return compactValueStr(val) +} + func compactValueStr(val cty.Value) string { // This is a specialized subset of value rendering tailored to producing // helpful but concise messages in diagnostics. It is not comprehensive