From 703f024cbd0af9d284cf84e0a9c4873b84197c13 Mon Sep 17 00:00:00 2001 From: Kristin Laemmert Date: Mon, 3 Jun 2019 11:19:03 -0400 Subject: [PATCH 1/2] command/show (-json): fix panic if a moduleCall has a nil config In the unlikely event that a moduleCall has a nil config - for example, if a nested module call includes a variable with a typo in an attribute - continue gracefully. --- command/jsonconfig/config.go | 6 +++++ .../show-json/nested-modules/main.tf | 3 +++ .../show-json/nested-modules/modules/main.tf | 3 +++ .../modules/more-modules/main.tf | 4 ++++ .../show-json/nested-modules/output.json | 23 +++++++++++++++++++ 5 files changed, 39 insertions(+) create mode 100644 command/test-fixtures/show-json/nested-modules/main.tf create mode 100644 command/test-fixtures/show-json/nested-modules/modules/main.tf create mode 100644 command/test-fixtures/show-json/nested-modules/modules/more-modules/main.tf create mode 100644 command/test-fixtures/show-json/nested-modules/output.json diff --git a/command/jsonconfig/config.go b/command/jsonconfig/config.go index ecfa12c5e9..42f67364ba 100644 --- a/command/jsonconfig/config.go +++ b/command/jsonconfig/config.go @@ -238,6 +238,12 @@ func marshalModuleCalls(c *configs.Config, schemas *terraform.Schemas) map[strin } func marshalModuleCall(c *configs.Config, mc *configs.ModuleCall, schemas *terraform.Schemas) moduleCall { + // It's possible, though unlikely, to have a module call with a nil config + // https://github.com/hashicorp/terraform/issues/21543 + if c == nil { + return moduleCall{} + } + ret := moduleCall{ Source: mc.SourceAddr, VersionConstraint: mc.Version.Required.String(), diff --git a/command/test-fixtures/show-json/nested-modules/main.tf b/command/test-fixtures/show-json/nested-modules/main.tf new file mode 100644 index 0000000000..ef0bad2bbc --- /dev/null +++ b/command/test-fixtures/show-json/nested-modules/main.tf @@ -0,0 +1,3 @@ +module "my_module" { + source = "./modules" +} diff --git a/command/test-fixtures/show-json/nested-modules/modules/main.tf b/command/test-fixtures/show-json/nested-modules/modules/main.tf new file mode 100644 index 0000000000..990155ecb3 --- /dev/null +++ b/command/test-fixtures/show-json/nested-modules/modules/main.tf @@ -0,0 +1,3 @@ +module "more" { + source = "./more-modules" +} diff --git a/command/test-fixtures/show-json/nested-modules/modules/more-modules/main.tf b/command/test-fixtures/show-json/nested-modules/modules/more-modules/main.tf new file mode 100644 index 0000000000..488a271930 --- /dev/null +++ b/command/test-fixtures/show-json/nested-modules/modules/more-modules/main.tf @@ -0,0 +1,4 @@ +variable "misspelled" { + default = "ehllo" + descriptoni = "I am a misspelled attribute" +} diff --git a/command/test-fixtures/show-json/nested-modules/output.json b/command/test-fixtures/show-json/nested-modules/output.json new file mode 100644 index 0000000000..75f15466e7 --- /dev/null +++ b/command/test-fixtures/show-json/nested-modules/output.json @@ -0,0 +1,23 @@ +{ + "format_version": "0.1", + "terraform_version": "0.12.1-dev", + "planned_values": { + "root_module": {} + }, + "configuration": { + "root_module": { + "module_calls": { + "my_module": { + "source": "./modules", + "module": { + "module_calls": { + "more": { + "module": {} + } + } + } + } + } + } + } +} \ No newline at end of file From 06a333331679e34ce0338695556c30e7019abc73 Mon Sep 17 00:00:00 2001 From: Kristin Laemmert Date: Mon, 3 Jun 2019 14:00:46 -0400 Subject: [PATCH 2/2] note: this is an unusual edgecase in the category of "probably should not happen" If https://github.com/hashicorp/terraform/issues/21543 is fixed, we can remove this check. --- command/jsonconfig/config.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/command/jsonconfig/config.go b/command/jsonconfig/config.go index 42f67364ba..0b27c7af4b 100644 --- a/command/jsonconfig/config.go +++ b/command/jsonconfig/config.go @@ -238,8 +238,7 @@ func marshalModuleCalls(c *configs.Config, schemas *terraform.Schemas) map[strin } func marshalModuleCall(c *configs.Config, mc *configs.ModuleCall, schemas *terraform.Schemas) moduleCall { - // It's possible, though unlikely, to have a module call with a nil config - // https://github.com/hashicorp/terraform/issues/21543 + // It is possible to have a module call with a nil config. if c == nil { return moduleCall{} }