Filtering and Exclusions¶
Control which files TerraTidy processes using exclusion patterns, recursive scanning, and VCS integration.
Quick Reference¶
| Method | Scope | Example |
|---|---|---|
--exclude | CLI, ad-hoc | --exclude "test/**" |
exclude: | Config file | exclude: ["vendor/**"] |
--no-recurse | CLI, ad-hoc | --no-recurse modules/ |
recursive: | Config file | recursive: false |
--changed | CLI, VCS-aware | --changed |
Exclusion Patterns¶
CLI Flag¶
Use --exclude to skip files matching glob patterns:
# Single pattern
terratidy check --exclude "**/*.generated.tf"
# Multiple patterns (comma-separated)
terratidy check --exclude "test/**,vendor/**,**/*.generated.tf"
# Multiple patterns (repeated flag)
terratidy check --exclude "test/**" --exclude "vendor/**"
Configuration File¶
Define permanent exclusions in .terratidy.yaml:
exclude:
- "**/*.generated.tf" # Generated files
- "vendor/**" # Vendored dependencies
- ".terraform/**" # Terraform cache
- "test/fixtures/**" # Test fixtures
- "modules/legacy/**" # Legacy code during migration
Combining CLI and config: Patterns from both sources are combined. CLI patterns add to config patterns, they don't replace them.
Pattern Syntax¶
| Pattern | Description | Matches |
|---|---|---|
* | Any characters except / | *.tf matches main.tf |
** | Zero or more directories | **/*.tf matches a/b/c/main.tf |
? | Any single character | ?.tf matches a.tf |
[abc] | Character class | [mv]*.tf matches main.tf, vars.tf |
Common Patterns¶
exclude:
# Generated files
- "**/*.generated.tf"
- "**/*.auto.tfvars"
# Build artifacts
- ".terraform/**"
- ".terragrunt-cache/**"
# Dependencies
- "vendor/**"
- "node_modules/**"
# Test files
- "test/**"
- "**/testdata/**"
- "**/*_test.tf"
# Environment-specific
- "environments/sandbox/**"
# Legacy code
- "modules/deprecated/**"
Recursive Scanning¶
Default Behavior¶
By default, TerraTidy recursively scans all subdirectories:
Disable Recursion¶
Use --no-recurse to scan only the specified directories:
# Scan only modules/ (not subdirectories)
terratidy check --no-recurse modules/
# Scan multiple directories without recursion
terratidy check --no-recurse . modules/ environments/
Configuration File¶
The CLI flag --no-recurse takes precedence over the config setting.
Use Cases¶
Module-level checks: Check each module independently without descending:
Root-only validation: Check only files in the current directory:
VCS Integration¶
Changed Files Only¶
The --changed flag uses git to detect modified files:
This checks:
- Staged files (added to index)
- Unstaged modifications
- Untracked
.tf,.hcl,.tfvarsfiles
How it works:
- Detects the default branch (
mainormaster) - Finds the merge-base between HEAD and the default branch
- Lists all changed files since that point
- Filters to supported file types
Combining with Other Flags¶
# Changed files, excluding test fixtures
terratidy check --changed --exclude "test/**"
# Changed files in specific directory only
terratidy check --changed --no-recurse modules/
Automatically Skipped Directories¶
These directories are always skipped, regardless of configuration:
| Directory | Reason |
|---|---|
.terraform/ | Terraform provider cache |
.terragrunt-cache/ | Terragrunt cache |
node_modules/ | npm dependencies |
vendor/ | Go dependencies |
__pycache__/ | Python cache |
.* (hidden) | Hidden directories (except .) |
You don't need to exclude these explicitly.
Precedence¶
When multiple filtering methods are used:
- Automatically skipped directories are always excluded
- Config file
exclude:patterns are applied - CLI
--excludepatterns are added --changedfilters to VCS-modified files--no-recurselimits directory depth
All filters are cumulative (they reduce the file set, never expand it).
Examples¶
CI Pipeline¶
# .github/workflows/terratidy.yml
- name: Check changed Terraform files
run: |
terratidy check --changed \
--exclude "test/**,examples/**" \
--format github
Pre-commit Hook¶
Monorepo¶
# .terratidy.yaml
exclude:
- "modules/legacy/**" # Skip legacy during migration
- "environments/sandbox/**" # Skip sandbox environment
- "**/testdata/**" # Skip test fixtures
profiles:
ci:
description: "Strict CI checks"
engines:
policy: { enabled: true }
development:
description: "Fast local checks"
engines:
lint: { enabled: false }
policy: { enabled: false }
Profile-specific exclusions
The exclude: setting is a top-level config key only. Profiles cannot override exclusions. For profile-specific file filtering, use separate config files with imports:.
Module Development¶
# Check only the module you're working on
terratidy check --no-recurse modules/vpc/
# Check module and its submodules
terratidy check modules/vpc/
Troubleshooting¶
Files Not Being Checked¶
- Check if they match an exclusion pattern:
-
Check if they're in an automatically skipped directory
-
Verify file extension is
.tf,.hcl, or.tfvars
Too Many Files Being Checked¶
- Add exclusion patterns:
-
Use
--no-recursefor targeted checks -
Use
--changedfor incremental checks
--changed Not Finding Files¶
- Verify you're in a git repository
- Check the default branch is detected:
- Verify files have been modified since the merge-base