Overview
Affected detection analyzes your workspace to determine:- Which files have changed
- Which projects are impacted by those changes
- Which tasks need to re-run
- Which projects depend on affected projects
Affected detection helps you run only what’s necessary, dramatically speeding up CI/CD and local development.
Change Types
To start, there are 3 types of “sources” that can trigger an affected state: files, environment variables, and graph relations.Files
A file is considered changed if it has been added, modified, deleted, renamed, moved, copied, so on and so forth. This state is determined by the version control system (VCS) in use, such as Git, Mercurial, or Subversion. For Git, this is determined by thegit status command.
For a project, any changed file that is within the project folder (starts with the project source) triggers affected.
inputs triggers affected.
packages/api/moon.yml
Environment Variables
An environment variable is considered changed if it exists and is non-empty. This is determined by the presence of the variable in the environment, and its value. For projects, they are not affected by environment variables. For a task, any changed environment variable that is configured within the task’senv triggers affected.
moon.yml
Graph Relations
A relation is considered changed if a project or task that is depended on, or depends on, is affected by a changed file or environment variable. This is determined by the project and task graph, which is built from the workspace configuration. For a project, affected dependency/dependent projects of the project (viadependsOn), or affected tasks within the project, triggers affected.
packages/web/moon.yml
deps) triggers affected.
moon.yml
This check is conditionally enabled. For
moon query, it is always enabled. For exec-based commands, it is not enabled by default and the --include-relations flag must be passed.Graph Depth
When determining affected state based on graph relations, the depth of traversal can be configured with the--upstream (--dependencies) and --downstream (--dependents) options. These options can be set to one of the following values:
none- Do not include any relationsdirect- Include only direct relationsdeep- Include all relations
--upstream=deep and --downstream=none, meaning all dependencies are included, but no dependents are included. For query and other commands, the default is none for both.
Upstream (Dependencies)
Downstream (Dependents)
Usage Examples
Running Affected Tasks
Querying Affected Projects
CI/CD with Affected
Optimize CI by running only affected tasks:.github/workflows/ci.yml
Change Detection
Git-based Detection
Moon uses Git to detect changes:Local Changes
Real-World Scenarios
Scenario 1: Library Change
Scenario 2: Feature Branch
Scenario 3: Multiple Projects
packages/web/moon.yml
Configuration
Base Branch Configuration
.moon/workspace.yml
Task Input Configuration
Configure inputs to control affected detection:moon.yml
Advanced Patterns
Matrix Testing
.github/workflows/test-matrix.yml
Selective Deployment
Troubleshooting
Everything is Affected
Causes:- Root-level configuration changed (
.moon/workspace.yml) - Global task configuration changed (
.moon/tasks/**/*) - Package manager lockfile changed
- Toolchain versions changed
Nothing is Affected
Causes:- No changes detected by Git
- Incorrect base branch
- Changes in ignored files only
Unexpected Affected Projects
Causes:- Implicit dependencies via package.json
- File groups including more than expected
- VCS ignore patterns
Best Practices
Use affected in CI
Use affected in CI
Always use
--affected in CI to run only necessary tasks. This dramatically reduces CI times and costs.Configure base branch correctly
Configure base branch correctly
Set the correct default branch in
.moon/workspace.yml to ensure accurate affected detection.Be explicit with inputs
Be explicit with inputs
Define explicit task inputs to control affected detection precisely. Avoid relying on defaults.
Test affected logic locally
Test affected logic locally
Before pushing, test affected detection locally to ensure the right tasks run.
Related Concepts
- Cache - Affected detection works with caching
- Tasks - Configure task inputs for affected detection
- Targets - Run affected targets
- Action Graph - Visualize affected dependencies