From 530f2b30ef12b7d6985ef1fa9011cee24e8e19b7 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 11 Feb 2026 14:59:43 +0100 Subject: [PATCH] Backport of Colorize diag details into v1.14 (#38148) --- internal/command/format/diagnostic.go | 4 +- internal/command/init_test.go | 63 +++++++++++++++++++ .../testdata/init-cloud-no-workspaces/main.tf | 10 +++ 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 internal/command/testdata/init-cloud-no-workspaces/main.tf diff --git a/internal/command/format/diagnostic.go b/internal/command/format/diagnostic.go index 5915f26bdf..d8f34ffc19 100644 --- a/internal/command/format/diagnostic.go +++ b/internal/command/format/diagnostic.go @@ -88,7 +88,7 @@ func DiagnosticFromJSON(diag *viewsjson.Diagnostic, color *colorstring.Colorize, lines := strings.Split(diag.Detail, "\n") for _, line := range lines { if !strings.HasPrefix(line, " ") { - line = wordwrap.WrapString(line, uint(paraWidth)) + line = wordwrap.WrapString(color.Color(line), uint(paraWidth)) } fmt.Fprintf(&buf, "%s\n", line) } @@ -162,7 +162,7 @@ func DiagnosticPlainFromJSON(diag *viewsjson.Diagnostic, width int) string { lines := strings.Split(diag.Detail, "\n") for _, line := range lines { if !strings.HasPrefix(line, " ") { - line = wordwrap.WrapString(line, uint(width-1)) + line = wordwrap.WrapString(disabledColorize.Color(line), uint(width-1)) } fmt.Fprintf(&buf, "%s\n", line) } diff --git a/internal/command/init_test.go b/internal/command/init_test.go index 6c266b9b8e..edbe6f9ab0 100644 --- a/internal/command/init_test.go +++ b/internal/command/init_test.go @@ -1552,6 +1552,69 @@ prompts. } +func TestInit_cloudConfigColorTokensProcessed(t *testing.T) { + // This test verifies that when the error + // diagnostic detail contains color formatting tokens like [bold] and + // [reset], they are properly processed + // by the diagnostic formatter and do not appear as literal text in the + // output. + td := t.TempDir() + testCopyDir(t, testFixturePath("init-cloud-no-workspaces"), td) + t.Chdir(td) + + ui := cli.NewMockUi() + view, done := testView(t) + c := &InitCommand{ + Meta: Meta{ + Ui: ui, + View: view, + }, + } + + args := []string{} + code := c.Run(args) + if code == 0 { + t.Fatalf("expected error, got success\n%s", done(t).Stdout()) + } + + gotStderr := done(t).Stderr() + + expected := ` +Error: failed to create backend alias to target "". The hostname is not in the correct format. + + +Error: Invalid workspaces configuration + + on main.tf line 7, in terraform: + 7: cloud { + +Missing workspace mapping strategy. Either workspace "tags" or "name" is +required. + +The 'workspaces' block configures how Terraform CLI maps its workspaces for +this single +configuration to workspaces within an HCP Terraform or Terraform Enterprise +organization. Two strategies are available: + +tags - A set of tags used to select remote HCP Terraform or Terraform +Enterprise workspaces to be used for this single +configuration. New workspaces will automatically be tagged with these tag +values. Generally, this +is the primary and recommended strategy to use. This option conflicts with +"name". + +name - The name of a single HCP Terraform or Terraform Enterprise workspace +to be used with this configuration. +When configured, only the specified workspace can be used. This option +conflicts with "tags" +and with the TF_WORKSPACE environment variable. +` + + if diff := cmp.Diff(gotStderr, expected); diff != "" { + t.Errorf("unexpected output (-got +expected):\n%s", diff) + } +} + // make sure inputFalse stops execution on migrate func TestInit_inputFalse(t *testing.T) { td := t.TempDir() diff --git a/internal/command/testdata/init-cloud-no-workspaces/main.tf b/internal/command/testdata/init-cloud-no-workspaces/main.tf new file mode 100644 index 0000000000..880ba213e8 --- /dev/null +++ b/internal/command/testdata/init-cloud-no-workspaces/main.tf @@ -0,0 +1,10 @@ +# This is a configuration with HCP Terraform mode activated but without +# a workspaces block, which should trigger an "Invalid workspaces configuration" +# error during initialization. This is used to test that the diagnostic +# formatting correctly processes color tokens in the error detail message. + +terraform { + cloud { + organization = "PLACEHOLDER" + } +}