# Cargo

`cooldown` gates Rust projects through `--tool cargo` (aliases `crates`, `rust`). It reads
`Cargo.toml` and `Cargo.lock`, and judges each crate by its crates.io publish time.

## What it reads

Cargo projects are detected by `Cargo.toml`. A nested manifest is a project of its own unless the
resolve of a project the run evaluates already covers it. Only a project that is actually evaluated
can cover anything, so a workspace that was pruned, ignored, or simply never scanned covers nothing.

A workspace resolves its members, minus whatever `exclude` names, unless `members` names it as
well:

- **The `members` globs:** the directories they match. `crates/*` matches `crates/x`, not
  `crates/x/y`, as in cargo, and `./a` and `a/` mean the directory `a`.
- **The root package itself:** when the root manifest declares `[package]`.
- **The path dependencies those pull in:** cargo makes them members too, whether they lie inside
  the workspace directory or outside it with their own `package.workspace` pointing back at this
  root.

A member's `dep = { workspace = true }` resolves through the root's `[workspace.dependencies]`, so
the crate that entry's `path` names is a member's dependency like any other. Beyond the members,
the resolve follows every `path` dependency they reach, through `[dependencies]`,
`[build-dependencies]`, and the `[target.<cfg>.…]` forms. A plain `[package]` with no `[workspace]`
table is the single member of its own implicit workspace and resolves the same closure from there.

Two kinds of edge stop at the members:

- **`[dev-dependencies]`:** count for members only. Cargo locks its members with dev units enabled
  but activates everything beyond them as an ordinary dependency, with dev-dependencies off. So a
  crate reachable only as a reached package's dev path dependency is in nobody's lock and is a
  project of its own.
- **An `optional` path dependency of a reached package:** `cooldown` treats it the same way, and
  that one is `cooldown`'s own rule rather than cargo's. The edge is not followed whether or not a
  feature enables it, because which features are enabled is known only to the resolve. A crate
  reached only that way is therefore gated on its own lock: it reports a missing one rather than
  passing silently, or is dropped with [`exclude-folders`](https://romnn.github.io/cooldown/docs/configuration/excludes/).

A known limit runs the other way. A path dependency reached as a non-member has its own
`[dev-dependencies]` in nobody's lock either, and `cooldown` gates the crate through the project
that resolves it rather than gating what only its standalone test build would fetch.

Two shapes below an enclosing `[workspace]` are not its members:

- **A manifest declaring `[workspace]`:** always a project of its own, since cargo forbids a
  workspace root from being another workspace's member.
- **A manifest the enclosing `[workspace]` neither lists in `members` nor `exclude`s:** stays with
  that workspace and is no project. Cargo refuses to build such a crate on its own ("current
  package believes it's in a workspace when it's not"), so nothing resolves it. Fixture and
  template crates commonly sit there. A `package.workspace` pointer is the exception: it asked to
  be a member of the root it names, so if that root does not list it, it is a project of its own.

Below a detected Cargo project, two kinds of directory are never projects, since cargo loads
neither as a package of the surrounding build:

- **A vendored crate:** `cargo vendor` output, marked by `.cargo-checksum.json`.
- **`cargo package`'s unpacked output:** under `target/package`.

`-C`/`--dir` follows the same rule. Pointing the run at a directory another project resolves runs
*that* project, wherever its root sits, since a workspace may list a member that is not below it.
The report is scoped to what the selected directory declares.

### Generated workspace-hack members

A cargo-hakari workspace-hack is a member whose `[dependencies]` are generated from the lock.
Declare it in
[`[tool.cargo] generated-members`](https://romnn.github.io/cooldown/docs/configuration/selectors/#toolcargo-generated-members):
its entries then follow every upgrade instead of being proposed, and reported `blocked`, as
upgrades of their own. `cooldown` hints when an undeclared member carries the hakari marker.

## Registry

Publish times come from crates.io.

### Cargo's own publish age

Cargo 1.100 and newer can skip crates.io releases younger than a configured age. `cooldown` reads
that age as the project's native cooldown, the way Cargo itself resolves it for a command run in
the project:

- `registries.crates-io.min-publish-age` wins over `registry.global-min-publish-age`, whichever
  file or variable sets them.
- The nearest `.cargo/config` (or `.cargo/config.toml` when there is no `config`) in the project's
  ancestors wins, and the Cargo home's file ranks last.
- `CARGO_REGISTRY_GLOBAL_MIN_PUBLISH_AGE` overrides a file's global age.
- `resolver.incompatible-publish-age = "allow"` (or `CARGO_RESOLVER_INCOMPATIBLE_PUBLISH_AGE=allow`)
  switches Cargo's enforcement off, so `cooldown` reads no age either.

A `min-age` under `[package.metadata.cooldown]` or `[workspace.metadata.cooldown]` in `Cargo.toml`
speaks for `cooldown` directly and wins over Cargo's age. Like every native window, it ranks below
`cooldown.toml` and the command line; see
[`--fail-on-stricter-native`](https://romnn.github.io/cooldown/docs/commands/check/) to fail when a policy overrides a
stricter one. An age set in a file brought in through Cargo's `include` is not seen.

`cooldown` applies the window itself and runs its own cargo commands without Cargo's crates.io age,
so a stricter native age never makes Cargo refuse a version the policy admits. Ages for other
registries are left to Cargo. To give `cooldown`'s own cargo commands a crates.io age anyway, set
`COOLDOWN_CARGO_MIN_PUBLISH_AGE` in Cargo's spelling (`"14 days"`). It bounds what Cargo can
resolve but is not read as policy.

[`cooldown sync`](https://romnn.github.io/cooldown/docs/commands/other/#cargo) writes the policy's default window into
the repository's Cargo configuration as `min-publish-age`.

## How it is driven

`cooldown` runs `cargo`, or the program `COOLDOWN_CARGO` names:

- **`upgrade` and `fix`:** move each crate with `cargo update -p <crate> --precise <version>`.
- **`--lock`:** runs `cargo update --workspace`, which generates a missing lock and re-resolves the
  members' own requirements while every locked version the manifests still admit stays put.
- **`--build`:** runs `cargo build`.

Cargo projects must currently use the workspace-root `Cargo.lock`. `cooldown` fails explicitly when
Cargo's `resolver.lockfile-path` configuration or `CARGO_RESOLVER_LOCKFILE_PATH` selects a custom
location, because it cannot yet stage, normalize and recover such an alternate file safely.

### Mutations and recovery

Cargo mutations that publish manifests or a lockfile currently require a Git worktree on Unix.
`cooldown` stores recovery authority beneath Git's common directory and verifies its Unix ownership
and permissions, so ordinary project content cannot claim permission to restore source files. The
Git metadata directory and common directory must be owned by the current Unix user, so
repositories exposed through another user's or root-owned bind mount fail closed.

Outside a Git worktree on Unix, read-only commands and isolated previews remain available, but a
source mutation fails closed. On Windows, where `cooldown` cannot prove the recovery namespace is
private to the current user, Cargo mutations run in place with trial and rollback instead and keep
no recovery record, as the
[Security model](https://romnn.github.io/cooldown/docs/security/#mutations-honor-the-selected-graph-gate) describes.
[`cooldown recover`](https://romnn.github.io/cooldown/docs/commands/other/#recover) settles the state an interrupted
mutation left.

## Edge cases

These Cargo configurations are rejected until `cooldown` can snapshot their complete local input
closure:

- Cargo configuration `include`
- legacy `paths`
- path-backed config patches
- local registries
- file-backed registry indices, including `CARGO_REGISTRIES_<NAME>_INDEX` overrides

`cooldown` also rejects a temporary staging location whose ancestors contain Cargo configuration
outside the active Cargo home, or a Rust toolchain file. Those files could affect only the isolated
trial and not the source project Cargo first described. This check runs when staging is prepared.
Cargo discovers ancestor configuration again when each subprocess starts, so a file created in that
ancestor chain after the check remains an unavoidable race. Use a temporary directory hierarchy
that other users cannot modify when this local threat matters.

A symlink used to locate the Cargo project root is supported, because `cooldown` canonicalizes that
root before coordinating access. A symlink inside a writable project path, such as a symlinked
workspace member whose manifest may be rewritten, is rejected, because the project lease cannot
govern the resolved target safely.

### When cargo crashes

Cargo has been seen to crash with a signal, such as a segmentation fault, right after printing an
ordinary resolver conflict that the same command otherwise reports normally. A cargo read that a
signal kills runs once more:

- **A `--locked` read:** always.
- **A resolve in `cooldown`'s private staged copy:** while that copy is still the one `cooldown`
  prepared and its `Cargo.lock` still holds the bytes it had, or is still absent. Nothing is
  restored, so a resolve that changed the lock before it crashed is not run again.

The second run's result stands. Only a second crash fails the project, and its error says cargo was
killed by the signal after printing its output, instead of presenting that output as the cause.

## Fixing what goes wrong

A detected project without a `Cargo.lock` is a `stale_lock` error naming it. Its dependencies would
otherwise resolve to whatever is newest at build time, which is exactly what the gate exists to
prevent. To fix it:

1. Generate and commit the lock with `cargo generate-lockfile`; `check --lock` and
   `outdated --lock` do it in place.
2. Run `cooldown fix` to mature the fresh resolve.

`--allow-stale-lock` downgrades the failure to a warning and skips the project on `check`,
`outdated`, `upgrade`, and `fix`; `baseline` still fails on it. A clean summary then covers one
project fewer. [`exclude-folders`](https://romnn.github.io/cooldown/docs/configuration/excludes/) drops a crate that
is never built.
