Files
coder/scripts/docsredirectscheck
Ethan 267f1d8c9e fix: stop lint/docs-redirects depending on the live docs site (#32739)
`lint` has failed on every push to `main` and `release/2.40` since the
docs site was republished on the 24th, e.g.
https://github.com/coder/coder/actions/runs/45560160492/job/139021802160

coder/docs-engine#273 has the docs site generate one page per REST API
endpoint from `coderd/apidoc/swagger.json`, at
`reference/api/<section>/<endpoint>`, and list them in `llms.txt`.
`lint/docs-redirects` downloads that list, and treats any page on it
that isn't in `docs/manifest.json` as a page we removed, so it reported
all 389 endpoint pages:

```
docs route "reference/api/agents/authenticate-agent-on-aws-instance" no longer resolves (from the live page list https://coder.com/beta-docs/@main/llms.txt). Add a rule to docs/redirects.json, for example: {"source": "reference/api/agents/authenticate-agent-on-aws-instance", "destination": "/"}
```

Nothing is broken on the site, those pages are live, and I don't think
the docs engine is doing anything wrong either. The actual problem is
that `make lint` depends on whatever coder.com is serving at the time,
so a docs site release can turn `main` red without a commit here. It
also fails where nobody can act on it: a PR only downloads the list when
it changes `docs/manifest.json` or `docs/redirects.json`, whilst every
push to `main` downloads it, and that run is after the merge. So PRs
kept passing lint and merging onto a red `main`.

To fix, `lint/docs-redirects` now runs with `-offline`, and only
compares the manifest and redirects against the base branch. A PR that
moves or removes a page without a redirect still fails, as that
comparison never needed the download.

I think this is the right fix, rather than just teaching the check about
endpoint pages, because the download wasn't catching anything. The site
is built from the manifest, so a page can only be on the live list and
missing from the manifest if a PR removed it, in which case the base
comparison already fails that PR, or if the docs engine made the page
itself. Today 391 live pages aren't in the manifest, and all of them are
the second kind: the 389 endpoint pages, plus 2 pages the site serves
with hyphens for dots, which the check already special-cased. Endpoint
pages would be the second special case of that kind, and the next docs
engine change that adds pages would need a third.

What we lose is a second failure on `main` when a page removal is merged
past a failing lint, and that failure clears by itself once the site is
republished without the page. On a push there's no base branch to
compare against, so only the rules themselves are validated there.

Alternatives I considered:

- Only skipping the endpoint pages, which is what this PR did at first.
It fixes today's failure and leaves the cause in place, as above.
- Deriving the endpoint pages from `swagger.json` in the check, so
they're compared like any other page. That means copying the docs
engine's URL rules into this repo and keeping the two in step, in order
to catch renamed or removed endpoints. In the last 90 days on `main`
that was one removal and no renames.
- Deleting the live comparison outright. I've kept it, as it's still
useful to run by hand without `-offline`, e.g. after a change to how the
docs engine builds its routes. So that it's usable, the first commit
stays: routes of the endpoint page form are left out of that comparison
whilst their section page is still in the manifest, and it prints how
many were skipped. `go run ./scripts/docsredirectscheck -branch main`
goes from 389 failures to passing.
- Comparing against the previous commit on push runs, so that `main`
keeps a removed-route check. That's the comparison the PR already ran,
and a failure there comes after the merge.

The URL of an endpoint page changes whenever its `@Summary` or tag does,
and nothing here will notice. Catching that would mean requiring a
redirect on those API changes, which I've left out of this PR. This also
needs backporting to `release/2.40`.
2026-11-25 07:53:58 +00:00
..

docsredirectscheck

docsredirectscheck validates docs/redirects.json, the list of redirects the documentation websites apply for pages that have moved or been removed. It runs as make lint/docs-redirects (part of make lint and make lint-light), so a bad or missing redirect is caught in the pull request that introduces it.

Each branch has its own docs/redirects.json. The file on main applies to @main, and the file on release/X.Y applies to vX.Y and, while X.Y is the mainline release, to the unversioned docs. A missing docs/redirects.json passes.

The rule file

docs/redirects.json is a JSON array of rules:

[
  {
    "source": "about/architecture",
    "destination": "/"
  },
  {
    "source": "ai-coder/ai-gateway/*",
    "destination": "ai-governance/ai-gateway/*",
    "permanent": false
  }
]
  • source and destination are routes relative to the docs root. A route is the URL path below /docs with no version, no leading or trailing slash, and no .md, such as templates/extending-templates. Each segment matches [A-Za-z0-9._-]+.
  • permanent is optional and defaults to true. true answers 308 and false answers 307.
  • A source is an exact route, or a route ending in /*. A wildcard matches the prefix itself and every route beneath it.
  • A destination is an exact route, / for the docs root, or an https:// URL that starts with https://github.com/coder/ or https://coder.com/. An external destination has no : after the scheme, no *, no \, and no . or .. path segment, also when percent-encoded.
  • An exact route destination may end in one #fragment that matches [A-Za-z0-9._-]+, such as workspaces/workspace-access#other-web-ides. The route part must be a live route. / and wildcard destinations never carry a fragment.
  • A destination ends in /* only when the source does. The part the source's * matched replaces the destination's *. An empty match means the destination prefix itself, so ai-governance/ai-gateway/* with an empty match is ai-governance/ai-gateway. A wildcard source with an exact destination sends everything under the prefix to that one page.
  • Rules apply in file order and the first match wins, so they must run from most specific to least. Put an exact rule above any wildcard that would also match it.
  • Unknown fields are an error.

The old absolute form, such as /docs/admin/old or /docs/x/:path*, is rejected. The error shows the converted rule.

What it catches

These fail the check:

  • A malformed file. Not valid JSON, not an array, an entry that is not an object, a missing or empty source or destination, a non-boolean permanent, or an unknown field.
  • Unsupported syntax. A route outside the grammar above, the old /docs/... form, or an external destination outside the allowlist.
  • A destination that is not a page. An internal destination must be a route in docs/manifest.json. A wildcard destination must point into a section that has pages.
  • A source that hides a live page. A page always wins over a redirect, and some sites apply redirects first. A rule whose source matches a page in the manifest is therefore rejected.
  • Duplicates, chains, and loops. Two rules with the same source, a rule whose destination is another rule's source (point it at the final page instead), and cycles.
  • A rule that can never apply. An earlier rule whose source matches every route a later rule would. A wildcard above a rule inside its subtree is the usual cause. An exact rule above a wildcard on the same prefix is allowed, because it claims the bare prefix and leaves the subtree to the wildcard.
  • A removed route. See the next section.

Removed routes

A route that worked before the change must still work after it. Each route in the inventory must be a live route in the head manifest, or be resolved by the head rules to a live route, to /, or to an allowed external URL. The inventory is the union of:

  • The routes in docs/manifest.json on the base branch.
  • The routes in the live production page list for the target branch, when the run reads the list. make lint/docs-redirects does not read it. The list is https://coder.com/beta-docs/@main/llms.txt for main and https://coder.com/beta-docs/vX.Y/llms.txt for release/X.Y. Only links under that version count. The version served at the unversioned /beta-docs path links its pages without a version segment, so when a list has no links under its own segment, the unversioned links count instead. When the list redirects to /docs, links under /docs count the same way.
  • The source of every rule in docs/redirects.json on the base branch. A wildcard source contributes its prefix. A base file in the old absolute form is read as well.

A failure names the route, where it came from, and a rule to paste, for example {"source": "path/to/page", "destination": "/"}. The destination is the one live page with the same last segment. Otherwise it is the one page this change added in the same parent section, which catches a rename such as admin/integrations/istio to admin/integrations/istio-mesh. Otherwise it is /. Change the destination when a better page exists. In GitHub Actions each failure is also an error annotation.

Both ends of the check can be missing, and the check says so and skips that part:

  • Base branch. In GitHub Actions on a pull request the base is the first parent of the merge commit, which is the tip of the branch the change merges into. Elsewhere pass -base <rev>. A shallow checkout that lacks the revision skips the comparison with a note.
  • Live page list. The target branch is -branch, then GITHUB_BASE_REF on a pull request, then GITHUB_REF_NAME on a push. A branch other than main or release/X.Y, or no known branch, skips the list with a note. A list that answers 404 skips the list with a note, because a new release/X.Y branch has no published docs version yet. When docs/manifest.json and docs/redirects.json are the same as on the base branch, the list is skipped with a note, because the change cannot affect the result. Pass -offline to skip the list on purpose. make lint/docs-redirects passes it, so that lint depends only on the repository and not on what the docs site serves at the time. Run the command without -offline to compare against the live site as well.

A live page list that cannot be read for any other reason is an infrastructure failure and exits 2, not a pass. The request has a descriptive User-Agent, because the site answers 403 to some default clients, and it retries a few times before it gives up. An empty list is also a failure.

A base docs/redirects.json that does not exist means the base branch had no rules. Any other failure to read it skips the base redirect sources with a note.

Dotted segments

beta-docs currently serves a dotted segment with hyphens, so esr-2.24-2.29-upgrade is served as esr-2-24-2-29-upgrade. Until the docs engine fixes it, a live route that equals a head route with . replaced by - counts as present.

REST API endpoint pages

The docs site generates one page for each REST API endpoint from coderd/apidoc/swagger.json, at reference/api/<section>/<endpoint>, such as reference/api/users/get-user-by-name. docs/manifest.json lists only the section pages, such as reference/api/users, so the check cannot tell which endpoint pages exist. A route of that form in the live page list is therefore not checked while its section page is a live route, and the check prints how many it left out. Once the section page is gone, its endpoint routes need a rule like any other route.

The endpoint part of the URL comes from the endpoint's summary and the section from its tag. A change to either one moves the page, and the check does not notice.

How it decides which pages exist

It derives routes from docs/manifest.json as the docs websites do: strip a leading ./, then strip the suffixes README.md, index.md, and .md one after another, and trim slashes. That includes the websites' quirk of matching the suffixes as plain text rather than per path segment, so a file named my-index.md becomes the route .../my-. Every manifest entry with a path counts as a page, whether or not it appears in the sidebar. The empty route is the docs root, and it always exists.

Usage

go run ./scripts/docsredirectscheck [-manifest path] [-redirects path] [-base rev] [-branch name] [-offline]

Exit status is 0 when the file is valid or absent, 1 when it has problems, and 2 when the check itself could not run. Examples are an unreadable manifest or a live page list that cannot be read.

Limitations

  • Chains through a wildcard destination are not checked, because where such a destination lands depends on the route being redirected. The removed-route check does follow rules, up to 3 hops, whatever the last destination is.
  • Only the two source forms above are understood. Regular-expression groups, version ranges, and mid-path parameters are rejected rather than guessed at.
  • The check does not know whether an external destination is reachable.