From fcfbd85aa5e1d981153852961c5e6f0e6462e98c Mon Sep 17 00:00:00 2001 From: James Bardin Date: Tue, 9 Jul 2024 15:06:27 -0400 Subject: [PATCH] add suggestion to run `terraform providers` On a default provider install failure we can always suggest running `terraform providers` to help in cases where it's not apparent to the user how to proceed. This would most commonly be encountered by conflicting provider requirements, but if we find there are other common situations we may want to generate a more precise error type for better diagnostics. --- internal/command/init.go | 5 +++-- internal/command/init_test.go | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/internal/command/init.go b/internal/command/init.go index 8c44b67e9f..89ab5746de 100644 --- a/internal/command/init.go +++ b/internal/command/init.go @@ -686,11 +686,12 @@ func (c *InitCommand) getProviders(ctx context.Context, config *configs.Config, // the end, by checking ctx.Err(). default: + suggestion := fmt.Sprintf("\n\nTo see which modules are currently depending on %s and what versions are specified, run the following command:\n terraform providers", provider.ForDisplay()) diags = diags.Append(tfdiags.Sourceless( tfdiags.Error, "Failed to query available provider packages", - fmt.Sprintf("Could not retrieve the list of available versions for provider %s: %s", - provider.ForDisplay(), err, + fmt.Sprintf("Could not retrieve the list of available versions for provider %s: %s%s", + provider.ForDisplay(), err, suggestion, ), )) } diff --git a/internal/command/init_test.go b/internal/command/init_test.go index 5f51b1bc45..55e8a46568 100644 --- a/internal/command/init_test.go +++ b/internal/command/init_test.go @@ -2939,6 +2939,10 @@ Error: Failed to query available provider packages Could not retrieve the list of available versions for provider hashicorp/test: no available releases match the given constraints 1.0.1, 1.0.2 + +To see which modules are currently depending on hashicorp/test and what +versions are specified, run the following command: + terraform providers ` if diff := cmp.Diff(got, want); len(diff) > 0 { t.Fatalf("wrong error message: \ngot:\n%s\nwant:\n%s\ndiff:\n%s", got, want, diff)