Skip to content

Commit 7928da3

Browse files
committed
configs: Fix module loader nil pointer panic
In configurations which have already been initialized, updating the source of a non-root module call to an invalid value could cause a nil pointer panic. This commit fixes the bug and adds test coverage.
1 parent 1fd140f commit 7928da3

File tree

7 files changed

+38
-2
lines changed

7 files changed

+38
-2
lines changed

internal/configs/configload/loader_load.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (l *Loader) moduleWalkerLoad(req *configs.ModuleRequest) (*configs.Module,
6161
var diags hcl.Diagnostics
6262

6363
// Check for inconsistencies between manifest and config
64-
if req.SourceAddr.String() != record.SourceAddr {
64+
if req.SourceAddr != nil && req.SourceAddr.String() != record.SourceAddr {
6565
diags = append(diags, &hcl.Diagnostic{
6666
Severity: hcl.DiagError,
6767
Summary: "Module source has changed",

internal/configs/configload/loader_snapshot_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package configload
22

33
import (
4+
"os"
45
"path/filepath"
56
"reflect"
67
"testing"
@@ -71,6 +72,31 @@ module "child_b" {
7172

7273
}
7374

75+
func TestLoadConfigWithSnapshot_invalidSource(t *testing.T) {
76+
fixtureDir := filepath.Clean("testdata/already-installed-now-invalid")
77+
78+
old, _ := os.Getwd()
79+
os.Chdir(fixtureDir)
80+
defer os.Chdir(old)
81+
82+
loader, err := NewLoader(&Config{
83+
ModulesDir: ".terraform/modules",
84+
})
85+
if err != nil {
86+
t.Fatalf("unexpected error from NewLoader: %s", err)
87+
}
88+
89+
_, _, diags := loader.LoadConfigWithSnapshot(".")
90+
assertDiagnosticCount(t, diags, 2)
91+
d := diags[0]
92+
if got, want := d.Summary, "Variables not allowed"; got != want {
93+
t.Errorf("Wrong diag summary\ngot: %s\nwant: %s", got, want)
94+
}
95+
if got, want := d.Subject.Filename, "foo/main.tf"; got != want {
96+
t.Errorf("Wrong diag filename\ngot: %s\nwant: %s", got, want)
97+
}
98+
}
99+
74100
func TestSnapshotRoundtrip(t *testing.T) {
75101
fixtureDir := filepath.Clean("testdata/already-installed")
76102
loader, err := NewLoader(&Config{

internal/configs/configload/loader_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func assertNoDiagnostics(t *testing.T, diags hcl.Diagnostics) bool {
1414

1515
func assertDiagnosticCount(t *testing.T, diags hcl.Diagnostics, want int) bool {
1616
t.Helper()
17-
if len(diags) != 0 {
17+
if len(diags) != want {
1818
t.Errorf("wrong number of diagnostics %d; want %d", len(diags), want)
1919
for _, diag := range diags {
2020
t.Logf("- %s", diag)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"Modules":[{"Key":"","Source":"","Dir":"."},{"Key":"foo","Source":"./foo","Dir":"foo"},{"Key":"foo.bar","Source":"./bar","Dir":"foo/bar"}]}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "hello" {
2+
value = "Hello from foo/bar"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module "bar" {
2+
source = "${path.module}/bar"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module "foo" {
2+
source = "./foo"
3+
}

0 commit comments

Comments
 (0)