Skip to content

Commit 4ca126c

Browse files
committed
Fixed discovery flags passing
1 parent abaf03c commit 4ca126c

File tree

4 files changed

+144
-1
lines changed

4 files changed

+144
-1
lines changed

internal/discovery/discovery.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ func (d *Discovery) Discover(ctx context.Context, l log.Logger, opts *options.Te
397397
if !filepath.IsAbs(p) {
398398
p = filepath.Join(d.workingDir, p)
399399
}
400+
400401
includePatterns = append(includePatterns, util.CleanPath(p))
401402
}
402403
}
@@ -406,6 +407,7 @@ func (d *Discovery) Discover(ctx context.Context, l log.Logger, opts *options.Te
406407
if !filepath.IsAbs(p) {
407408
p = filepath.Join(d.workingDir, p)
408409
}
410+
409411
excludePatterns = append(excludePatterns, util.CleanPath(p))
410412
}
411413
}
@@ -421,6 +423,7 @@ func (d *Discovery) Discover(ctx context.Context, l log.Logger, opts *options.Te
421423

422424
// Apply include/exclude filters by directory path first
423425
dir := filepath.Dir(path)
426+
424427
canonicalDir, canErr := util.CanonicalPath(dir, d.workingDir)
425428
if canErr == nil {
426429
for _, pattern := range excludePatterns {
@@ -438,11 +441,13 @@ func (d *Discovery) Discover(ctx context.Context, l log.Logger, opts *options.Te
438441
// Enforce include patterns only when strictInclude or excludeByDefault are set
439442
if d.strictInclude || d.excludeByDefault {
440443
included := false
444+
441445
for _, pattern := range includePatterns {
442446
matched, matchErr := zglob.Match(pattern, canonicalDir)
443447
if matchErr != nil {
444448
l.Debugf("error matching include glob %s against %s: %v", pattern, canonicalDir, matchErr)
445449
}
450+
446451
if matched {
447452
included = true
448453
break
@@ -459,25 +464,29 @@ func (d *Discovery) Discover(ctx context.Context, l log.Logger, opts *options.Te
459464
if !d.hidden && d.isInHiddenDirectory(path) {
460465
// If the directory is hidden, allow it only if it matches an include pattern
461466
allowHidden := false
467+
462468
if canErr == nil {
463469
// Always allow .terragrunt-stack contents
464470
cleanDir := util.CleanPath(canonicalDir)
465471
if strings.Contains(cleanDir, "/"+config.StackDir+"/") || strings.HasSuffix(cleanDir, "/"+config.StackDir) {
466472
allowHidden = true
467473
}
474+
468475
if !allowHidden {
469476
for _, pattern := range includePatterns {
470477
matched, matchErr := zglob.Match(pattern, canonicalDir)
471478
if matchErr != nil {
472479
l.Debugf("error matching include glob %s against %s: %v", pattern, canonicalDir, matchErr)
473480
}
481+
474482
if matched {
475483
allowHidden = true
476484
break
477485
}
478486
}
479487
}
480488
}
489+
481490
if !allowHidden {
482491
return nil
483492
}
@@ -617,11 +626,11 @@ func (d *Discovery) Discover(ctx context.Context, l log.Logger, opts *options.Te
617626
type DependencyDiscovery struct {
618627
discoveryContext *DiscoveryContext
619628
cfgs DiscoveredConfigs
629+
parserOptions []hclparse.Option
620630
depthRemaining int
621631
discoverExternal bool
622632
suppressParseErrors bool
623633
ignoreExternal bool
624-
parserOptions []hclparse.Option
625634
}
626635

627636
// DependencyDiscoveryOption is a function that modifies a DependencyDiscovery.

internal/discovery/discovery_test.go

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,3 +584,134 @@ inputs = {
584584
unitConfig := units[0]
585585
assert.NotNil(t, unitConfig.Parsed, "Unit configuration should be parsed")
586586
}
587+
588+
func TestDiscoveryIncludeExcludeFilters(t *testing.T) {
589+
t.Parallel()
590+
591+
tmpDir := t.TempDir()
592+
593+
unit1Dir := filepath.Join(tmpDir, "unit1")
594+
unit2Dir := filepath.Join(tmpDir, "unit2")
595+
unit3Dir := filepath.Join(tmpDir, "unit3")
596+
597+
for _, d := range []string{unit1Dir, unit2Dir, unit3Dir} {
598+
require.NoError(t, os.MkdirAll(d, 0755))
599+
}
600+
601+
for _, f := range []string{
602+
filepath.Join(unit1Dir, "terragrunt.hcl"),
603+
filepath.Join(unit2Dir, "terragrunt.hcl"),
604+
filepath.Join(unit3Dir, "terragrunt.hcl"),
605+
} {
606+
require.NoError(t, os.WriteFile(f, []byte(""), 0644))
607+
}
608+
609+
l := logger.CreateLogger()
610+
opts, err := options.NewTerragruntOptionsForTest(tmpDir)
611+
require.NoError(t, err)
612+
613+
// Exclude unit2
614+
d := discovery.NewDiscovery(tmpDir).WithExcludeDirs([]string{unit2Dir})
615+
cfgs, err := d.Discover(t.Context(), l, opts)
616+
require.NoError(t, err)
617+
assert.ElementsMatch(t, []string{unit1Dir, unit3Dir}, cfgs.Filter(discovery.ConfigTypeUnit).Paths())
618+
619+
// Exclude-by-default and include only unit1
620+
d = discovery.NewDiscovery(tmpDir).WithExcludeByDefault().WithIncludeDirs([]string{unit1Dir})
621+
cfgs, err = d.Discover(t.Context(), l, opts)
622+
require.NoError(t, err)
623+
assert.ElementsMatch(t, []string{unit1Dir}, cfgs.Filter(discovery.ConfigTypeUnit).Paths())
624+
625+
// Strict include behaves the same
626+
d = discovery.NewDiscovery(tmpDir).WithStrictInclude().WithIncludeDirs([]string{unit3Dir})
627+
cfgs, err = d.Discover(t.Context(), l, opts)
628+
require.NoError(t, err)
629+
assert.ElementsMatch(t, []string{unit3Dir}, cfgs.Filter(discovery.ConfigTypeUnit).Paths())
630+
}
631+
632+
func TestDiscoveryHiddenIncludedByIncludeDirs(t *testing.T) {
633+
t.Parallel()
634+
635+
tmpDir := t.TempDir()
636+
hiddenUnitDir := filepath.Join(tmpDir, ".hidden", "hunit")
637+
require.NoError(t, os.MkdirAll(hiddenUnitDir, 0755))
638+
require.NoError(t, os.WriteFile(filepath.Join(hiddenUnitDir, "terragrunt.hcl"), []byte(""), 0644))
639+
640+
l := logger.CreateLogger()
641+
opts, err := options.NewTerragruntOptionsForTest(tmpDir)
642+
require.NoError(t, err)
643+
644+
// Without hidden, but included via includeDirs pattern
645+
d := discovery.NewDiscovery(tmpDir).WithIncludeDirs([]string{filepath.Join(tmpDir, ".hidden", "**")})
646+
cfgs, err := d.Discover(t.Context(), l, opts)
647+
require.NoError(t, err)
648+
assert.ElementsMatch(t, []string{hiddenUnitDir}, cfgs.Filter(discovery.ConfigTypeUnit).Paths())
649+
}
650+
651+
func TestDiscoveryStackHiddenAllowed(t *testing.T) {
652+
t.Parallel()
653+
654+
tmpDir := t.TempDir()
655+
stackHiddenDir := filepath.Join(tmpDir, ".terragrunt-stack", "u")
656+
require.NoError(t, os.MkdirAll(stackHiddenDir, 0755))
657+
require.NoError(t, os.WriteFile(filepath.Join(stackHiddenDir, "terragrunt.hcl"), []byte(""), 0644))
658+
659+
l := logger.CreateLogger()
660+
opts, err := options.NewTerragruntOptionsForTest(tmpDir)
661+
require.NoError(t, err)
662+
663+
// Should be discovered even without WithHidden()
664+
d := discovery.NewDiscovery(tmpDir)
665+
cfgs, err := d.Discover(t.Context(), l, opts)
666+
require.NoError(t, err)
667+
assert.Contains(t, cfgs.Filter(discovery.ConfigTypeUnit).Paths(), stackHiddenDir)
668+
}
669+
670+
func TestDiscoveryIgnoreExternalDependencies(t *testing.T) {
671+
t.Parallel()
672+
673+
tmpDir := t.TempDir()
674+
internalDir := filepath.Join(tmpDir, "internal")
675+
externalDir := filepath.Join(tmpDir, "external")
676+
appDir := filepath.Join(internalDir, "app")
677+
dbDir := filepath.Join(internalDir, "db")
678+
vpcDir := filepath.Join(internalDir, "vpc")
679+
extApp := filepath.Join(externalDir, "app")
680+
681+
for _, d := range []string{appDir, dbDir, vpcDir, extApp} {
682+
require.NoError(t, os.MkdirAll(d, 0755))
683+
}
684+
685+
require.NoError(t, os.WriteFile(filepath.Join(appDir, "terragrunt.hcl"), []byte(`
686+
dependency "db" { config_path = "../db" }
687+
dependency "external" { config_path = "../../external/app" }
688+
`), 0644))
689+
require.NoError(t, os.WriteFile(filepath.Join(dbDir, "terragrunt.hcl"), []byte(`
690+
dependency "vpc" { config_path = "../vpc" }
691+
`), 0644))
692+
require.NoError(t, os.WriteFile(filepath.Join(vpcDir, "terragrunt.hcl"), []byte(""), 0644))
693+
require.NoError(t, os.WriteFile(filepath.Join(extApp, "terragrunt.hcl"), []byte(""), 0644))
694+
695+
opts := options.NewTerragruntOptions()
696+
opts.WorkingDir = internalDir
697+
opts.RootWorkingDir = internalDir
698+
699+
l := logger.CreateLogger()
700+
701+
d := discovery.NewDiscovery(internalDir).WithDiscoverDependencies().WithIgnoreExternalDependencies()
702+
cfgs, err := d.Discover(t.Context(), l, opts)
703+
require.NoError(t, err)
704+
705+
// Find app config and assert it only has internal deps
706+
var appCfg *discovery.DiscoveredConfig
707+
for _, c := range cfgs {
708+
if c.Path == appDir {
709+
appCfg = c
710+
break
711+
}
712+
}
713+
require.NotNil(t, appCfg)
714+
depPaths := appCfg.Dependencies.Paths()
715+
assert.Contains(t, depPaths, dbDir)
716+
assert.NotContains(t, depPaths, extApp)
717+
}

internal/runner/runnerpool/builder.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func Build(ctx context.Context, l log.Logger, terragruntOptions *options.Terragr
3232
if len(terragruntOptions.IncludeDirs) > 0 {
3333
d = d.WithIncludeDirs(terragruntOptions.IncludeDirs)
3434
}
35+
3536
if len(terragruntOptions.ExcludeDirs) > 0 {
3637
d = d.WithExcludeDirs(terragruntOptions.ExcludeDirs)
3738
}
@@ -40,6 +41,7 @@ func Build(ctx context.Context, l log.Logger, terragruntOptions *options.Terragr
4041
if terragruntOptions.StrictInclude {
4142
d = d.WithStrictInclude()
4243
}
44+
4345
if terragruntOptions.ExcludeByDefault {
4446
d = d.WithExcludeByDefault()
4547
}

test/helpers/test_helpers.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,6 @@ func IsExperimentMode(t *testing.T) bool {
7070
t.Helper()
7171
// Enable only on explicit true
7272
val := strings.TrimSpace(os.Getenv("TG_EXPERIMENT_MODE"))
73+
7374
return strings.EqualFold(val, "true")
7475
}

0 commit comments

Comments
 (0)