Skip to content

Commit

Permalink
config/module: disallow root modules named "root"
Browse files Browse the repository at this point in the history
Fixes #11038

This is a **short term fix**.

Terraform core doesn't currently handle root modules named "root" well
because the prefix `[]string{"root"}` has special meaning and Terraform
core [currently] can't disambiguate between the root module and a module
named "root" in the root module.

This PR introduces a short term fix by simply disallowing root modules
named "root". This shouldn't break any BC because since 0.8.0 this
didn't work at all in many broken ways (including crashes).

Longer term, this should be fixed by removing the special prefix at all
and having empty paths be root. I started down this path but the core
changes necessary are far too scary for a patch release. We can aim for
0.9.
  • Loading branch information
mitchellh committed Jan 8, 2017
1 parent 4fd1035 commit 095b7e7
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Empty
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module "root" { source = "./child" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module "child" {
source = "./child"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Empty
3 changes: 3 additions & 0 deletions config/module/test-fixtures/validate-module-root/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module "root" {
source = "./child"
}
8 changes: 8 additions & 0 deletions config/module/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,14 @@ func (t *Tree) Validate() error {
// If something goes wrong, here is our error template
newErr := &TreeError{Name: []string{t.Name()}}

// Terraform core does not handle root module children named "root".
// We plan to fix this in the future but this bug was brought up in
// the middle of a release and we don't want to introduce wide-sweeping
// changes at that time.
if len(t.path) == 1 && t.name == "root" {
return fmt.Errorf("root module cannot contain module named 'root'")
}

// Validate our configuration first.
if err := t.config.Validate(); err != nil {
newErr.Err = err
Expand Down
12 changes: 12 additions & 0 deletions config/module/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,18 @@ func TestTreeValidate_table(t *testing.T) {
"validate-alias-bad",
"alias must be defined",
},

{
"root module named root",
"validate-module-root",
"cannot contain module",
},

{
"grandchild module named root",
"validate-module-root-grandchild",
"",
},
}

for i, tc := range cases {
Expand Down

0 comments on commit 095b7e7

Please sign in to comment.