TypeScript
fix(install): stable installs no longer fail behind a page of nightlies (#22907)
Theo Browne03d1a909153d
diff --git a/scripts/install.ps1 b/scripts/install.ps1index f440ebaa119..60a70fefc72 100644--- a/scripts/install.ps1+++ b/scripts/install.ps1@@ -133,8 +133,19 @@ if (-not $version) {"preview" { '^v\d+\.\d+\.\d+-preview\.\d+\.\d+$' }default { Fail "T3CODE_CHANNEL must be stable, nightly, or preview" }}- $releases = Invoke-RestMethod -Uri "https://api.github.com/repos/$repo/releases?per_page=100" -Headers @{ "User-Agent" = "t3-install" }- $tag = ($releases | Where-Object { -not $_.draft -and $_.tag_name -match $tagPattern } | Select-Object -First 1).tag_name+ if ($channel -eq "stable") {+ # release.yml marks only plain stable releases latest. This is a github.com+ # page, so it is not cut off by a page of newer nightlies or the API quota.+ $release = Invoke-RestMethod -Uri "https://github.com/$repo/releases/latest" -Headers @{ "User-Agent" = "t3-install"; "Accept" = "application/json" }+ $tag = ($release | Where-Object { $_.tag_name -match $tagPattern }).tag_name+ } else {+ $tag = $null+ for ($page = 1; -not $tag -and $page -le 10; $page++) {+ $releases = Invoke-RestMethod -Uri "https://api.github.com/repos/$repo/releases?per_page=100&page=$page" -Headers @{ "User-Agent" = "t3-install" }+ $tag = ($releases | Where-Object { -not $_.draft -and $_.tag_name -match $tagPattern } | Select-Object -First 1).tag_name+ if (-not $releases) { break }+ }+ }if (-not $tag) { Fail "could not find a $channel release; set T3CODE_VERSION" }$version = $tag.Substring(1)}diff --git a/scripts/install.sh b/scripts/install.shindex 20a0c025153..4c164c45040 100755--- a/scripts/install.sh+++ b/scripts/install.sh@@ -53,14 +53,14 @@ step "Finding your release..."# Exit 44 on a 404 so callers can tell "no such asset" from a network failure.fetch() {if command -v curl >/dev/null 2>&1; then- status="$(curl -sSL -w '%{http_code}' "$1" -o "$2")" || return 1+ status="$(curl -sSL -H "Accept: ${3:-*/*}" -w '%{http_code}' "$1" -o "$2")" || return 1case "$status" in2??) return 0 ;;404) return 44 ;;*) printf 'GET %s returned HTTP %s\n' "$1" "$status" >&2; return 1 ;;esacelif command -v wget >/dev/null 2>&1; then- wget -q --server-response "$1" -O "$2" 2>"$2.headers" && rm -f "$2.headers" && return 0+ wget -q --server-response --header="Accept: ${3:-*/*}" "$1" -O "$2" 2>"$2.headers" && rm -f "$2.headers" && return 0if grep -q ' 404 ' "$2.headers" 2>/dev/null; then rm -f "$2.headers"; return 44; ficat "$2.headers" >&2; rm -f "$2.headers"; return 1else@@ -151,8 +151,20 @@ if [ -z "$version" ]; then*) fail "T3CODE_CHANNEL must be stable, nightly, or preview" ;;esactmp_index="$(mktemp)"- fetch "https://api.github.com/repos/${repo}/releases?per_page=100" "$tmp_index"- version="$(sed -n "s/.*\"tag_name\": *\"${tag_pattern}\".*/\1/p" "$tmp_index" | head -n 1)"+ if [ "$channel" = stable ]; then+ # release.yml marks only plain stable releases latest. This is a github.com+ # page, so it is not cut off by a page of newer nightlies or the API quota.+ fetch "https://github.com/${repo}/releases/latest" "$tmp_index" application/json+ version="$(sed -n "s/.*\"tag_name\": *\"${tag_pattern}\".*/\1/p" "$tmp_index" | head -n 1)"+ else+ page=1+ while [ -z "$version" ] && [ "$page" -le 10 ]; do+ fetch "https://api.github.com/repos/${repo}/releases?per_page=100&page=${page}" "$tmp_index"+ version="$(sed -n "s/.*\"tag_name\": *\"${tag_pattern}\".*/\1/p" "$tmp_index" | head -n 1)"+ grep -q '"tag_name"' "$tmp_index" || break+ page=$((page + 1))+ done+ firm -f "$tmp_index"[ -n "$version" ] || fail "could not find a ${channel} release; set T3CODE_VERSION"fidiff --git a/scripts/install.test.ts b/scripts/install.test.tsnew file mode 100644index 00000000000..5be7acc8e00--- /dev/null+++ b/scripts/install.test.ts@@ -0,0 +1,80 @@+// @effect-diagnostics nodeBuiltinImport:off - Drives the real shell installer against a local GitHub fixture.+import * as HostProcess from "@t3tools/shared/HostProcess";+import * as NodeChildProcess from "node:child_process";+import * as NodeFSP from "node:fs/promises";+import * as NodeHttp from "node:http";+import * as NodeOS from "node:os";+import * as NodePath from "node:path";+import { describe, expect, it } from "vite-plus/test";++const nightlies = Array.from({ length: 100 }, (_, index) => ({+ tag_name: `v1.2.4-nightly.20261020.${200 - index}`,+}));++// regression: #22490+describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")("installer channel lookup", () => {+ it.each([+ ["stable", "1.2.3"],+ ["preview", "1.2.4-preview.20261019.50"],+ ])("finds the newest %s release behind a full page of nightlies", async (channel, version) => {+ const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-install-index-"));+ const target = NodePath.join(root, "home/runtime/versions", version);+ const server = NodeHttp.createServer((request, response) => {+ const url = new URL(request.url ?? "/", "http://fixture");+ const body =+ url.pathname === "/pingdotgg/t3code/releases/latest" &&+ request.headers.accept === "application/json"+ ? { tag_name: "v1.2.3" }+ : url.pathname === "/repos/pingdotgg/t3code/releases"+ ? { "1": nightlies, "2": [{ tag_name: `v${version}` }, { tag_name: "v1.2.3" }] }[+ url.searchParams.get("page") ?? ""+ ]+ : undefined;+ if (body === undefined) response.statusCode = 404;+ // The API pretty-prints and the installer reads it line by line.+ response.end(JSON.stringify(body ?? [], null, 2));+ });+ try {+ // An existing completed install isolates release resolution from archive downloads.+ await NodeFSP.mkdir(target, { recursive: true });+ await NodeFSP.writeFile(NodePath.join(target, ".install-complete"), `${version}\n`);+ await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));+ const address = server.address();+ if (!address || typeof address === "string") throw new Error("Expected a TCP listener");+ const origin = `http://127.0.0.1:${address.port}`;+ const source = await NodeFSP.readFile(+ NodePath.resolve(import.meta.dirname, "install.sh"),+ "utf8",+ );+ const installer = NodePath.join(root, "install.sh");+ await NodeFSP.writeFile(installer, source.replace(/https:\/\/(api\.)?github\.com/g, origin));+ const { code, output } = await new Promise<{ code: number | null; output: string }>(+ (resolve, reject) => {+ const child = NodeChildProcess.spawn("sh", [installer], {+ env: {+ ...process.env,+ T3CODE_VERSION: "",+ T3CODE_CHANNEL: channel,+ T3CODE_HOME: NodePath.join(root, "home"),+ T3CODE_INSTALL_BIN_DIR: NodePath.join(root, "bin"),+ // A wrong version must fail here instead of downloading a real release.+ T3CODE_RELEASE_BASE_URL: origin,+ },+ stdio: ["ignore", "pipe", "pipe"],+ });+ let output = "";+ child.stdout.on("data", (chunk: Buffer) => (output += chunk.toString()));+ child.stderr.on("data", (chunk: Buffer) => (output += chunk.toString()));+ child.on("error", reject);+ child.on("close", (code) => resolve({ code, output }));+ },+ );+ expect(code, output).toBe(0);+ expect(output).toContain(`Installed T3 Code ${version}`);+ } finally {+ server.closeAllConnections();+ await new Promise<void>((resolve) => server.close(() => resolve()));+ await NodeFSP.rm(root, { recursive: true, force: true });+ }+ });+});