Skip to content

Commit

Permalink
3274 - fix: Include nothing when value for terragrunt-include-dir d…
Browse files Browse the repository at this point in the history
…oesn't exist on the filesystem (#3286)

* 3274 - fix: Include nothing when value for `terragrunt-include-dir` doesn't exist on the filesystem

* 3274 - fix: Manually set `ExcludeByDefault` for unit test
  • Loading branch information
yhakbar authored Jul 23, 2024
1 parent 358ec76 commit 66333d4
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 2 deletions.
5 changes: 5 additions & 0 deletions cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,11 @@ func initialSetup(cliCtx *cli.Context, opts *options.TerragruntOptions) error {
return err
}

if len(opts.IncludeDirs) > 0 {
opts.Logger.Debugf("Included directories set. Excluding by default.")
opts.ExcludeByDefault = true
}

opts.IncludeDirs, err = util.GlobCanonicalPath(opts.WorkingDir, opts.IncludeDirs...)
if err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions configstack/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,9 @@ func (modules TerraformModules) flagExcludedDirs(terragruntOptions *options.Terr

// flagIncludedDirs iterates over a module slice and flags all entries not in the list specified via the terragrunt-include-dir CLI flag as excluded.
func (modules TerraformModules) flagIncludedDirs(terragruntOptions *options.TerragruntOptions) TerraformModules {
// If no IncludeDirs is specified return the modules list instantly
if len(terragruntOptions.IncludeDirs) == 0 {
// If we're not excluding by default, we should include everything by default.
// This can happen when a user doesn't set include flags.
if !terragruntOptions.ExcludeByDefault {
// If we aren't given any include directories, but are given the strict include flag,
// return no modules.
if terragruntOptions.StrictInclude {
Expand Down
1 change: 1 addition & 0 deletions configstack/stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,7 @@ func TestResolveTerraformModulesTwoModulesWithDependenciesIncludedDirsWithNoDepe

opts, _ := options.NewTerragruntOptionsForTest("running_module_test")
opts.IncludeDirs = []string{canonical(t, "../test/fixture-modules/module-a")}
opts.ExcludeByDefault = true

moduleA := &TerraformModule{
Path: canonical(t, "../test/fixture-modules/module-a"),
Expand Down
5 changes: 5 additions & 0 deletions options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ type TerragruntOptions struct {
// Unix-style glob of directories to include when running *-all commands
IncludeDirs []string

// If set to true, exclude all directories by default when running *-all commands
// Is set automatically if IncludeDirs is set
ExcludeByDefault bool

// If set to true, do not include dependencies when processing IncludeDirs (unless they are in the included dirs)
StrictInclude bool

Expand Down Expand Up @@ -529,6 +533,7 @@ func (opts *TerragruntOptions) Clone(terragruntConfigPath string) *TerragruntOpt
ExcludesFile: opts.ExcludesFile,
ExcludeDirs: opts.ExcludeDirs,
IncludeDirs: opts.IncludeDirs,
ExcludeByDefault: opts.ExcludeByDefault,
ModulesThatInclude: opts.ModulesThatInclude,
Parallelism: opts.Parallelism,
StrictInclude: opts.StrictInclude,
Expand Down
1 change: 1 addition & 0 deletions test/integration_download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ func TestIncludeDirs(t *testing.T) {
includeArgs string
includedModuleOutputs []string
}{
{testFixtureLocalWithIncludeDir, "--terragrunt-include-dir xyz", []string{}},
{testFixtureLocalWithIncludeDir, "--terragrunt-include-dir */aws", []string{"Module GCE B", "Module GCE C", "Module GCE E"}},
{testFixtureLocalWithIncludeDir, "--terragrunt-include-dir production-env --terragrunt-include-dir **/module-gce-c", []string{"Module GCE B", "Module AWS A"}},
{testFixtureLocalWithIncludeDir, "--terragrunt-include-dir integration-env/gce/module-gce-b --terragrunt-include-dir integration-env/gce/module-gce-c --terragrunt-include-dir **/module-aws*", []string{"Module GCE E"}},
Expand Down

0 comments on commit 66333d4

Please sign in to comment.