Skip to content

Commit

Permalink
fix: detect terragrunt modules without remote_state block (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
leg100 authored Jul 25, 2024
1 parent 361ae18 commit 379bea9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
23 changes: 20 additions & 3 deletions internal/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (m *Module) LogValue() slog.Value {
//
// A root module is deemed to be a directory that contains a .tf file that
// contains a backend or cloud block, or in the case of terragrunt, a
// terragrunt.hcl file containing a remote_state block.
// terragrunt.hcl file.
func findModules(logger logging.Interface, workdir internal.Workdir) (modules []Options, err error) {
walkfn := func(path string, d fs.DirEntry, walkerr error) error {
if walkerr != nil {
Expand All @@ -81,15 +81,31 @@ func findModules(logger logging.Interface, workdir internal.Workdir) (modules []
}
return nil
}
if filepath.Ext(path) == ".tf" || d.Name() == "terragrunt.hcl" {

var isTerragrunt bool
switch {
case d.Name() == "terragrunt.hcl":
isTerragrunt = true
fallthrough
case filepath.Ext(path) == ".tf":
backend, found, err := detectBackend(path)
if err != nil {
logger.Error("reloading modules: parsing hcl", "path", path, "error", err)
return nil
}
if !found {
if !isTerragrunt && !found {
// Not a terragrunt module, nor a vanilla terraform module with a
// backend config, so skip.
return nil
}
if isTerragrunt && backend == "" {
// Unless terragrunt.hcl directly contains a `remote_state`
// block then Pug doesn't have a way of determining the backend
// type (not unless it evaluates terragrunt's language and
// follows `find_in_parent` etc. to locate the effective
// remote_state, which is perhaps a future exercise...).
logger.Warn("reloading modules: could not determine backend type", "path", path)
}
// Strip workdir from module path
stripped, err := filepath.Rel(workdir.String(), filepath.Dir(path))
if err != nil {
Expand All @@ -102,6 +118,7 @@ func findModules(logger logging.Interface, workdir internal.Workdir) (modules []
// skip walking remainder of parent directory
return fs.SkipDir
}

return nil
}
if err := filepath.WalkDir(workdir.String(), walkfn); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/module/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ func TestFindModules(t *testing.T) {
got, err := findModules(logging.Discard, workdir)
require.NoError(t, err)

assert.Equal(t, 4, len(got), got)
assert.Equal(t, 5, len(got), got)
assert.Contains(t, got, Options{Path: "with_local_backend", Backend: "local"})
assert.Contains(t, got, Options{Path: "with_s3_backend", Backend: "s3"})
assert.Contains(t, got, Options{Path: "with_cloud_backend", Backend: "cloud"})
assert.Contains(t, got, Options{Path: "terragrunt_with_local", Backend: "local"})
assert.Contains(t, got, Options{Path: "terragrunt_without_backend", Backend: ""})
assert.NotContains(t, got, "broken")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
include {
path = find_in_parent_folders("root.hcl")
}

0 comments on commit 379bea9

Please sign in to comment.