Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions docs/pm/catalogs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ In your workspace packages, use the `catalog:` protocol to reference versions:
}
```

`catalog:` and `catalog:<name>` are accepted in `dependencies`, `devDependencies`, `optionalDependencies` and `peerDependencies`, and as the value of a root [`overrides` / `resolutions`](/pm/overrides) rule. A catalog peer is resolved and hoisted exactly as if the catalog's range were written inline: if an ancestor already provides a version that satisfies the catalog range, the workspace package reuses that copy instead of getting its own. If no ancestor provides a satisfying version, the workspace package gets its own copy of whatever the catalog range resolves to, just as it would with the range written inline. `bun.lock` stores the catalog ranges themselves, not a resolved version per reference, so a catalog reference binds to whichever version in the lockfile satisfies the range, again like an inline range. (pnpm additionally pins each catalog entry to the version it first resolved to; Bun does not.)
`catalog:` references work in `dependencies`, `devDependencies`, `optionalDependencies`, `peerDependencies`, and as the value of a root [`overrides`](/pm/overrides) rule. A catalog reference behaves exactly as if the catalog's range were written inline.

### 3. Run Bun Install

Expand Down Expand Up @@ -238,17 +238,15 @@ To update versions across all packages, change the version in the root package.j

Then run `bun install` to update all packages.

To add a dependency to the catalog, run `bun add` with `--catalog` (or `--catalog=<name>`) from the workspace package or from the root; an entry that already exists is reused unless you give a version:
## Adding to the catalog with `bun add`

`bun add --catalog` (or `--catalog=<name>`) adds the entry to the root catalog and writes `"catalog:"` to the current package. An existing catalog entry is reused unless you pass an explicit version. See [`bun add --catalog`](/pm/cli/add#--catalog).

```bash terminal icon="terminal"
bun add react --catalog
```

Bun writes the entry to the catalog in the root `package.json` (the range the current `package.json` already declares, otherwise the resolved version) and `"catalog:"` to the `package.json` you ran the command in. See [`bun add --catalog`](/pm/cli/add#--catalog).

When the entry already existed and differs from the range the package declared, `bun add` says so on stderr, e.g. `note: react in app now follows the catalog entry "^19.0.0" instead of "^18.2.0"`. With `--filter`, the first selected package that declares a range seeds a missing entry; a later package whose declared range does not fit that entry keeps its range and gets a `keeps` note instead.

`bun add react` without the flag also uses the default catalog when it lists `react`: the package gets `"catalog:"`, and an existing `"catalog:<name>"` reference is kept. `bun add react@latest` (or any explicit version) writes a concrete range instead. Named catalogs are only used through `--catalog=<name>` or an existing reference, and a version you type is never checked against the catalog (there is no equivalent of pnpm's `catalogMode` setting).
Even without the flag, `bun add react` (no version) writes `"catalog:"` when the default catalog already lists `react`. Pass a version to write a concrete range instead.

## Lockfile Integration

Expand Down Expand Up @@ -296,9 +294,9 @@ Bun's lockfile tracks catalog versions, so installs are consistent across enviro

- Catalog references must match a dependency defined in either `catalog` or one of the named `catalogs`
- Empty strings and whitespace in catalog names are ignored (treated as default catalog)
- `catalog:default` is the same as `catalog:`; both read the `catalog` field, or `catalogs.default` if that is where the entry is defined (as pnpm names it). A package may be defined in only one of the two: if both `catalog` and `catalogs.default` list it, `bun install` stops with `error: "react" is defined in both "catalog" and "catalogs.default"` (`bun add --catalog` always edits the one that already defines the package)
- `catalog:default` is the same as `catalog:`. The default catalog can be defined as either `catalog` or `catalogs.default`, but a package listed in both is an error
- Invalid dependency versions in catalogs fail to resolve during `bun install`
- `catalog:` is only understood in the root `package.json` and in workspace packages. A `catalog:` specifier inside an installed package (from the registry, git, a tarball or a `file:` folder) never reads your catalogs: as a regular dependency it fails to resolve (`x@catalog: failed to resolve`), and as a peer dependency it behaves like an optional `*` peer: it binds to whichever version the package that depends on it already provides, and no copy is installed for it otherwise. Publish with `bun publish` or `bun pm pack`, which substitute the ranges (see [Publishing](#publishing))
- `catalog:` only works in the root and workspace `package.json` files. Inside a published package it fails to resolve — publish with `bun publish` or `bun pm pack`, which replace it with the real range (see [Publishing](#publishing))

## Publishing

Expand Down
43 changes: 15 additions & 28 deletions docs/pm/cli/add.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
bun add zod@latest
```

The package is written to `dependencies` unless `--dev`, `--optional` or `--peer` is given; if the current `package.json` already lists it in another group, that entry is updated in place instead.
The package is written to `dependencies` unless `--dev`, `--optional`, or `--peer` is given. If `package.json` already lists it in another group, that entry is updated in place.

## `--dev`

Expand Down Expand Up @@ -48,7 +48,7 @@
bun add --peer @types/bun
```

Only `peerDependencies` is written; because Bun installs peer dependencies by default, no `devDependencies` entry is needed for the package to be installed locally (pnpm's `--save-peer` adds one).
Bun installs peer dependencies by default, so no additional `devDependencies` entry is needed.

## `--exact`

Expand Down Expand Up @@ -83,7 +83,7 @@

## `--catalog`

In a workspace, `bun add react --catalog` resolves the version as usual, but writes it to the root `package.json` [catalog](/pm/catalogs) (`workspaces.catalog`, or `workspaces.catalogs.<name>` with `--catalog=<name>`) and adds `"react": "catalog:"` (or `"catalog:<name>"`) to the current package. An entry that is already in the catalog is reused unless you give a version. Combine with `--exact`, `--dev`, etc. as usual.
In a workspace, `--catalog` writes the version to the root `package.json` [catalog](/pm/catalogs) and adds `"catalog:"` to the current package. `--catalog=<name>` uses a named catalog (`workspaces.catalogs.<name>`) and writes `"catalog:<name>"`.

```bash terminal icon="terminal"
bun add react --catalog
Expand All @@ -110,26 +110,19 @@
}
```

A few things to know, some of which differ from pnpm's `--save-catalog`:
- If the catalog already has an entry, it is reused and only `"catalog:"` is written to the current package. Pass an explicit version (`bun add react@19 --catalog`) to replace the entry — this affects every package that references it.

Check warning on line 113 in docs/pm/cli/add.mdx

View check run for this annotation

Claude / Claude Code Review

--catalog: explicit version does not always replace an existing entry

This drops a qualifier the old text had: an explicit *exact* version that already satisfies the existing catalog range (e.g. `bun add react@19.1.0 --catalog` with an entry of `^19.0.0`) does **not** replace the entry — per `add_catalog.rs` `exact_within` → `Outcome::Moved`, the original range is written back and only the locked version moves. The same over-simplification is in `docs/pm/catalogs.mdx` ("reused unless you pass an explicit version"). Consider e.g. "Pass a version outside the existin

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 This drops a qualifier the old text had: an explicit exact version that already satisfies the existing catalog range (e.g. bun add react@19.1.0 --catalog with an entry of ^19.0.0) does not replace the entry — per add_catalog.rs exact_within → Outcome::Moved, the original range is written back and only the locked version moves. The same over-simplification is in docs/pm/catalogs.mdx ("reused unless you pass an explicit version"). Consider e.g. "Pass a version outside the existing range to replace the entry".

Extended reasoning...

What the new text says vs. what the code does

The rewritten bullet at docs/pm/cli/add.mdx:113 reads:

Pass an explicit version (bun add react@19 --catalog) to replace the entry — this affects every package that references it.

and docs/pm/catalogs.mdx now says:

An existing catalog entry is reused unless you pass an explicit version.

Both are stated as unconditional. The old text this PR trimmed correctly said:

bun add react@19.1.0 --catalog keeps an entry such as ^19.0.0 that the version satisfies and only changes which version is installed; any other version or range replaces the entry.

Code path

In src/install/PackageManager/add_catalog.rs:

  • edit_root_before_install (~lines 907–919): with an existing catalog entry and Candidate::Explicit(wanted), it writes wanted to the catalog for the install pass, then calls exact_within(wanted, &entry). If that returns true, the outcome is Outcome::Moved { entry }, which stores the original entry text.
  • exact_within (~lines 851–862): parses wanted, calls group.get_exact_version(). Returns true only when wanted is a single exact version and the existing entry's range satisfies it.
  • edit_root_after_install (~line 1077): Outcome::Moved { entry } => entry.to_vec(), and lines ~1109–1123 write that literal back into the root catalog.

Net effect: when the user passes an exact version that already fits the existing entry, the catalog entry is not replaced — the original range is restored to the root package.json and only the locked/installed version changes.

Step-by-step example

  1. Root catalog has "react": "^19.0.0".
  2. User runs bun add react@19.1.0 --catalog, expecting the catalog entry to become 19.1.0 per the new doc.
  3. Candidate::Explicit("19.1.0"), existing entry "^19.0.0" → exact_within("19.1.0", "^19.0.0"): "19.1.0" parses to a single exact version via get_exact_version(), and ^19.0.0 satisfies 19.1.0 → true.
  4. Outcome = Moved { entry: "^19.0.0" }.
  5. After install, edit_root_after_install writes "^19.0.0" back to the catalog.
  6. Result: catalog entry is still ^19.0.0; bun.lock now has react@19.1.0. The doc's promised replacement did not happen.

Note the doc's own inline example, react@19, does replace the entry — 19 is a range, so get_exact_version() returns None, exact_within is false, and the outcome is Seeded. The inaccuracy is only in the blanket claim, not the example.

Why existing wording doesn't cover it

The PR description says "No behaviour documented in #38333 was changed, only trimmed", but this trim removed a load-bearing qualifier. Nothing else on either page mentions the exact-within-range case any more, so a user reading the new text has no way to learn that react@19.1.0 and react@19 behave differently against an existing ^19.0.0 entry.

Impact

Documentation-only. Worst case: a user runs bun add react@19.1.0 --catalog expecting to pin the catalog to 19.1.0, sees ^19.0.0 still there, and is confused. Nothing breaks at runtime; the common case (crossing the existing range, or passing a range like @19) behaves as documented.

Fix

Restore the qualifier in a short form, e.g. in add.mdx:

Pass a version outside the existing range (bun add react@19 --catalog) to replace the entry — this affects every package that references it. An exact version that already fits the entry only moves the locked version.

and in catalogs.mdx:

An existing catalog entry is reused unless you pass a version outside its range.

- Without a version, a range already in the current `package.json` (`"react": "^18.2.0"`) is what gets cataloged.
- A package that already references `"catalog:<name>"` keeps using that catalog.
- The name must be attached with `=`: `--catalog=testing`, not `--catalog testing`.
- Tarball and git specifiers are cataloged under the package's real name. Relative paths and workspace packages are rejected.

- If the catalog already has an entry for the package, `bun add react --catalog` leaves it alone and only writes `"react": "catalog:"` to the current package. `bun add react@19.1.0 --catalog` keeps an entry such as `^19.0.0` that the version satisfies and only changes which version is installed; any other version or range replaces the entry, which moves every package that depends on `"catalog:"` along with it.
- Without a version, a range the current `package.json` already has (`"react": "^18.2.0"`) is what gets cataloged. The package is only resolved to its latest version when neither the catalog nor the `package.json` mentions it.
- A package that already depends on `"react": "catalog:legacy"` stays in `legacy`, whatever name the flag carries: the `legacy` entry is reused, or replaced if you gave a version.
- With `--filter`, the entry is decided per selected package. An entry the root already has is used by all of them. When there is none, the first selected package that declares a range seeds it; a later package that declares the same range, or an exact version inside it, is switched to `"catalog:"`, and one that declares anything else keeps its range. Bun prints a `note:` for every package whose range was changed or kept.
- `--catalog` and `--catalog=default` are the same catalog: the entry is written to whichever of `catalog` or `catalogs.default` the root `package.json` already defines (`catalog` is created when there is neither), so a repository migrated from pnpm never ends up with the package in both.
- The recorded version is the one that was installed, so an entry in [`overrides`](/pm/overrides) for the package ends up in the catalog too.
- The name must be attached with `=` (`--catalog=testing`); `--catalog testing` adds a package called `testing`.
- pnpm's `--save-catalog` is spelled `--catalog` and `--save-catalog-name testing` is `--catalog=testing`; Bun silently skips flags it does not know, so the pnpm spellings add the package as an ordinary dependency (and `--save-catalog-name testing` also tries to add a package called `testing`) instead of failing.
- A positional without a name (`bun add https://example.com/react.tgz --catalog`, `bun add github:facebook/react --catalog`) is installed first and then cataloged under the package's real name, with the same text a plain `bun add` would have written. If the catalog already has a different entry for that name, the package gets the tarball directly and Bun prints a note; `bun add <name> --catalog` converts it. If the current package already declares that name itself, the command fails without writing anything and tells you to spell it `bun add <name>@<url> --catalog`, which moves that declaration into the catalog. Relative paths (`./vendor/foo`, `foo@file:../foo`, `link:`) and workspace packages (by name or via `workspace:`) are still refused before anything is written.
- The root `package.json` must have a `workspaces` field.

Catalogs are also used without the flag: in a workspace whose default catalog lists `react`, `bun add react` with no version writes `"react": "catalog:"`, and a package that already depends on `"catalog:<name>"` keeps that reference. Pass a version (`bun add react@latest`) to write a concrete range instead.
Even without the flag, `bun add react` (no version) writes `"catalog:"` if the default catalog already lists `react`. Pass a version to write a concrete range instead.

## `--filter`

<Note>**Alias** — `-F`</Note>

In a monorepo, `--filter` adds the package to the matching workspace package(s) instead of the package in the current directory. Patterns match workspace names or paths (`./packages/*`), `*` matches every workspace package (not the root), and `!pattern` excludes; the `{dir}` and `...` relation selectors from the filtering page work here too (`--filter '{packages}'`, `--filter 'api...'`). Repeat the flag to combine patterns. Every matching package is edited; if nothing matches, the command fails without touching anything, and a pattern that matches nothing while others do prints a warning. `bun remove` and `bun install <pkg>` accept the same flag. See [filtering](/pm/filter).
In a monorepo, add the package to the matching workspace(s) instead of the current directory's package. See [filtering](/pm/filter) for the pattern syntax. Repeat the flag to combine patterns; `!pattern` excludes.

```bash terminal icon="terminal"
bun add zod --filter api
Expand All @@ -138,17 +131,11 @@
bun remove zod --filter '*' --filter '!api'
```

A few things to know, some of which differ from pnpm:

- The workspace root is only edited when a pattern names it: its name, `--filter .` from the root directory, or `{.}`. `*`, filter sets made only of `!` patterns, and relations that merely reach it through its own dependencies (`...api` when the root depends on `api`) leave it alone (`--filter '*' --filter '<root-name>'` edits everything). A `package.json` without workspaces is still selected by `*`.
- With a `...` relation, `bun add` and `bun remove` read the workspace links from the workspace `package.json` files as they are on disk, so no `bun.lock` is needed and a dependency you just added by hand counts. The closure includes the named package itself unless you write `^` (`api^...`, `...^api`).
- A `!` pattern always wins, whatever order the flags are given in: `--filter '!./packages/*' --filter api` selects nothing.
- Name patterns are globs, and `*` does not cross `/`: `*-utils` does not match `@acme/date-utils`; use `*/*-utils` or `@acme/*`.
- Path patterns must match a workspace directory exactly, relative to the current directory: `--filter ./packages` selects nothing, `--filter ./packages/*` or `--filter '{packages}'` selects every package in it, and `--filter .` only works from a workspace's own directory.
- Local paths (`./vendor/logger`, `file:../logger`, `./logger.tgz`) are relative to the current directory and are re-spelled relative to each selected package (`"logger": "../../vendor/logger"` in `packages/api/package.json`).
- The flag chooses both which `package.json` files are edited and which workspaces are installed. Everything is still resolved and `bun.lock` is updated for the whole repository, but, as with `bun install --filter`, only the selected workspaces' dependencies are linked into `node_modules`; the other workspaces' `node_modules` (and the root's own dependencies and lifecycle scripts, unless a pattern selected the root) are left exactly as they were, and a plain `bun install` brings them up to date. The `package.json` files are written as soon as the lockfile is saved, so they agree with `bun.lock` even if a root `postinstall` script then fails.
- The install summary is printed from the point of view of one selected workspace (the first one that received every requested package); the other selected `package.json` files get the same edits even though they are not listed in the output.
- `--dry-run` skips writing; `--filter` cannot be combined with `--global`.
- `*` matches every workspace package but not the root. To include the root, name it: `--filter '*' --filter '<root-name>'`.
- If no workspace matches, nothing is written and the command fails.
- Local paths are resolved from the current directory and rewritten relative to each selected package.
- `bun.lock` is updated for the whole repo, but only the selected workspaces are linked into `node_modules`, as with `bun install --filter`.
- Cannot be combined with `--global`.

## `--global`

Expand Down
Loading