From 655f18c3936fc84780ee9246635f0f40323fa69a Mon Sep 17 00:00:00 2001 From: James Bardin Date: Fri, 7 May 2021 11:52:06 -0400 Subject: [PATCH 1/2] don't build a config if it didn't load properly --- configs/configload/loader_load.go | 2 +- configs/configload/loader_load_test.go | 16 ++++++++++++++++ .../configload/testdata/invalid-names/main.tf | 3 +++ 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 configs/configload/testdata/invalid-names/main.tf diff --git a/configs/configload/loader_load.go b/configs/configload/loader_load.go index e3c9bdca6..8f5f48a99 100644 --- a/configs/configload/loader_load.go +++ b/configs/configload/loader_load.go @@ -20,7 +20,7 @@ import ( // required to process the individual modules func (l *Loader) LoadConfig(rootDir string) (*configs.Config, hcl.Diagnostics) { rootMod, diags := l.parser.LoadConfigDir(rootDir) - if rootMod == nil { + if rootMod == nil || diags.HasErrors() { return nil, diags } diff --git a/configs/configload/loader_load_test.go b/configs/configload/loader_load_test.go index 845b227a3..60fef238d 100644 --- a/configs/configload/loader_load_test.go +++ b/configs/configload/loader_load_test.go @@ -80,3 +80,19 @@ func TestLoaderLoadConfig_addVersion(t *testing.T) { t.Fatalf("wrong error\ngot:\n%s\n\nwant: containing %q", got, want) } } + +func TestLoaderLoadConfig_loadDiags(t *testing.T) { + // building a config which didn't load correctly may cause configs to panic + fixtureDir := filepath.Clean("testdata/invalid-names") + loader, err := NewLoader(&Config{ + ModulesDir: filepath.Join(fixtureDir, ".terraform/modules"), + }) + if err != nil { + t.Fatalf("unexpected error from NewLoader: %s", err) + } + + _, diags := loader.LoadConfig(fixtureDir) + if !diags.HasErrors() { + t.Fatalf("success; want error") + } +} diff --git a/configs/configload/testdata/invalid-names/main.tf b/configs/configload/testdata/invalid-names/main.tf new file mode 100644 index 000000000..d4eee4c3e --- /dev/null +++ b/configs/configload/testdata/invalid-names/main.tf @@ -0,0 +1,3 @@ +provider "42_bad!" { + invalid_provider_name = "yes" +} From ced676d50dc922b303b15fa40428da921ca8c617 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Fri, 7 May 2021 12:28:09 -0400 Subject: [PATCH 2/2] fixed configloader doesn't add unnecessary diags --- .../incorrectmodulename/output.json | 54 +------------------ .../multiple_modules/output.json | 28 +--------- 2 files changed, 2 insertions(+), 80 deletions(-) diff --git a/command/testdata/validate-invalid/incorrectmodulename/output.json b/command/testdata/validate-invalid/incorrectmodulename/output.json index 4dfa754b6..0c2ce68ab 100644 --- a/command/testdata/validate-invalid/incorrectmodulename/output.json +++ b/command/testdata/validate-invalid/incorrectmodulename/output.json @@ -1,7 +1,7 @@ { "format_version": "0.1", "valid": false, - "error_count": 6, + "error_count": 4, "warning_count": 0, "diagnostics": [ { @@ -107,58 +107,6 @@ "highlight_end_offset": 25, "values": [] } - }, - { - "severity": "error", - "summary": "Module not installed", - "detail": "This module is not yet installed. Run \"terraform init\" to install all modules required by this configuration.", - "range": { - "filename": "testdata/validate-invalid/incorrectmodulename/main.tf", - "start": { - "line": 4, - "column": 1, - "byte": 27 - }, - "end": { - "line": 4, - "column": 15, - "byte": 41 - } - }, - "snippet": { - "context": null, - "code": "module \"super\" {", - "start_line": 4, - "highlight_start_offset": 0, - "highlight_end_offset": 14, - "values": [] - } - }, - { - "severity": "error", - "summary": "Module not installed", - "detail": "This module is not yet installed. Run \"terraform init\" to install all modules required by this configuration.", - "range": { - "filename": "testdata/validate-invalid/incorrectmodulename/main.tf", - "start": { - "line": 1, - "column": 1, - "byte": 0 - }, - "end": { - "line": 1, - "column": 22, - "byte": 21 - } - }, - "snippet": { - "context": null, - "code": "module \"super#module\" {", - "start_line": 1, - "highlight_start_offset": 0, - "highlight_end_offset": 21, - "values": [] - } } ] } diff --git a/command/testdata/validate-invalid/multiple_modules/output.json b/command/testdata/validate-invalid/multiple_modules/output.json index a18c6bc78..4cd6dfb9f 100644 --- a/command/testdata/validate-invalid/multiple_modules/output.json +++ b/command/testdata/validate-invalid/multiple_modules/output.json @@ -1,7 +1,7 @@ { "format_version": "0.1", "valid": false, - "error_count": 2, + "error_count": 1, "warning_count": 0, "diagnostics": [ { @@ -29,32 +29,6 @@ "highlight_end_offset": 21, "values": [] } - }, - { - "severity": "error", - "summary": "Module not installed", - "detail": "This module is not yet installed. Run \"terraform init\" to install all modules required by this configuration.", - "range": { - "filename": "testdata/validate-invalid/multiple_modules/main.tf", - "start": { - "line": 5, - "column": 1, - "byte": 46 - }, - "end": { - "line": 5, - "column": 22, - "byte": 67 - } - }, - "snippet": { - "context": null, - "code": "module \"multi_module\" {", - "start_line": 5, - "highlight_start_offset": 0, - "highlight_end_offset": 21, - "values": [] - } } ] }