# Yarn

`cooldown` gates Yarn projects through `--tool yarn`. Yarn Classic (v1) and Yarn Berry (v2+) share
the tool, its `[tool.yarn]` config, and `yarn.lock`.

## What it reads

A Yarn project is a `package.json` with its `yarn.lock`. `cooldown` tells Classic and Berry apart by
the lock itself, since only a Berry lock opens with a `__metadata` entry.

A Berry lock written by Yarn 2 through 4.18 (lockfile versions 4 to 6 and 8 to 10) is read for
every npm release it resolves, so `outdated` and `explain` cover it:

- **An npm alias:** read as its target's release.
- **A patched package:** its own npm release is read as usual.
- **Workspaces and `link:`, `portal:`, `file:`, `patch:`, `exec:`, git, and tarball URL entries:**
  no registry releases, so they are left out.

A lock that is malformed or of another lockfile version is reported `lockfile_unreadable`.

### Workspace members on Berry

A Berry lock records every workspace Yarn installed, each with the dependencies its manifest
declared. `cooldown` takes the members from the lock and credits each one the registry releases its
own declarations bind: the lock entry a member's current declaration of a name resolves to, through
an `npm:` range, a same-name `npm:` alias, or Yarn's builtin `typescript`, `fsevents`, and
`resolve` patches.

A row is direct exactly when some member owns it, and `exclude-folders` drops the rows only an
excluded member owns. These bind no registry release of the name they are declared under:

- an alias of another package
- a `workspace:` range
- a `file:`, `link:`, `portal:`, `exec:`, git, or tarball source

A declaration whose binding `cooldown` cannot prove leaves its name unattributed:

- every locked copy of that name is reported as a direct dependency of no member
- none is landed
- an exact pin of it holds no copy
- the run warns naming each such declaration and why

These declarations count as unproven:

- a name the root `resolutions` can rewrite
- a `catalog:` or `patch:` reference
- a name declared at different ranges across one manifest's install fields
- a declaration the lock records under another range or not at all
- a member the lock does not list
- in a lock of version 6 or older, which records ranges without their protocol, a bare range when
  the project's `defaultProtocol` is set to anything but `npm:` in a `.yarnrc.yml` Yarn reads or in
  `YARN_DEFAULT_PROTOCOL`

A Yarn plugin, configured in any `.yarnrc.yml` Yarn reads or through `YARN_PLUGINS`, can rewrite
any declaration. So no member is attributed at all, and the run warns.

The attribution reads the lock through the manifests and Yarn configuration as they are now, not as
they were at the last install. `check` verifies the lock first and evaluates only a current one.
`outdated` and `explain` read the lock as written, so a lock that is stale against its inputs, such
as a `.yarnrc.yml` or a `resolutions` entry changed since the last install, can be misattributed
there.

## Registry

Publish times come from the npm registry.

Yarn's native cooldown is `npmMinimalAgeGate`. For a Berry project run by Yarn 4.10 or later,
[`cooldown sync`](https://romnn.github.io/cooldown/docs/commands/other/#yarn-berry) writes the native age gate,
`npmMinimalAgeGate` and `npmPreapprovedPackages`, into its `.yarnrc.yml`. A Yarn Classic project
has no native age gate.

## How it is driven

`cooldown` runs `yarn`, or the program `COOLDOWN_YARN` names. It runs Yarn with Corepack's auto-pin
off (`COREPACK_ENABLE_AUTO_PIN=0`), so no check writes a `packageManager` field into the root
`package.json`.

`--build` runs a frozen install in the project:

- **Yarn Classic:** `yarn install --frozen-lockfile`.
- **Yarn Berry:** `yarn install --immutable`, once the run has landed.

### Which Yarn runs on a Classic lock

Before running Yarn on a Classic lock, `cooldown` checks that the Yarn the project declares
(`packageManager`) is Yarn 1, and so is the one `yarn --version` reports:

- **For `--build`:** in the project, through `yarnPath` or Corepack.
- **For a landing:** only in its confined copy, with `yarnPath` ignored.

Otherwise it refuses rather than let a Berry Yarn migrate the lock. Classic landings run on Linux
only, in a confined throwaway copy; [`upgrade`](https://romnn.github.io/cooldown/docs/commands/upgrade/#lock-versus-manifest)
describes them.

### Checking a Classic lock

`check` proves a Yarn Classic lock current by letting Yarn Classic write it. An install with
`--force` and `--ignore-scripts` runs in a throwaway copy of the project, made and confined as an
`upgrade` landing's, with its cache in the copy's scratch directory:

- **A lock Yarn writes back with the same entries:** current, whatever its line endings and
  comments.
- **A lock Yarn rewrites with other entries:** stale, which `--allow-stale-lock` waives.

`yarn install --frozen-lockfile` is no such check. It accepts a lock that still records a
dependency the manifests dropped, or a workspace member's old range.

### Checking a Berry lock

`check` asks Yarn itself whether a Berry lock is current, with Yarn 2.4 through 4: the `yarn` that
runs in the project, through `yarnPath` or Corepack. It runs `yarn install --immutable` without
builds, never in the project, which stays byte for byte as it was, but in a throwaway copy of every
input Yarn reads:

- the lock
- every `package.json` outside `node_modules`, `.git`, and `.yarn`
- the `.yarnrc.yml` files from the project up to the home directory
- `.env.yarn`
- `.yarn/releases`
- `.yarn/patches`
- `file:` folders, whole with their permissions
- the manifests of `portal:` and `link:` targets

The run is offline for Yarn, Corepack, and git, and every folder Yarn writes is redirected into the
copy. Yarn's result decides the verdict:

- **Yarn reaches its fetch step:** the lock is current.
- **Yarn reports `YN0028`** (the lockfile would be modified): the lock is stale, an error that
  `--allow-stale-lock` waives.
- **Anything else:** `lock_unknown`, listed under [Edge cases](#edge-cases).

Should any input change while `check` reads the dependencies after the verdict, the run reports
`lock_conflict` instead of evaluating a graph Yarn never verified. A change undone before that
second read goes unnoticed, the same window pnpm's verify-then-read check leaves. `--lock`
refreshes no Berry lock.

### Landing on Berry

`upgrade` and `fix` land each candidate of a Berry project run by Yarn 4 at exactly its target. The
run first has Yarn prove the lock current, as `check` does, and refuses a stale lock or one Yarn
cannot judge before changing anything. Each candidate then resolves in a throwaway copy of the
project in two lock-only installs (`yarn install --mode=update-lockfile`):

1. the first with every declaration of the package pinned to the target;
2. the second with the declarations restored and the lock binding each declared range to the
   target's entry, which Yarn would otherwise keep bound to the old copy or resolve afresh to the
   range's newest release.

Yarn then proves the settled lock current in the copy. Only the lock is written back, once every
input the copy holds is unchanged in the project; manifests, install state, and caches stay as they
were. A candidate that fails is skipped `resolver_conflict` with Yarn's error and leaves the lock as
it was.

A Yarn with the native age gate (4.10 or later) resolves each landing under an approval of exactly
the target, beside the approvals Yarn's configuration already holds. It also resolves under
`cooldown`'s window when that is stricter than Yarn's own gate, so a new transitive the gate
quarantines fails the candidate. Yarn 4.17 and later gate each configured `npmScopes` entry by that
scope's own gate instead, so a landing that newly resolves another release in a scope gated below
the window fails too.

The installs keep Yarn's own network settings and global folder, whose metadata cache and mirror
serve an `--offline` landing. A global folder inside the project, or inside any directory holding a
`package.json`, is replaced by one in the copy.

## Edge cases

On Yarn Classic, the lock is `lock_unknown`:

- under `--offline`, since the install fetches every package;
- off Linux, where no Yarn Classic install is confined;
- when `packageManager` names another manager;
- when an rc file Yarn's launcher reads mentions `yarn-path` or `yarnPath`, since the project may
  then run another Yarn;
- when Yarn fails, reports an error, even one it exits 0 after, or exits without writing the lock;
- when the project's inputs change while Yarn checks it.

On Yarn Berry, `lock_unknown` is an error nothing waives. The lock is `lock_unknown` for:

- a lock whose change needs registry metadata the offline run cannot fetch;
- a configuration the copy cannot reproduce faithfully:
  - a configured plugin
  - an `exec:` dependency
  - a workspace pattern with `..` or an absolute path
  - a local package named by an absolute path, through a symbolic link, or outside the home
    directory
  - a symbolic link whose copy would resolve outside the copy
  - injected environment files other than `.env.yarn`
  - a lockfile name other than `yarn.lock`
  - `networkSettings` or an enabled `enableNetwork`
  - hardened mode set to anything but off
  - an output folder outside the copy
  - an rc file using Yarn's `onConflict` form at its top level or for one of those settings,
    `packageExtensions`, or `yarnPath`
- an input that changes while the copy is written;
- a Yarn without a supported version.

A Berry candidate is held `not_eligible`, naming the declaration, when:

- one of its declarations is no registry range admitting the target (an `npm:` alias, a `patch:`,
  or a local source);
- a member declares it at several ranges;
- binding the declared ranges would move a copy the candidate does not own: a range the lock binds
  to another copy, or one a member excluded from the run declares too.

`upgrade` and `fix` refuse a Berry project run by any Yarn but Yarn 4, which the other commands
still read.

A `--dry-run` copy recreates in-tree symbolic links as absolute links and drops directory
permissions. So a project relying on them reports the preview as an error and writes nothing, while
the real run lands.

Every npm-family tool owns `package.json`, so two of them at one root take turns under `upgrade`,
`fix` and `--lock`; see [Several ecosystems at once](https://romnn.github.io/cooldown/docs/tools/#several-ecosystems-at-once).
