# How it works

> How bump makes a release, step by step: packages, anchors, changes, new versions, edits, the commit, the tags, and the undo.

A release has these steps, in this order:

1. Find the root and read the config.
2. Find the packages, and put them in units.
3. Find the anchor of each unit.
4. Find the changes, and select the units to release.
5. Compute the new versions, the range edits, and the file edits.
6. Do the preflight checks.
7. Print the release. With `--dryrun`, stop here.
8. Write the files, commit, and tag.

If a step before 8 fails, bump wrote nothing. If step 8 fails, bump undoes it.

The examples on this page use an npm workspace in independent mode:

```
.
├── packages/
│   ├── cli/package.json    @acme/cli, depends on @acme/core
│   ├── core/package.json   @acme/core
│   └── docs/package.json   @acme/docs, private, dev dependency on @acme/core
├── bump.json               { "independent": true }
└── package.json            { "private": true, "workspaces": ["packages/*"] }
```

## Root

The **root** is the first folder with a config file, from the `--cwd` folder up to the git top level. Without a config file, the root is the git top level. See [Config file](/config/#config-file). Outside a git repository, bump stops with `Not a git repository`.

The root matters for three things:

- The globs in `packages` are relative to the root.
- bump reads the workspace files in the root: `workspaces` in `package.json`, `pnpm-workspace.yaml`, and `[workspace]` in `Cargo.toml`.
- bump looks for npm lockfiles in the root. See [Lockfiles](/lockfiles/).

## Packages

A package is one manifest file with a `name`: a `package.json` or a `Cargo.toml`. One folder can hold both.

bump finds the packages with the `packages` globs of the config. Without `packages`, it reads the workspace files of each ecosystem. See [`packages`](/config/#packages) for the globs and the npm workspace files, and [Cargo workspaces](/cargo/#find-the-crates) for the Cargo rules.

### Package rules

- A manifest without a name is skipped, with a warning. A Cargo virtual manifest is skipped with no warning.
- A package with no version is in no unit. It cannot be released, but it still gets [range edits](#edits).
- An invalid version is an error. Two packages with the same name are an error, also in different ecosystems.
- An npm package with `"private": true` is not publishable. A crate with `publish = false` is not publishable. bump still releases them; the [JSON result](/json/) tells you which ones to publish.

The versioned packages then form units. See [Lockstep and independent](/modes/).

## Anchors

The manifest version is the current version of a unit. The tag of that version is its **anchor**: the release commit that bump compares HEAD with.

For each unit, bump applies the first rule that matches:

| # | Manifest and tags | Result |
| --- | --- | --- |
| 1 | A tag in the history of HEAD has a higher version than the manifest. | Error: `"<tag>" is released, but the manifest says <version>`. |
| 2 | The tag of the manifest version is in the history of HEAD. | Normal. That tag is the anchor. |
| 3 | The tag of the manifest version exists, but is not in the history of HEAD. | Error: `The "<tag>" release commit is not in the history of HEAD; merge it first`. |
| 4 | The manifest version is `0.0.0`. | First release. Reason `first`. |
| 5 | Any other case. | No anchor. Error, or with `--adopt`, reason `adopted`. |

bump ignores tags that are higher than the manifest but not in the history of HEAD. They belong to other branches.

Examples for a unit named `app`:

| `app` manifest | Tags of `app` | Rule |
| --- | --- | --- |
| `0.0.0` | none | 4: first release |
| `3.4.0` | `app@3.4.0`, in the history of HEAD | 2: normal |
| `3.4.0` | none, or only `app@3.3.0` | 5: no anchor |
| `3.4.0` | `app@3.4.0`, only on another branch | 3: error |
| `3.4.0` | `app@3.4.0` and `app@3.5.0`, both in the history of HEAD | 1: error |
| `0.0.0` | `app@1.0.0`, in the history of HEAD | 1: error |

Rule 3 happens, for example, when the release commit was rebased or squashed, so that its tag points to a commit that HEAD does not contain:

```console
$ bump patch
FAIL The "@acme/cli@0.4.2" release commit is not in the history of HEAD; merge it first
```

### No anchor

Each package in the example repository has a version past `0.0.0`. Without tags, each unit matches rule 5. bump lists every unit with no anchor, and gives two ways out:

```console
$ bump minor
FAIL No tag found for 3 units whose manifests are past 0.0.0
│
│      @acme/core   manifest 1.2.0   expected tag @acme/core@1.2.0
│      @acme/cli    manifest 0.4.1   expected tag @acme/cli@0.4.1
│      @acme/docs   manifest 0.1.0   expected tag @acme/docs@0.1.0
│
│    To continue from the last release, tag the commit that released it:
│      git tag -a @acme/core@1.2.0 -m @acme/core@1.2.0 <commit>
│      git tag -a @acme/cli@0.4.1 -m @acme/cli@0.4.1 <commit>
│      git tag -a @acme/docs@0.1.0 -m @acme/docs@0.1.0 <commit>
│
│    To use the manifest versions as the base, run again with --adopt:
│      bump minor --adopt   →   @acme/core 1.3.0, @acme/cli 0.5.0, @acme/docs 0.2.0
```

- **Tag the last release.** Find the commit that released each version, and tag it with the command from the error. bump then finds the changes after that commit.
- **Adopt the manifest versions.** With `--adopt`, bump releases each unit with no anchor, as changed:

```console
$ bump minor --adopt
BUMP @acme/core 1.2.0 → 1.3.0  (adopted)
BUMP @acme/cli 0.4.1 → 0.5.0  (adopted)
BUMP @acme/docs 0.1.0 → 0.2.0  (adopted)
EDIT @acme/cli dependencies["@acme/core"] ^1.2.0 → ^1.3.0
EDIT @acme/docs devDependencies["@acme/core"] ^1.2.0 → ^1.3.0
DONE Committed 6f6a473 release: @acme/core@1.3.0, @acme/cli@0.5.0, @acme/docs@0.2.0
     Tagged @acme/core@1.3.0, @acme/cli@0.5.0, @acme/docs@0.2.0
```

`--adopt` does nothing to units that have an anchor. Rules 1 and 3 have no override.

### Scope

bump finds anchors only for the units that the command can release. Without targets, that is every unit. With targets, it is the target units and the units that depend on them. Thus an unrelated unit with no tag does not stop `bump patch @acme/cli`.

### History

bump needs the full history and all tags. A shallow clone stops with `A full clone is needed`. See [Full clone](/ci/#full-clone). A repository with no commits stops too. A detached HEAD is allowed.

## Changes

A unit is **changed** when a file of one of its packages changed after the unit's anchor.

### Owners

Each changed file belongs to the deepest package folder that holds it. Every package in that folder owns the file. A file in no package folder belongs to no unit: in the example repository, a change to the root `README.md` releases nothing. A package in the root folder owns every file that no deeper package owns.

### Release commits

bump ignores the changes that only release commits made. A release commit is a commit that a unit tag points to.

A release can edit a file of a unit that it does not release, such as a dev dependency range or a `Cargo.lock`. Without this rule, that unit would count as changed in the next release. Here, the tags `@acme/core@1.2.0`, `@acme/cli@0.4.1`, and `@acme/docs@0.1.0` are in place, and `@acme/core` changed:

```console
$ bump minor
BUMP @acme/core 1.2.0 → 1.3.0  (changed)
BUMP @acme/cli 0.4.1 → 0.4.2  (dependent)
EDIT @acme/cli dependencies["@acme/core"] ^1.2.0 → ^1.3.0
EDIT @acme/docs devDependencies["@acme/core"] ^1.2.0 → ^1.3.0
DONE Committed 0394e49 release: @acme/core@1.3.0, @acme/cli@0.4.2
     Tagged @acme/core@1.3.0, @acme/cli@0.4.2
$ bump patch
DONE Nothing to release
```

The release commit edited `packages/docs/package.json`, but `@acme/docs` is not changed.

A change that a merge commit makes, such as a conflict resolution, counts.

### Units with no anchor

A `first` or `adopted` unit has no anchor. All of its files count as changed.

## Selection

In **lockstep** mode, bump releases the one unit when it is changed, `first`, or `adopted`, or when you give `--all`.

In **independent** mode, bump selects:

1. Without targets, each unit that is changed, `first`, or `adopted`. With `--all`, every unit. With targets, only the target units, changed or not.
2. Then each unit that depends at runtime on a selected unit, again and again, until no unit is added.

When nothing is selected, bump prints `DONE Nothing to release` and exits with `0`.

### Reasons

Each released unit has one reason. It is the first that applies:

| Reason | Meaning |
| --- | --- |
| `target` | You named it on the command line. |
| `first` | Its version is `0.0.0`, with no tag. |
| `adopted` | It has no anchor, and you gave `--adopt`. |
| `changed` | Its files changed after its anchor. |
| `all` | You gave `--all`. |
| `dependent` | It depends on a released unit. |

A unit that bump selects only because of a dependency is always `dependent`, also when it changed. See [Lockstep and independent](/modes/#dependents).

## Versions

Each unit that step 1 of the selection picks gets the type or the exact version of the command. See [Type or version](/cli/#type-or-version) for the new version that each type gives.

A dependent gets a patch or a prerelease. See [Lockstep and independent](/modes/#dependents) and [Prereleases](/prereleases/).

Two checks follow:

- Each new version must be greater than the current version.
- No new tag can exist already. bump lists all the tags that exist in one error.

## Edits

bump computes every edit before it writes a file:

- the `version` of each released package;
- each internal dependency range on a released package, in every package, released or not;
- `[workspace.package] version` and `[workspace.dependencies]` of a Cargo workspace, and each tracked `Cargo.lock`. See [Cargo workspaces](/cargo/);
- each tracked npm lockfile in the root. See [Lockfiles](/lockfiles/).

bump replaces only the characters of each old value. The rest of the file stays the same, byte for byte: comments, key order, whitespace, line endings. After each edit, bump parses the new text. It must give the old values with only the intended changes. If not, bump stops before it writes a file. bump never writes `bump.json`.

### Range rewrite

An internal dependency is a dependency on a package of the same ecosystem in the repository. bump keeps the prefix and the precision of a range, and puts in the new version:

| Old range | New version | New range |
| --- | --- | --- |
| `^1.2.0` | `2.0.0` | `^2.0.0` |
| `~1.2.3` | `1.3.0` | `~1.3.0` |
| `=1.2.0` | `1.3.0` | `=1.3.0` |
| `1.2` | `2.0.0` | `2.0` |
| `^1.2` | `1.3.0` | `^1.3` |
| `1` | `2.0.0` | `2` |
| `1.2` | `1.3.0-next.0` | `1.3.0-next.0` |
| `v1.2.0` | `1.3.0` | `1.3.0` |

A prerelease always uses all three numbers. bump updates a range also when the old range already allows the new version.

bump keeps every other range as it is: `workspace:*`, `workspace:^`, `*`, `latest`, `1.x`, `>=1.0.0 <2`, `file:…`, `npm:…`, and git or path sources. An npm range that starts with `npm:` is an alias to another package, so it is not an internal dependency.

## Preflight

These checks run before bump writes a file:

- No tracked file has changes.
- No merge, rebase, cherry-pick, revert, or bisect is in progress.
- git tracks every file to write. An untracked `Cargo.lock` is skipped.
- Each package manager for a lockfile is on your `PATH`.
- Each new version is greater, and no new tag exists.
- Each new tag is a valid git ref name.
- Each edit passes the parse check.

A tracked file with changes stops a release. A dry run does the same checks, but a tracked file with changes gives only a warning. See [Dirty tree](/errors/#dirty-tree).

## Commit and tags

A release does these steps, in this order:

1. Write the files.
2. Update the npm lockfiles.
3. Stage the written files, and commit only those files. The commit message is the `message` template of the config. Your git hooks run.
4. Add one annotated tag per unit on the release commit, with the [tag message](/config/#message). Git config such as `tag.gpgSign` applies.

The default tag and message depend on the mode. See [Lockstep and independent](/modes/#tags-and-messages).

## Undo

If a step of the release fails, bump undoes it in reverse order. It deletes the new tags, resets the commit, and writes back the original text of each file. Then it prints the error:

```console
$ bump patch
BUMP @acme/core 1.3.0 → 1.3.1  (changed)
BUMP @acme/cli 0.4.2 → 0.4.3  (dependent)
EDIT @acme/cli dependencies["@acme/core"] ^1.3.0 → ^1.3.1
EDIT @acme/docs devDependencies["@acme/core"] ^1.3.0 → ^1.3.1
FAIL Could not commit: lint failed: packages/cli/index.js
│    The release was undone: no files, commits, or tags changed.
```

Here a `pre-commit` hook failed. The same undo happens when a tag signature fails, when a lockfile command fails, or when the process gets `SIGINT` or `SIGTERM`. After a signal, bump exits with `130` or `143`.

If the undo itself fails, bump prints both errors. The message says `The undo failed too:`, then lists what remains (the tags, the commit, the changed files), and the git commands to clean up by hand.
