Public: anyone can read this; the crew can change it.
Defect of #13042: production runs unstamped (version "dev", release null) at main's tip, so the 1.0.0 line cannot pass #15500
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15497-defect-of-13042-production-runs-unstampe → main 0 commits fork
6 files
changed +194
−4
docs/APPS.md +1
| @@ -778,6 +778,7 @@ The forge starts the app with: | ||
| 778 | 778 | | `FORGE_APP_DATA` | this environment's data directory, created for it | |
| 779 | 779 | | `FORGE_APP_URL` | the public URL this environment is served at | |
| 780 | 780 | | `FORGE_APP_COMMIT` | the commit being run | |
| 781 | | `FORGE_APP_RELEASE` | on production, the newest `vX.Y.Z` tag at or below that commit, as `x.y.z`; unset when none, and never on a preview | | |
| 781 | 782 | | `FORGE_APP_PULL` | the pull number, on a preview | |
| 782 | 783 | | `FORGE_APP_SSH` | the exact `ssh …` that opens this environment's terminal, when SSH is on | |
| 783 | 784 | | `FORGE_URL` | the forge's own base URL | |
src/apps/process.ts +11
| @@ -75,6 +75,10 @@ export interface StartInput { | ||
| 75 | 75 | readonly feature?: Record<string, string> | undefined; |
| 76 | 76 | /** The process's read-only credential into the forge (`apps/token.ts`). */ |
| 77 | 77 | readonly forgeToken?: string | undefined; |
| 78 | /** | |
| 79 | * The newest release tag at or below the commit, `x.y.z`, on production (`apps/release_at.ts`). | |
| 80 | */ | |
| 81 | readonly release?: string | undefined; | |
| 78 | 82 | readonly log: (line: string) => void; |
| 79 | 83 | /** Aborting cancels whatever step is in flight and fails the start. */ |
| 80 | 84 | readonly signal: AbortSignal; |
| @@ -192,6 +196,8 @@ export function appEnv(input: { | ||
| 192 | 196 | forgeToken?: string | undefined; |
| 193 | 197 | /** A pinned toolchain's PATH and homes (`apps/toolchain.ts`). */ |
| 194 | 198 | toolchain?: Record<string, string> | undefined; |
| 199 | /** The release the commit is at, `x.y.z`; production only (`apps/release_at.ts`). */ | |
| 200 | release?: string | undefined; | |
| 195 | 201 | }): Record<string, string> { |
| 196 | 202 | const pull = envPull(input.env); |
| 197 | 203 | // The operator's variables go underneath everything the forge names, so |
| @@ -210,6 +216,10 @@ export function appEnv(input: { | ||
| 210 | 216 | FORGE_APP_DATA: input.dataDir, |
| 211 | 217 | FORGE_APP_URL: input.url, |
| 212 | 218 | FORGE_APP_COMMIT: input.commitOid, |
| 219 | // A preview runs a pull, which is no release: only production is told one. | |
| 220 | ...(input.env === "production" && input.release !== undefined && /^\d+\.\d+\.\d+$/.test(input.release) | |
| 221 | ? { FORGE_APP_RELEASE: input.release } | |
| 222 | : {}), | |
| 213 | 223 | FORGE_URL: input.forgeUrl, |
| 214 | 224 | FORGE_API_URL: forgeApiUrl(input.forgeUrl), |
| 215 | 225 | FORGE_APP_IDENTITY_KEY: input.identityKey, |
| @@ -728,6 +738,7 @@ export async function startProcess( | ||
| 728 | 738 | vars: input.vars, |
| 729 | 739 | feature: input.feature, |
| 730 | 740 | forgeToken: input.forgeToken, |
| 741 | release: input.release, | |
| 731 | 742 | }); |
| 732 | 743 | let proc: ReturnType<typeof Bun.spawn<"ignore", "pipe", "pipe">>; |
| 733 | 744 | try { |
src/apps/release_at.ts added +35
| @@ -0,0 +1,35 @@ | ||
| 1 | /** | |
| 2 | * The release a production commit is at (#15497): the newest `vX.Y.Z` tag at or below it. | |
| 3 | * | |
| 4 | * An app's tree is exported without its history (`checkout.ts`), so the app cannot read its own | |
| 5 | * tags; the forge reads them from the bare repository and hands the answer over as | |
| 6 | * `FORGE_APP_RELEASE` (`process.ts`). Only a production deploy is told: a preview runs a pull, | |
| 7 | * and a pull is no release, so it never passes for the one beneath it. | |
| 8 | */ | |
| 9 | ||
| 10 | import { runGit } from "../git/env.ts"; | |
| 11 | import { isSafeOid } from "../git/guard.ts"; | |
| 12 | ||
| 13 | const TAG = /^v(\d+)\.(\d+)\.(\d+)$/; | |
| 14 | ||
| 15 | /** The newest `x.y.z` among tag names; anything that is not `vX.Y.Z` is passed over. */ | |
| 16 | export function newestRelease(tags: readonly string[]): string | undefined { | |
| 17 | let best: [number, number, number] | undefined; | |
| 18 | for (const name of tags) { | |
| 19 | const m = TAG.exec(name.trim()); | |
| 20 | if (m === null) continue; | |
| 21 | const v: [number, number, number] = [Number(m[1]), Number(m[2]), Number(m[3])]; | |
| 22 | if (best === undefined || (v[0] - best[0] || v[1] - best[1] || v[2] - best[2]) > 0) best = v; | |
| 23 | } | |
| 24 | return best === undefined ? undefined : best.join("."); | |
| 25 | } | |
| 26 | ||
| 27 | /** | |
| 28 | * The newest release tag reachable from `oid` in the bare repository; undefined when none or git | |
| 29 | * cannot say. | |
| 30 | */ | |
| 31 | export async function releaseAt(bare: string, oid: string): Promise<string | undefined> { | |
| 32 | if (!isSafeOid(oid)) return undefined; | |
| 33 | const out = await runGit(["tag", "--list", "--merged", oid, "v[0-9]*"], { cwd: bare }).catch(() => undefined); | |
| 34 | return out?.ok === true ? newestRelease(out.value.split("\n")) : undefined; | |
| 35 | } |
src/apps/runtime_bringup.ts +2
| @@ -19,6 +19,7 @@ import { exportTree, readFileAt } from "./checkout.ts"; | ||
| 19 | 19 | import { Stopwatch } from "./timing.ts"; |
| 20 | 20 | import { ensureEnvData, envDataDir, envDir, envPull, reseedEmpty, waitedEmptyNote } from "./data.ts"; |
| 21 | 21 | import { parseManifest } from "./manifest.ts"; |
| 22 | import { releaseAt } from "./release_at.ts"; | |
| 22 | 23 | import { identityKey } from "./proxy.ts"; |
| 23 | 24 | import { startProcess, type StartInput } from "./process.ts"; |
| 24 | 25 | import type { AppsRuntime } from "./runtime.ts"; |
| @@ -119,6 +120,7 @@ export async function bringUp( | ||
| 119 | 120 | env: state.env, |
| 120 | 121 | url: rt.urlFor(state.app, state.env), |
| 121 | 122 | commitOid: run.commitOid, |
| 123 | ...(state.env === "production" ? { release: await releaseAt(bare, run.commitOid) } : {}), | |
| 122 | 124 | forgeUrl: rt.options.forgeUrl, |
| 123 | 125 | identityKey: identityKey(rt.app.config.dataDir, state.app.id), |
| 124 | 126 | ssh: rt.sshFor(state.app, state.env), |
src/kernel/version.ts +12 −4
| @@ -24,6 +24,11 @@ export interface Running { | ||
| 24 | 24 | readonly version: string; |
| 25 | 25 | /** The commit that is running, in full, or null when nothing says. */ |
| 26 | 26 | readonly commit: string | null; |
| 27 | /** | |
| 28 | * The release the commit is at, `x.y.z`, when the forge that runs this one as its production | |
| 29 | * app says so (`FORGE_APP_RELEASE`, `apps/release_at.ts`, #15497); absent otherwise. | |
| 30 | */ | |
| 31 | readonly release?: string; | |
| 27 | 32 | } |
| 28 | 33 | |
| 29 | 34 | /** What an unstamped forge calls itself; the `Dockerfile`'s own default. */ |
| @@ -31,6 +36,7 @@ export const UNSTAMPED = "dev"; | ||
| 31 | 36 | |
| 32 | 37 | const VERSION = /^[0-9A-Za-z][0-9A-Za-z.+_-]{0,63}$/; |
| 33 | 38 | const COMMIT = /^[0-9a-f]{7,64}$/; |
| 39 | const RELEASE = /^\d+\.\d+\.\d+$/; | |
| 34 | 40 | |
| 35 | 41 | export function running( |
| 36 | 42 | env: Readonly<Record<string, string | undefined>> = process.env, |
| @@ -40,9 +46,11 @@ export function running( | ||
| 40 | 46 | const commit = (env["FORGE_COMMIT"] || env["FORGE_APP_COMMIT"] || "") |
| 41 | 47 | .trim() |
| 42 | 48 | .toLowerCase(); |
| 49 | const release = (env["FORGE_APP_RELEASE"] ?? "").trim().replace(/^v(?=\d)/, ""); | |
| 43 | 50 | return { |
| 44 | 51 | version: VERSION.test(version) ? version : UNSTAMPED, |
| 45 | 52 | commit: COMMIT.test(commit) ? commit : null, |
| 53 | ...(RELEASE.test(release) ? { release } : {}), | |
| 46 | 54 | }; |
| 47 | 55 | } |
| 48 | 56 | |
| @@ -56,15 +64,15 @@ export function runningLine(r: Running = running()): string { | ||
| 56 | 64 | /** The short id of a commit, seven characters: the one form every door prints. */ |
| 57 | 65 | export const shortOf = (commit: string): string => commit.slice(0, 7); |
| 58 | 66 | |
| 59 | const RELEASE = /^\d+\.\d+\.\d+$/; | |
| 60 | ||
| 61 | 67 | /** |
| 62 | 68 | * The release that is running as `x.y.z`, or null when the build names none (#13263): a probe |
| 63 | 69 | * reads it as a version, so an unstamped forge's `dev`, or any stamp that is not a release |
| 64 | * number, is null here, never a word in a version's place. | |
| 70 | * number, is null here, never a word in a version's place. An unstamped forge run as another | |
| 71 | * forge's production app names the release its commit is at (#15497). | |
| 65 | 72 | */ |
| 66 | 73 | export function releaseOf(r: Running = running()): string | null { |
| 67 | return RELEASE.test(r.version) ? r.version : null; | |
| 74 | if (RELEASE.test(r.version)) return r.version; | |
| 75 | return r.release !== undefined && RELEASE.test(r.release) ? r.release : null; | |
| 68 | 76 | } |
| 69 | 77 | |
| 70 | 78 | /** |
test/app-release-15497.test.ts added +133
| @@ -0,0 +1,133 @@ | ||
| 1 | /** | |
| 2 | * A forge run as another forge's production app names the release its commit is at (#15497). | |
| 3 | * | |
| 4 | * Production ran main's tip as `dev` with `release: null`: the app's tree is exported without | |
| 5 | * history, so nothing in it could name a tag. The hosting forge now reads the newest `vX.Y.Z` at | |
| 6 | * or below the commit from the bare repository and hands it over as `FORGE_APP_RELEASE`; | |
| 7 | * `/api/v1/health` reads it back as `release`. A preview is never told one. | |
| 8 | */ | |
| 9 | ||
| 10 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 11 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 12 | import { tmpdir } from "node:os"; | |
| 13 | import { join } from "node:path"; | |
| 14 | import { createApp, type App } from "../src/app.ts"; | |
| 15 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 16 | import { appEnv } from "../src/apps/process.ts"; | |
| 17 | import { newestRelease, releaseAt } from "../src/apps/release_at.ts"; | |
| 18 | import { releaseOf, running } from "../src/kernel/version.ts"; | |
| 19 | ||
| 20 | const git = (cwd: string, ...args: string[]): string => { | |
| 21 | const r = Bun.spawnSync(["git", "-c", "user.name=t", "-c", "[email protected]", "-c", "tag.gpgSign=false", "-c", "commit.gpgSign=false", ...args], { cwd }); | |
| 22 | if (r.exitCode !== 0) throw new Error(`git ${args.join(" ")}: ${r.stderr.toString()}`); | |
| 23 | return r.stdout.toString().trim(); | |
| 24 | }; | |
| 25 | ||
| 26 | describe("the release a commit is at", () => { | |
| 27 | test("the newest x.y.z wins, by number, and nothing else is a release", () => { | |
| 28 | expect(newestRelease(["v0.7.9", "v0.7.629", "v0.7.62", "v0.0.0-pr.1", "latest", "v1"])).toBe("0.7.629"); | |
| 29 | expect(newestRelease(["v1.0.0", "v0.9.99"])).toBe("1.0.0"); | |
| 30 | expect(newestRelease(["nightly", ""])).toBeUndefined(); | |
| 31 | }); | |
| 32 | ||
| 33 | let repo: string; | |
| 34 | const at: Record<string, string> = {}; | |
| 35 | beforeAll(async () => { | |
| 36 | repo = await mkdtemp(join(tmpdir(), "release-at-")); | |
| 37 | git(repo, "init", "-q", "-b", "main"); | |
| 38 | const commit = (name: string) => { | |
| 39 | git(repo, "commit", "-q", "--allow-empty", "-m", name); | |
| 40 | at[name] = git(repo, "rev-parse", "HEAD"); | |
| 41 | }; | |
| 42 | commit("a"); | |
| 43 | git(repo, "tag", "-a", "-m", "r", "v0.7.1"); | |
| 44 | commit("b"); | |
| 45 | git(repo, "tag", "-a", "-m", "r", "v0.7.2"); | |
| 46 | commit("tip"); | |
| 47 | // A release on another line is not below the tip. | |
| 48 | git(repo, "checkout", "-q", "-b", "side", at["a"]!); | |
| 49 | commit("side"); | |
| 50 | git(repo, "tag", "-a", "-m", "r", "v0.9.0"); | |
| 51 | git(repo, "checkout", "-q", "main"); | |
| 52 | }); | |
| 53 | afterAll(async () => { | |
| 54 | await rm(repo, { recursive: true, force: true }); | |
| 55 | }); | |
| 56 | ||
| 57 | test("main's tip, past its last tag, is at that tag; a tagged commit is at its own", async () => { | |
| 58 | expect(await releaseAt(repo, at["tip"]!)).toBe("0.7.2"); | |
| 59 | expect(await releaseAt(repo, at["b"]!)).toBe("0.7.2"); | |
| 60 | expect(await releaseAt(repo, at["a"]!)).toBe("0.7.1"); | |
| 61 | expect(await releaseAt(repo, at["side"]!)).toBe("0.9.0"); | |
| 62 | }); | |
| 63 | ||
| 64 | test("a commit below every tag, or not a commit id, is at none", async () => { | |
| 65 | git(repo, "checkout", "-q", "--orphan", "fresh"); | |
| 66 | git(repo, "commit", "-q", "--allow-empty", "-m", "fresh"); | |
| 67 | const fresh = git(repo, "rev-parse", "HEAD"); | |
| 68 | git(repo, "checkout", "-q", "main"); | |
| 69 | expect(await releaseAt(repo, fresh)).toBeUndefined(); | |
| 70 | expect(await releaseAt(repo, "--all")).toBeUndefined(); | |
| 71 | }); | |
| 72 | ||
| 73 | test("production is told it and reads it as its release; a preview is not told", async () => { | |
| 74 | const release = await releaseAt(repo, at["tip"]!); | |
| 75 | const input = { | |
| 76 | port: 1, | |
| 77 | dataDir: "/d", | |
| 78 | url: "http://lab.test", | |
| 79 | commitOid: at["tip"]!, | |
| 80 | forgeUrl: "http://forge.test", | |
| 81 | identityKey: "k", | |
| 82 | release, | |
| 83 | }; | |
| 84 | const prod = appEnv({ ...input, env: "production" }); | |
| 85 | expect(prod["FORGE_APP_RELEASE"]).toBe("0.7.2"); | |
| 86 | expect(releaseOf(running(prod))).toBe("0.7.2"); | |
| 87 | // The stamp stays what the build says: `dev`, so the keeper never takes this for a cut. | |
| 88 | expect(running(prod).version).toBe("dev"); | |
| 89 | const preview = appEnv({ ...input, env: "pull-12" }); | |
| 90 | expect(preview["FORGE_APP_RELEASE"]).toBeUndefined(); | |
| 91 | expect(releaseOf(running(preview))).toBeNull(); | |
| 92 | // Only a release number is passed on. | |
| 93 | expect(appEnv({ ...input, env: "production", release: "dev; rm" })["FORGE_APP_RELEASE"]).toBeUndefined(); | |
| 94 | }); | |
| 95 | ||
| 96 | test("a stamped build's own version still wins over what it is told", () => { | |
| 97 | expect(releaseOf(running({ FORGE_VERSION: "0.8.0", FORGE_APP_RELEASE: "0.7.2" }))).toBe("0.8.0"); | |
| 98 | expect(releaseOf(running({ FORGE_APP_RELEASE: "nope" }))).toBeNull(); | |
| 99 | }); | |
| 100 | }); | |
| 101 | ||
| 102 | describe("/api/v1/health on an unstamped production app", () => { | |
| 103 | let dataDir: string; | |
| 104 | let app: App; | |
| 105 | let servers: Servers; | |
| 106 | const keys = ["FORGE_VERSION", "FORGE_COMMIT", "FORGE_APP_COMMIT", "FORGE_APP_RELEASE"] as const; | |
| 107 | const before = Object.fromEntries(keys.map((k) => [k, process.env[k]])); | |
| 108 | beforeAll(async () => { | |
| 109 | delete process.env["FORGE_VERSION"]; | |
| 110 | delete process.env["FORGE_COMMIT"]; | |
| 111 | process.env["FORGE_APP_COMMIT"] = "1701de1790eb21474f584fed5b02accf0b839434"; | |
| 112 | process.env["FORGE_APP_RELEASE"] = "0.7.629"; | |
| 113 | dataDir = await mkdtemp(join(tmpdir(), "app-release-")); | |
| 114 | app = createApp({ dataDir, port: 0 }); | |
| 115 | servers = await serve(app); | |
| 116 | }); | |
| 117 | afterAll(async () => { | |
| 118 | for (const k of keys) { | |
| 119 | if (before[k] === undefined) delete process.env[k]; | |
| 120 | else process.env[k] = before[k]; | |
| 121 | } | |
| 122 | servers.stop(); | |
| 123 | app.close(); | |
| 124 | await rm(dataDir, { recursive: true, force: true }); | |
| 125 | }); | |
| 126 | ||
| 127 | test("names the release its commit is at, not null", async () => { | |
| 128 | const health = (await (await fetch(`${servers.url}/api/v1/health`)).json()) as Record<string, unknown>; | |
| 129 | expect(health["release"]).toBe("0.7.629"); | |
| 130 | expect(health["commit"]).toBe("1701de1790eb21474f584fed5b02accf0b839434"); | |
| 131 | expect(health["version"]).toBe("dev"); | |
| 132 | }); | |
| 133 | }); |
new above
offline