---
title: Scopes with Pants
description: Compute scopes from the Pants dependency graph, including the targets that depend on a change.
---

[Pants](https://www.pantsbuild.org/) builds a dependency graph of your repository by reading your
BUILD files and inferring imports from your sources. Its `--changed` selectors turn a commit range
into the list of targets that range affects, which is exactly what a scope list has to be.

## Configuring manual scopes

To use the manual scopes mechanism, configure Mergify to expect scopes from your CI system:

```yaml
scopes:
  source:
    manual:

queue_rules:
  - name: default
    batch_size: 5
```

## Detecting scopes with Pants

Select the changed targets with `--changed-diffspec`, then ask for the targets that depend on them:

```bash
pants --changed-diffspec="$BASE..$HEAD" \
      --changed-dependents=transitive \
      --no-filedeps-transitive filedeps
```

### Why `--changed-dependents=transitive`

`--changed-dependents` defaults to `none`, which reports only the targets that own the changed
files. Nothing downstream of the change appears. For a repository where `app/server` depends on
`lib/util`, which depends on `lib/core`, editing a source file in `lib/core` gives:

| `--changed-dependents` | Scopes reported                       |
| ---------------------- | ------------------------------------- |
| `none` (the default)   | `lib/core`                            |
| `direct`               | `lib/core`, `lib/util`                |
| `transitive`           | `lib/core`, `lib/util`, `app/server`  |

Only `transitive` describes what the change can break. An under-reported list makes the queue batch
and parallelize changes that genuinely conflict, and lets scope-gated CI skip jobs the change
breaks.

`--no-filedeps-transitive` turns off the transitive mode of `filedeps` itself, which walks the other
way: it would list the files of the affected targets' own *dependencies*, and `lib/core` does not
become affected because something that imports it changed. Off is already the default, so pass it
explicitly to keep a repository-level `[filedeps] transitive = true` in `pants.toml` from quietly
reversing the direction.

### From targets to scope names

`filedeps` prints the source and BUILD files of the targets it was given, so taking the directory of
each one and deduplicating gives one scope per BUILD file directory:

```bash
awk -F/ 'NF>1 { OFS="/"; NF--; print } NF==1 { print "//" }' | sort -u
```

A file at the build root has no directory component, and gets `//`, the address Pants itself uses
for the root.

Reporting the target addresses instead would be finer grained, but they do not survive the upload.
A target [parametrized](https://www.pantsbuild.org/stable/docs/using-pants/key-concepts/targets-and-build-files)
over two fields is addressed `lib/multi@interpreter_constraints=py39,resolve=alt`, and the comma in
it splits into two meaningless scopes wherever the list is uploaded as comma-separated values.

Either grain can be given a capacity of its own:
[`scopes.capacities`](/merge-queue/queue-modes#limiting-concurrency-per-scope) takes any name the
scopes API accepts, whatever the source, so `lib/util: 2` caps that directory and `//` caps
the build root. A key holding `*`, `?`, `[` or `{` is [a glob over scope
names](/merge-queue/queue-modes#naming-scopes-with-patterns) instead, so `lib/**` gives every
directory under `lib` its own budget. To bound the rest,
[`scopes.default_capacity`](/merge-queue/queue-modes#capping-scopes-you-did-not-list) applies to
every scope no key matches. Both keys apply in every [`merge_queue.mode`](/merge-queue/queue-modes),
including the default `serial` mode the [configuration above](#configuring-manual-scopes) leaves the
queue in.

<CITabs>

### GitHub Actions

```yaml
name: Detect Scopes
on:
  pull_request:

jobs:
  detect-scopes:
    runs-on: ubuntu-26.04
    permissions:
      id-token: write
      contents: read
    steps:
      - uses: actions/checkout@v5
        with:
          # Both revisions must exist locally: Pants resolves the diffspec through git.
          fetch-depth: 0
          # Every commit, without the file contents of past revisions. Leave
          # it out with persist-credentials: false, or if a later step reads
          # file history (git blame, git log -p): git fetches each missing
          # file on demand, one round trip at a time, with the job's token.
          filter: blob:none

      - name: Install Pants
        uses: pantsbuild/actions/init-pants@v11
        with:
          gha-cache-key: scopes
          # Scope detection only loads BUILD files, so it needs none of the
          # named caches a build or a test run would.
          named-caches-hash: disable

      - name: Get git refs
        id: refs
        uses: Mergifyio/gha-mergify-ci@v35
        with:
          action: scopes-git-refs

      - name: Get scopes
        id: scopes
        env:
          HEAD: ${{ steps.refs.outputs.head }}
          BASE: ${{ steps.refs.outputs.base }}
        run: |
          # scopes-git-refs reports an empty base on any event that is neither a
          # pull request nor a push. Pants reads the resulting `..HEAD` as an
          # empty range and reports no scopes at all, with a zero exit code.
          [ -n "$BASE" ] || { echo "no base ref reported" >&2; exit 1; }

          # Pants passes the diffspec to git with no option terminator, so pin
          # both values to real commits first. This also fails loudly when the
          # base is missing from the clone instead of reporting an empty range.
          base=$(git rev-parse --verify --end-of-options "$BASE^{commit}") || exit 1
          head=$(git rev-parse --verify --end-of-options "$HEAD^{commit}") || exit 1

          # Keep this list in sync with build_file_prelude_globs: a change to a
          # file that belongs to no target reports nothing, which the queue
          # would read as "this pull request affects nothing".
          changed=$(git diff --name-only --diff-filter=ACMRD --end-of-options "$base" "$head" --) || exit 1
          all_scopes=false
          if printf '%s\n' "$changed" | grep -qE '^(pants\.toml|pants\.ci\.toml|build-support/.*\.py)$'; then
            all_scopes=true
          fi

          # --changed-dependents defaults to `none`, which reports only the
          # targets owning the changed files and nothing downstream of them.
          files=$(pants --changed-diffspec="$base..$head" \
                        --changed-dependents=transitive \
                        --no-filedeps-transitive filedeps) || exit 1
          scopes=$(printf '%s\n' "$files" \
            | awk -F/ 'NF>1 { OFS="/"; NF--; print } NF==1 { print "//" }' \
            | sort -u | paste -sd, -)
          echo "Detected scopes: $scopes"
          echo "scopes=$scopes" >> "$GITHUB_OUTPUT"
          echo "all_scopes=$all_scopes" >> "$GITHUB_OUTPUT"

      - name: Scopes upload
        uses: Mergifyio/gha-mergify-ci@v35
        with:
          action: scopes-upload
          scopes: ${{ steps.scopes.outputs.scopes }}
          all_scopes: ${{ steps.scopes.outputs.all_scopes }}
```

#### Let your CI reach Mergify

Grant `id-token: write` to the job, or to the whole workflow. The job then trades its GitHub-signed ID token for a Mergify OIDC CI token, and no `MERGIFY_TOKEN` is stored:

```yaml
permissions:
  id-token: write # lets the job ask GitHub for an ID token
  contents: read # actions/checkout needs it
```

A `permissions` block sets every permission it does not list to `none`: list the ones your other steps need too.

Mergify refuses the ID token until a trust policy of your organization matches the job. A GitHub organization `Owner` or a **CI Admin** writes one under **Settings → Security & Access → CI Authentication → Trust Policies** on the [dashboard](https://dashboard.mergify.com). [OIDC Authentication](/integrations/gha/oidc) covers trust policies, pull requests from forks and refused exchanges. If your workflows store `MERGIFY_TOKEN` today, [move them off it](/integrations/gha/oidc#move-your-workflows-off-mergify_token).

#### Use an application key instead

Other CIs authenticate with a `ci` [application key](/api/application-keys#ci-reporting), and so do the GitHub Actions jobs OIDC leaves out: runs for pull requests from forks when your organization sends them secrets, and jobs that send scopes from the base branch (`pull_request_target`, `workflow_run`, `issue_comment`). Store the key as the `MERGIFY_TOKEN` repository secret and set it on the job:

```yaml
env:
  MERGIFY_TOKEN: ${{ secrets.MERGIFY_TOKEN }}
```

A `MERGIFY_TOKEN` that is set wins over the ID token. For pull requests from forks, set it on those runs only and drop the fork condition from the upload step, as [Jobs that keep a key](/integrations/gha/oidc#jobs-that-keep-a-key) shows. Store the key in your [Dependabot secrets](/integrations/dependabot#reaching-mergify-from-dependabot-pull-requests) too, since runs for Dependabot pull requests read only those.

### Buildkite

Using the
[`mergifyio/mergify-ci`](https://github.com/Mergifyio/mergify-ci-buildkite-plugin)
Buildkite plugin, a first step resolves the merge-queue-aware base and head
SHAs and exposes them as meta-data, a second computes the affected targets with
Pants into the `mergify-ci.scopes` meta-data, and a third uploads them. The
upload runs in its own step because the plugin replaces the step's command, so a
step that both detects and uploads never runs its detection:

```yaml
steps:
  - label: ":mag: Get git refs"
    key: git-refs
    plugins:
      - mergifyio/mergify-ci#v30:
          action: scopes-git-refs

  - label: ":mag: Detect scopes"
    key: detect-scopes
    depends_on: git-refs
    command: |
      # The base meta-data is only published when a base was resolved, so a
      # missing key means there is no base to diff against. Check the value too:
      # Pants reads an empty base as an empty range and reports no scopes.
      BASE=$(buildkite-agent meta-data get "mergify-ci.base") || exit 1
      HEAD=$(buildkite-agent meta-data get "mergify-ci.head") || exit 1
      [ -n "$$BASE" ] || { echo "no base ref reported" >&2; exit 1; }
      # Pants passes the diffspec to git with no option terminator, so pin both
      # values to real commits first.
      BASE=$(git rev-parse --verify --end-of-options "$$BASE^{commit}") || exit 1
      HEAD=$(git rev-parse --verify --end-of-options "$$HEAD^{commit}") || exit 1
      # --changed-dependents defaults to `none`, which reports only the targets
      # owning the changed files and nothing downstream of them.
      FILES=$(pants --changed-diffspec="$$BASE..$$HEAD" \
                    --changed-dependents=transitive \
                    --no-filedeps-transitive filedeps) || exit 1
      SCOPES=$(printf '%s\n' "$$FILES" \
        | awk -F/ 'NF>1 { OFS="/"; NF--; print } NF==1 { print "//" }' \
        | sort -u | paste -sd, -)
      buildkite-agent meta-data set "mergify-ci.scopes" "$$SCOPES"

  - label: ":mag: Upload scopes"
    depends_on: detect-scopes
    plugins:
      - mergifyio/mergify-ci#v30:
          action: scopes-upload
```

The plugin reads your [application key](/api/application-keys) from `MERGIFY_TOKEN` in
the agent's environment. Without it the step still runs, but it logs a warning
and sends nothing, and the build stays green, so a missing token looks like a
pipeline that reports no scopes.

> **Caution: keep the token out of the pipeline you upload.** Set `MERGIFY_TOKEN` in the environment of the agents running your steps: [Buildkite secrets](https://buildkite.com/docs/pipelines/security/secrets/buildkite-secrets), a secrets plugin backed by your own secrets store, or an agent `environment` hook that exports it. Do not put the token in the pipeline itself: `buildkite-agent pipeline upload` interpolates `${MERGIFY_TOKEN}` before the pipeline is stored, so a token written into an `env:` block is uploaded in plain text and stays readable in the Buildkite UI and in build history. The plugin's `token:` property carries it the same way.

The plugin's `scopes-upload` action takes `all_scopes: true`, flagging a pull request as
[impacting every scope](#changes-pants-attributes-to-no-target). It is set on the step rather than
read from meta-data, so a flag computed from the changed files needs the Mergify CLI, as the Any CI
tab shows.

### Any CI (Mergify CLI)

<ScopesDetection
  command={String.raw`# Pants passes the diffspec to git with no option terminator, so pin both
# values to real commits first. This also fails loudly when the base is
# missing from the clone, which Pants would otherwise read as an empty range
# and report no scopes for, with a zero exit code.
BASE=$(git rev-parse --verify --end-of-options "$BASE^{commit}") || exit 1
HEAD=$(git rev-parse --verify --end-of-options "$HEAD^{commit}") || exit 1

# Keep this list in sync with build_file_prelude_globs: a change to a file
# that belongs to no target reports nothing, which the queue would read as
# "this pull request affects nothing".
CHANGED=$(git diff --name-only --diff-filter=ACMRD --end-of-options "$BASE" "$HEAD" --) || exit 1
ALL_SCOPES=false
if printf '%s\n' "$CHANGED" | grep -qE '^(pants\.toml|pants\.ci\.toml|build-support/.*\.py)$'; then
  ALL_SCOPES=true
fi

# --changed-dependents defaults to none, which reports only the targets owning
# the changed files and nothing downstream of them.
FILES=$(pants --changed-diffspec="$BASE..$HEAD" \
              --changed-dependents=transitive \
              --no-filedeps-transitive filedeps) || exit 1
printf '%s\n' "$FILES" \
  | awk -F/ 'NF>1 { OFS="/"; NF--; print } NF==1 { print "//" }' \
  | sort -u \
  | jq -R -s --argjson all "$ALL_SCOPES" \
      '{scopes: split("\n") | map(select(length > 0)), all_scopes: $all}' > scopes.json`}
/>

</CITabs>

## What Pants reports for each kind of change

The list Pants returns depends on whether the changed file belongs to a target, which is not the
same question as whether it changes the build:

- **A source file.** The target that owns it, plus every target that depends on it.

- **A BUILD file.** The targets that file defines, plus their dependents. Editing metadata such as
  a target's `tags` or `dependencies` reports the same scopes as editing its sources, so BUILD-only
  pull requests are scoped correctly.

- **A deleted source file.** The generated per-file target is gone, so Pants reports the target
  generator in its place. The generator's BUILD file keeps the directory in the list, and the
  dependents of the deleted file are still reported.

- **A lockfile or a requirements file.** The requirement targets it defines, plus everything that
  imports them. A lockfile at the build root therefore reports `//` and each directory that uses a
  requirement from it.

- **A file no target owns.** No scopes at all. `README.md` gives an empty list because it really
  does affect nothing, but so do `pants.toml` and the files your `build_file_prelude_globs` points
  at, which affect everything.

## Changes Pants attributes to no target

Pants answers "which targets own these files, and what depends on them". Files outside the target
graph belong to no target, so a change confined to them reports an empty list whatever
`--changed-dependents` is set to.

Two of them are worth handling, because both change how every target builds:

- **`pants.toml`**, and any other config file you pass through `PANTS_CONFIG_FILES`.

- **The BUILD file prelude**, the Python files named by `build_file_prelude_globs`. Every BUILD file
  loads them, but Pants does not model that as a dependency: giving a prelude file a
  `python_sources` target of its own changes nothing, since the change is then reported as that one
  target with no dependents.

Neither can be expressed as a scope list, so report them as a
[barrier](/configuration/scopes#declaring-a-pull-request-impacts-every-scope) instead. The GitHub
Actions and Any CI recipes set `all_scopes` from a path match on the changed files, so keep that
pattern in sync with your `build_file_prelude_globs`.
