Skip to content

Commit fb7ae48

Browse files
docs: add depth-limited traversal to filter documentation (#5382)
1 parent 9926916 commit fb7ae48

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

docs-starlight/src/content/docs/03-features/18-filter.mdx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,55 @@ terragrunt find --filter '...^vpc'
514514
- terragrunt.hcl
515515
</FileTree>
516516

517+
#### Depth-Limited Traversal
518+
519+
You can limit how many levels of dependencies or dependents to traverse by adding a numeric depth before or after the ellipsis (`...`) operator. This is useful when you only want immediate or nearby relationships rather than the full transitive closure.
520+
521+
```bash
522+
# Find 'service' and only its direct dependencies (1 level deep)
523+
terragrunt find --filter 'service...1'
524+
525+
# Find 'vpc' and only components that directly depend on it (1 level)
526+
terragrunt find --filter '1...vpc'
527+
528+
# Find 'db' with 2 levels of dependencies and 1 level of dependents
529+
terragrunt find --filter '1...db...2'
530+
```
531+
532+
Given this dependency graph where service depends on db and cache, which both depend on vpc:
533+
534+
<FileTree>
535+
- .
536+
- vpc
537+
- terragrunt.hcl
538+
- db
539+
- terragrunt.hcl (depends on: vpc)
540+
- cache
541+
- terragrunt.hcl (depends on: vpc)
542+
- service
543+
- terragrunt.hcl (depends on: db, cache)
544+
</FileTree>
545+
546+
Using `service...1` (dependencies with depth 1):
547+
548+
<FileTree>
549+
- .
550+
- vpc \<-- Not matched (2 hops away, beyond depth limit)
551+
- terragrunt.hcl
552+
- **db** \<-- Matched (1 hop from service)
553+
- terragrunt.hcl (depends on: vpc)
554+
- **cache** \<-- Matched (1 hop from service)
555+
- terragrunt.hcl (depends on: vpc)
556+
- **service** \<-- Matched (target)
557+
- terragrunt.hcl (depends on: db, cache)
558+
</FileTree>
559+
560+
<Aside type="note" title="Multiple Targets and Depth">
561+
When a filter matches multiple targets, the depth limit applies independently to each target. If a component is reachable from multiple targets at different distances, it will be included if it is within the depth limit of _any_ target.
562+
563+
For example, if target A can reach component X in 3 hops and target B can reach X in 1 hop, with a depth limit of 2, X will be included because it is within 2 hops of target B.
564+
</Aside>
565+
517566
<Aside type="tip">
518567
Graph expressions require dependency/dependent information to work correctly.
519568

test/integration_docs_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,26 @@ func TestFilterDocumentationExamples(t *testing.T) {
508508
expectedOutput: "cache\ndb\nservice\n",
509509
},
510510

511+
// Depth-limited graph traversal
512+
{
513+
name: "graph-depth-limited-dependencies-1-level",
514+
fixtureDir: "graph-based",
515+
filterQuery: "service...1",
516+
expectedOutput: "cache\ndb\nservice\n",
517+
},
518+
{
519+
name: "graph-depth-limited-dependents-1-level",
520+
fixtureDir: "graph-based",
521+
filterQuery: "1...vpc",
522+
expectedOutput: "cache\ndb\nvpc\n",
523+
},
524+
{
525+
name: "graph-depth-limited-both-directions",
526+
fixtureDir: "graph-based",
527+
filterQuery: "1...db...2",
528+
expectedOutput: "db\nservice\nvpc\n",
529+
},
530+
511531
// Source-based filtering
512532
{
513533
name: "source-exact-match-github",

0 commit comments

Comments
 (0)