Skip to content
Merged
7 changes: 7 additions & 0 deletions changelog.d/prerelease-rust-candidates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- **Pre-release candidates publish the four Rust crates.** `bun run prerelease:publish --only rust`
(alias `cargo`, and part of `--only all`) publishes `metaobjects`, `metaobjects-render`,
`metaobjects-codegen` and `metaobjects-cli` to the private registry's Cargo index in dependency
order, so the sibling-crate publish path is proven before the crates.io cut. Sibling dependencies
are pinned to the private registry for the publish and restored afterwards. A Rust consumer adds
the registry in `.cargo/config.toml` (see `docs/features/prerelease.md`, "Rust"); candidates
before `1.3.0-rc.6` still pin a commit.
46 changes: 39 additions & 7 deletions docs/features/prerelease.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ releases with a verified one-command revert.

| | |
|---|---|
| Registry | one Gitea instance serving npm, PyPI, NuGet and Maven — the address is configuration (`MO_REGISTRY_BASE`), never a committed default; see [§7](#7-running-your-own-registry) |
| Registry | one Gitea instance serving npm, PyPI, NuGet, Maven and Cargo — the address is configuration (`MO_REGISTRY_BASE`), never a committed default; see [§7](#7-running-your-own-registry) |
| Reads | **anonymous** — a consumer needs the URL and the owner, no account and no token |
| Writes | token only, in gitignored local config, never in a committed file |
| Publisher | `bun run prerelease:publish` (`scripts/prerelease.mjs`) |
Expand Down Expand Up @@ -46,7 +46,7 @@ to be talked out of it. One forgotten flag burns a version number permanently.

`<N>` is a
**monotonic iteration counter**, derived from what the registry already holds across all
four ecosystems so that `--only npm` today and `--only csharp` tomorrow cannot collide.
five ecosystems so that `--only npm` today and `--only csharp` tomorrow cannot collide.

**Why a counter and not a commit sha.** npm *strips* SemVer build metadata:
`0.24.0-rc.1+aaa` and `0.24.0-rc.1+bbb` compare **equal**, so the second publish is refused
Expand Down Expand Up @@ -104,7 +104,7 @@ Then:
```bash
bun run prerelease:publish # next iteration, npm + Maven (the default scope)
bun run prerelease:publish --only python,csharp # pick ports
bun run prerelease:publish --only all # all four
bun run prerelease:publish --only all # all five (npm, python, csharp, java, rust)
bun run prerelease:publish --iter 7 # pin the iteration number
bun run prerelease:publish --base 0.25.0 # target a different in-development version
bun run prerelease:publish --dry-run # build + normalize + gate, publish nothing
Expand Down Expand Up @@ -150,7 +150,7 @@ check that gates the final cut (`docs/RELEASING.md`, Step 0).
bun run prerelease:candidate # = bun run prerelease:publish --only all
```

The default scope is npm and Maven only, so a Python or a C# estate gets nothing from a bare
The default scope is npm and Maven only, so a Python, C# or Rust estate gets nothing from a bare
`prerelease:publish`. Use the full command for any candidate an estate in those ports will run;
name a narrower `--only` only for a fix you are iterating on one port.

Expand All @@ -172,9 +172,39 @@ pushed: a sha nobody can fetch is not a record.

### Rust

There is no Rust candidate on the private registry: the four crates are ready to publish and not
published, and the registry serves npm, PyPI, NuGet and Maven. A Rust consumer **pins the commit** the
candidate's row in the log names:
The four crates (`metaobjects`, `metaobjects-render`, `metaobjects-codegen`, `metaobjects-cli`) publish
to the registry's Cargo index with the other ports: `--only rust` (or its alias `--only cargo`) alone,
and `--only all` as part of a full candidate, after the other ecosystems. They publish in the order
`scripts/release.mjs` uses for crates.io (each crate after the ones it depends on), at the candidate's
SemVer form, `1.3.0-rc.N`. The run edits the manifests for the publish and restores them on exit: it
adds `registry = "mo-prerelease"` to each sibling-crate dependency, so a published crate's siblings
resolve from the private registry and never from crates.io or git. The publish names the registry
explicitly, the crates.io token variable is removed from cargo's environment, and a dry run of the whole
workspace must report the private index before anything is uploaded. Before any ecosystem publishes, the
run also refuses a candidate number the registry already holds for any crate.

Add the registry to the consumer's `.cargo/config.toml` (reads are anonymous; `<registry-base>` and
`<owner>` are the same values `registry.env` carries):

```toml
[registries.mo-prerelease]
index = "sparse+<registry-base>/api/packages/<owner>/cargo/"
```

```toml
[dependencies]
metaobjects = { version = "=1.3.0-rc.6", registry = "mo-prerelease" }
```

```bash
cargo install metaobjects-cli --version 1.3.0-rc.6 --registry mo-prerelease
```

A crate's other dependencies still come from crates.io; only the `metaobjects*` crates come from the
private registry. Cargo resolves a candidate only for an exact requirement such as `=1.3.0-rc.6`.

**Fallback: pin the commit.** A candidate published before the crates joined it (`1.3.0-rc.5` and earlier)
has no crates on the registry, and the commit its row in the log names still works:

```toml
[dependencies]
Expand Down Expand Up @@ -213,6 +243,8 @@ Everything else keeps resolving from the public registry — verified: `zod` and
still come from npmjs.org and pypi.org while the vendor packages come from the pre-release
registry.

`link` does not configure Cargo: a Rust consumer adds the registry by hand, as [§2a Rust](#rust) shows.

Everything it writes is delimited by managed markers, so `unlink` removes exactly what
`link` added:

Expand Down
69 changes: 69 additions & 0 deletions scripts/lib/rust-release.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,24 @@ export function bumpRustManifest(text, version) {
return out;
}

/**
* A crate manifest cut as a pre-release candidate for a PRIVATE registry: `bumpRustManifest`'s
* version edit, plus `registry = "<name>"` on every sibling dependency given by `path`. Without
* it a published crate's sibling dependency names no registry at all, and cargo resolves it from
* crates.io, where no candidate exists. Nothing else in the text changes.
*
* @param {string} text
* @param {string} version
* @param {string} registry the name the private registry is configured under in cargo
* @returns {string}
*/
export function candidateRustManifest(text, version, registry) {
return bumpRustManifest(text, version).split(/(?<=\n)/).map((line) =>
/\bpath\s*=/.test(line) && !/\bregistry\s*=/.test(line)
? line.replace(/(\bversion\s*=\s*"[^"]*")/, `$1, registry = "${registry}"`)
: line).join("");
}

/**
* Whether crates.io already holds `version` of a crate, from the JSON its
* `/api/v1/crates/<name>` endpoint answers. A crate that does not exist answers 404, which
Expand All @@ -192,3 +210,54 @@ export function bumpRustManifest(text, version) {
export function crateVersionTaken(json, version) {
return (json.versions ?? []).some((v) => v.num === version);
}

/**
* The environment variable cargo reads a named registry's index address from.
* @param {string} registry
* @returns {string}
*/
export const cargoIndexVar = (registry) => `CARGO_REGISTRIES_${registry.toUpperCase().replaceAll("-", "_")}_INDEX`;

/** Where crates.io lives, in each form cargo accepts: the sparse index and the retired git index. */
const CRATES_IO = /(^|[/@.])(crates\.io|index\.crates\.io|crates-io)([/:.]|$)|rust-lang\/crates\.io-index/;

/**
* Refuse a `cargo publish` invocation that could reach anywhere but the private registry.
* `args` is the argument list after `cargo publish`; `env` is the child environment.
* It proves the target from what cargo will be GIVEN, because a dry run prints no target.
*
* @param {{args: string[], env: Record<string, string | undefined>, registry: string, expectedIndex: string}} input
* @throws when the invocation is not bound to exactly the expected private registry.
*/
export function assertCargoPublishTarget({ args, env, registry, expectedIndex }) {
const at = args.indexOf("--registry");
if (at === -1 || args[at + 1] !== registry || args.indexOf("--registry", at + 1) !== -1)
throw new Error(`cargo publish must name exactly one --registry, "${registry}"; got [${args.join(" ")}]`);
const banned = args.filter((a) => a === "--index" || a === "--token" || a.startsWith("--index=") || a.startsWith("--token="));
if (banned.length) throw new Error(`cargo publish must not set ${banned.join(", ")}: the target is the named registry`);
const index = env[cargoIndexVar(registry)];
if (index !== expectedIndex)
throw new Error(`cargo would read registry "${registry}" from ${index ?? "nothing"}, not the configured ${expectedIndex}`);
if (CRATES_IO.test(expectedIndex)) throw new Error(`the expected index ${expectedIndex} is crates.io`);
for (const v of ["CARGO_REGISTRY_TOKEN", "CARGO_REGISTRY_DEFAULT", "CARGO_REGISTRIES_CRATES_IO_TOKEN", "CARGO_REGISTRIES_CRATES_IO_INDEX"])
if (env[v]) throw new Error(`${v} is set in the cargo environment: it could redirect or authorize a crates.io publish`);
}

/**
* Read what cargo printed and refuse anything that names a target other than the private registry.
* Cargo reports a publish as ``Uploaded <crate> to registry `<name>` `` and
* ``Published <crate> at registry `<name>` ``, and states a registry's index address in a
* ``name of alternative registry `<index>` `` note; each must match. A dry run prints none of the
* first two, so `published` demands them for a real publish.
*
* @param {{output: string, registry: string, expectedIndex: string, published?: boolean}} input
* @throws when the output names another registry, or a real publish never confirms this one.
*/
export function assertCargoPublishOutput({ output, registry, expectedIndex, published = false }) {
for (const m of output.matchAll(/\b(?:Uploaded|Published)\b[^\n]*?\b(?:to|at) registry `([^`]*)`/g))
if (m[1] !== registry) throw new Error(`cargo reports a publish to registry \`${m[1]}\`, not \`${registry}\`: ${m[0].trim()}`);
for (const m of output.matchAll(/alternative registry `([^`]*)`/g))
if (m[1] !== expectedIndex) throw new Error(`cargo reports registry index ${m[1]}, not the configured ${expectedIndex}`);
if (published && !new RegExp(`Published [^\\n]* at registry \`${registry}\``).test(output))
throw new Error(`cargo never confirmed a publish to registry \`${registry}\``);
}
2 changes: 1 addition & 1 deletion scripts/prerelease-version.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export const CANDIDATE_LOG_HEADER =

/**
* One row of the candidate log. `candidate` is the canonical `<base>-rc.<N>`; `registries` the
* ecosystems actually published (npm, python, csharp, java).
* ecosystems actually published (npm, python, csharp, java, rust).
*/
export function candidateLogRow({ candidate, commit, dirty, date, registries }) {
if (!/^\d+\.\d+\.\d+-rc\.\d+$/.test(candidate)) throw new Error(`not a candidate version: ${candidate}`);
Expand Down
Loading
Loading