---
title: "Affected Dependencies"
description: "Documentation for the vlt client and the vlt registry."
url: "https://docs.vlt.io/client/affected"
image: "https://docs.vlt.io/og/client/affected"
lang: "en"
---

# Affected Dependencies

In a monorepo, a change to one workspace rarely stays there. The _affected_ set is the workspace you touched **plus everything that depends on it** — the packages whose tests, builds and publishes could break because of your change.

vlt has no `affected` command. Affected is inherited state, so it is expressed with [Dependency Selector Syntax](https://docs.vlt.io/client/selectors.md) instead: [`:diff()`](https://docs.vlt.io/client/selectors/pseudo-classes/diff.md) finds what changed, and [`:has()`](https://docs.vlt.io/client/selectors/pseudo-classes/has.md) walks back up to what depends on it.

## What changed

[`:diff()`](https://docs.vlt.io/client/selectors/pseudo-classes/diff.md) matches packages whose files changed against a git commitish, defaulting to `HEAD`:

```bash title="Terminal"
# uncommitted changes
$ vlt query ':workspace:diff()'
```

```bash title="Terminal"
# everything this branch changed
$ vlt query ':workspace:diff(main...HEAD)'
```

Use the three-dot form. `:diff(<ref>)` runs `git diff --name-only <ref>`, which compares that ref to your working tree in _both_ directions — as soon as `main` moves ahead, everything changed on `main` is reported as changed here too. `main...HEAD` diffs against the merge base instead, which is what "changed on this branch" means.

Quote any ref containing a `/`: `:diff("origin/main...HEAD")`. Bare, the selector parser rejects the slash and the query exits non-zero.

`git diff` also ignores untracked files, so a brand new file only counts once it is staged or committed.

## Why the `:workspace` prefix

`:diff()` matches the project root whenever _anything_ in the repo changed, so a bare `:diff(main...HEAD)` almost always includes `.` as well. `:workspace` drops it.

The trade-off: a change to root-level files only — `tsconfig.json`, a CI workflow, the lockfile — maps to the root package alone, so `:workspace:diff()` returns nothing at all for it. A gate built on `:workspace` skips the job on those changes.

## What depends on what changed

[`:has()`](https://docs.vlt.io/client/selectors/pseudo-classes/has.md) matches packages with a **direct** dependency on a match:

```bash title="Terminal"
$ vlt query ':workspace:has(:diff(main...HEAD))'
```

Nest it to reach further up the graph. Each level of `:has()` is one more hop away from the change:

```bash title="Terminal"
# dependents of the dependents
$ vlt query ':workspace:has(:has(:diff(main...HEAD)))'
```

## The affected set

Combine both with a comma. Selector lists are unioned, so this is "changed, or depending on something changed":

```bash title="Terminal"
$ vlt query ':workspace:diff(main...HEAD),
  :workspace:has(:diff(main...HEAD))'
```

Given a monorepo where `packages/utils` changed on your branch:

```
my-monorepo
├── packages/utils/       ← files changed
├── packages/core/          depends on utils
├── packages/cli/           depends on core
└── apps/docs/              depends on nothing
```

that union selects:

```
my-monorepo
├── packages/utils/       ✅ changed
├── packages/core/        ✅ depends on utils
├── packages/cli/            depends on core, two hops away
└── apps/docs/
```

`packages/cli` is two hops from the change, so it needs another level of `:has()`. Add one selector per hop your graph is deep:

```bash title="Terminal"
$ vlt query ':workspace:diff(main...HEAD),
  :workspace:has(:diff(main...HEAD)),
  :workspace:has(:has(:diff(main...HEAD)))'
```

## Running scripts on the affected set

`vlt run` takes the same selector via `--scope`, so a CI job can test only what your branch could have broken:

```bash title="Terminal"
$ vlt run test --scope=':workspace:diff(main...HEAD),
  :workspace:has(:diff(main...HEAD)),
  :workspace:has(:has(:diff(main...HEAD)))'
```

Matched workspaces run in dependency order, so changed workspaces run before their dependents.

Workspaces without the script are skipped rather than failing: `--scope` implies `--if-present`, as do `--workspace`, `--workspace-group` and `--recursive`.

`--workspace` and `--workspace-group` narrow by path glob or named group instead of by query, but they are not combinable with `--scope` — when `--scope` is given it wins and both are ignored.

## Gating a CI job

`--expect-results` turns a query into a check: `vlt query` exits non-zero when the result count does not match, so a job can bail out early when nothing relevant changed.

```bash title="Terminal"
$ vlt query ':workspace:diff("origin/main...HEAD")' --expect-results='>0' \
|| echo 'nothing affected, skipping'
```

Keep the ref quoted here. A parse error also exits non-zero, so an unquoted `origin/main` would be swallowed by the `||` and report "nothing affected" forever.

Results are counted as graph edges, not as packages: one workspace depended on from several places counts more than once. `--view=json` emits those same edges — each entry is an edge with the package under `to` — so workspace paths need extracting and deduping:

```bash title="Terminal"
$ vlt query ':workspace:diff("origin/main...HEAD")' --view=json \
| jq -r '.[].to.location' | sort -u
```

## See also

- [`:diff()`](https://docs.vlt.io/client/selectors/pseudo-classes/diff.md) — match packages by git changes
- [`:has()`](https://docs.vlt.io/client/selectors/pseudo-classes/has.md) — match packages by their dependencies
- [Workspaces](https://docs.vlt.io/client/workspaces.md) — defining workspaces and groups
- [`vlt run`](https://docs.vlt.io/client/commands/run.md) — running scripts across workspaces
