From 470be8c2b8ec8c7af4632396f68b1957f0ce9642 Mon Sep 17 00:00:00 2001 From: Komal Date: Thu, 10 Jun 2021 19:24:15 -0700 Subject: [PATCH] [cli/import] - Handle panic during import codegen (#7265) --- CHANGELOG_PENDING.md | 3 +++ pkg/cmd/pulumi/import.go | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/CHANGELOG_PENDING.md b/CHANGELOG_PENDING.md index ad912284e..a9a56fe27 100644 --- a/CHANGELOG_PENDING.md +++ b/CHANGELOG_PENDING.md @@ -8,6 +8,9 @@ - [codegen] - Include properties with an underlying type of string on Go provider instances. +- [cli] - Provide a more helpful error instead of panicking when codegen fails during import. + [#7265](https://github.com/pulumi/pulumi/pull/7265) + ### Bug Fixes - [sdk/python] - Fix regression in behaviour for `Output.from_input({})` diff --git a/pkg/cmd/pulumi/import.go b/pkg/cmd/pulumi/import.go index 0ec3f5c32..2e86e8f91 100644 --- a/pkg/cmd/pulumi/import.go +++ b/pkg/cmd/pulumi/import.go @@ -198,6 +198,20 @@ func generateImportedDefinitions(out io.Writer, stackName tokens.QName, projectN snap *deploy.Snapshot, programGenerator programGeneratorFunc, names importer.NameTable, imports []deploy.Import, protectResources bool) (bool, error) { + defer func() { + v := recover() + if v != nil { + errMsg := strings.Builder{} + errMsg.WriteString("Your resource has been imported into Pulumi state, but there was an error generating the import code.\n") //nolint:lll + errMsg.WriteString("\n") + if strings.Contains(fmt.Sprintf("%v", v), "invalid Go source code:") { + errMsg.WriteString("You will need to copy and paste the generated code into your Pulumi application and manually edit it to correct any errors.\n\n") //nolint:lll + } + errMsg.WriteString(fmt.Sprintf("%v\n", v)) + fmt.Print(errMsg.String()) + } + }() + resourceTable := map[resource.URN]*resource.State{} for _, r := range snap.Resources { if !r.Delete { @@ -499,7 +513,7 @@ func newImportCmd() *cobra.Command { return result.FromError(err) } if protectResources { - _, err := helperWriter.Write([]byte("Please note, that the imported resources are marked as protected. " + + _, err := helperWriter.Write([]byte("Please note that the imported resources are marked as protected. " + "To destroy them\n" + "you will need to remove the `protect` option and run `pulumi update` *before*\n" + "the destroy will take effect.\n\n"))