Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
fix: Gate Builder releases on the published CLI contract (#6282)
Oleg Isonenbc8630f41304
diff --git a/.github/actions/vercel/action.yaml b/.github/actions/vercel/action.yamlindex 07f0be0b44..8a3de7fc04 100644--- a/.github/actions/vercel/action.yaml+++ b/.github/actions/vercel/action.yaml@@ -134,6 +134,13 @@ runs:shell: bash+ - name: Verify published CLI contract against staged Builder+ if: ${{ inputs.production-staged == 'true' }}+ run: pnpm --filter webstudio check:published-cli-contract+ shell: bash+ env:+ WEBSTUDIO_CLI_CONTRACT_BUILDER_ORIGIN: ${{ steps.deploy.outputs.domain }}+- name: Set Aliasid: aliasrun: |diff --git a/apps/builder/app/routes/rest.cli-compatibility.ts b/apps/builder/app/routes/rest.cli-compatibility.tsnew file mode 100644index 0000000000..48e7a0b15e--- /dev/null+++ b/apps/builder/app/routes/rest.cli-compatibility.ts@@ -0,0 +1,6 @@+import { json } from "@remix-run/server-runtime";+import { bundleVersion } from "@webstudio-is/protocol";+import { privateNoStoreResponseHeaders } from "~/services/cache-control.server";++export const loader = () =>+ json({ bundleVersion }, { headers: privateNoStoreResponseHeaders });diff --git a/apps/builder/app/services/api-compatibility.server.ts b/apps/builder/app/services/api-compatibility.server.tsindex a8b91ae12d..61659a3dac 100644--- a/apps/builder/app/services/api-compatibility.server.ts+++ b/apps/builder/app/services/api-compatibility.server.ts@@ -4,15 +4,26 @@ import {type ApiCompatibilityTarget,} from "@webstudio-is/trpc-interface/api-compatibility";-export const throwApiClientUpdateRequired = (- target: ApiCompatibilityTarget-): never => {+export const throwApiClientUpdateRequired = ({+ expectedVersion,+ receivedVersion,+ target,+}: {+ expectedVersion: string | number;+ receivedVersion: string | number | undefined;+ target: ApiCompatibilityTarget;+}): never => {+ const compatibility = createApiCompatibilityPayload({+ reason: "clientVersionUnsupported",+ target,+ });+ const message = `${compatibility.message} Expected bundle version ${expectedVersion}, received ${receivedVersion ?? "missing"}.`;throw new TRPCError({code: "PRECONDITION_FAILED",- message: "The API client must be updated before continuing.",- cause: createApiCompatibilityPayload({- reason: "clientVersionUnsupported",- target,- }),+ message,+ cause: {+ ...compatibility,+ message,+ },});};diff --git a/apps/builder/app/services/build-router.server.test.ts b/apps/builder/app/services/build-router.server.test.tsindex 4292f94c9b..9588bdd523 100644--- a/apps/builder/app/services/build-router.server.test.ts+++ b/apps/builder/app/services/build-router.server.test.ts@@ -27,11 +27,26 @@ describe("build router project bundle compatibility", () => {expect(getApiCompatibilityPayload(error)).toMatchObject({reason: "clientVersionUnsupported",target: "cli",+ message: `This version of the Webstudio CLI is incompatible with the current API. Expected bundle version ${bundleVersion}, received missing.`,action: { type: "updateCli" },});expect(() => assertCliBundleVersion(ctx, bundleVersion)).not.toThrow();});+ test("reports expected and received bundle contract versions", () => {+ const ctx = { apiClient: { type: "cli" } } as never;+ const message = `This version of the Webstudio CLI is incompatible with the current API. Expected bundle version ${bundleVersion}, received bundle-client.`;+ let error: unknown;+ try {+ assertCliBundleVersion(ctx, "bundle-client");+ } catch (caught) {+ error = caught;+ }++ expect(error).toMatchObject({ message });+ expect(getApiCompatibilityPayload(error)?.message).toBe(message);+ });+test("removes agent instructions from non-CLI bundles", async () => {const bundle = createPublishedProjectBundleFixture({build: {diff --git a/apps/builder/app/services/build-router.server.ts b/apps/builder/app/services/build-router.server.tsindex 429262c6e6..c687e6e78d 100644--- a/apps/builder/app/services/build-router.server.ts+++ b/apps/builder/app/services/build-router.server.ts@@ -67,7 +67,11 @@ const assertCliBundleVersion = (clientBundleVersion: string | number | undefined) => {if (ctx.apiClient?.type === "cli" && clientBundleVersion !== bundleVersion) {- throwApiClientUpdateRequired("cli");+ throwApiClientUpdateRequired({+ expectedVersion: bundleVersion,+ receivedVersion: clientBundleVersion,+ target: "cli",+ });}};diff --git a/apps/builder/app/services/cli-compatibility-route.node.test.ts b/apps/builder/app/services/cli-compatibility-route.node.test.tsnew file mode 100644index 0000000000..b92ecb4ef8--- /dev/null+++ b/apps/builder/app/services/cli-compatibility-route.node.test.ts@@ -0,0 +1,11 @@+import { expect, test } from "vitest";+import { bundleVersion } from "@webstudio-is/protocol";+import { loader } from "~/routes/rest.cli-compatibility";++test("reports the Builder runtime CLI contract without caching it", async () => {+ const response = loader();++ expect(response.status).toBe(200);+ expect(response.headers.get("Cache-Control")).toContain("no-store");+ await expect(response.json()).resolves.toEqual({ bundleVersion });+});diff --git a/apps/builder/e2e/tests/mdx-content-block.[shard-4].e2e.ts b/apps/builder/e2e/tests/mdx-content-block.[shard-4].e2e.tsindex dadb1601b8..1171b77cf6 100644--- a/apps/builder/e2e/tests/mdx-content-block.[shard-4].e2e.ts+++ b/apps/builder/e2e/tests/mdx-content-block.[shard-4].e2e.ts@@ -138,11 +138,9 @@ const insertTemplateIntoEmptyContentBlock = async ({await block.hover();const insert = page.getByRole("button", { name: "Insert block" }).last();await insert.waitFor({ state: "visible" });- await insert.focus();- await insert.press("Enter");- await page- .getByRole("menuitemradio", { name: templateName })- .click({ force: true });+ await insert.click();+ const menuItem = page.getByRole("menuitemradio", { name: templateName });+ await menuItem.press("Enter");};const openFixture = async ({diff --git a/packages/cli/package.json b/packages/cli/package.jsonindex 0c5db246cb..2c82b01f59 100644--- a/packages/cli/package.json+++ b/packages/cli/package.json@@ -30,6 +30,7 @@"evaluations": "tsx --import ../../scripts/register-react-global.ts --conditions=webstudio evaluations/high-impact/run-local-agent.ts","test": "vitest run","test:ssg-package": "tsx --conditions=webstudio scripts/test-ssg-package.ts",+ "check:published-cli-contract": "tsx --conditions=webstudio scripts/builder-contract-gate.ts","test:release-smoke": "pnpm build && tsx --conditions=webstudio scripts/release-smoke.ts","test:release-smoke:model": "WEBSTUDIO_RELEASE_SMOKE_MODEL_EVAL_REQUIRED=1 pnpm test:release-smoke","test:release-smoke:registry": "tsx --conditions=webstudio scripts/release-smoke.ts"diff --git a/packages/cli/scripts/builder-contract-gate.test.ts b/packages/cli/scripts/builder-contract-gate.test.tsnew file mode 100644index 0000000000..200f45ec6e--- /dev/null+++ b/packages/cli/scripts/builder-contract-gate.test.ts@@ -0,0 +1,71 @@+import { describe, expect, test, vi } from "vitest";+import {+ assertPublishedCliContract,+ loadBuilderBundleVersion,+} from "./builder-contract-gate";++describe("published CLI contract gate", () => {+ test("accepts the exact Builder bundle contract", () => {+ expect(() =>+ assertPublishedCliContract({+ cliVersion: "0.293.0",+ expectedVersion: "bundle-shared",+ receivedVersion: "bundle-shared",+ })+ ).not.toThrow();+ });++ test("reports the expected and received bundle contracts", () => {+ expect(() =>+ assertPublishedCliContract({+ cliVersion: "0.293.0",+ expectedVersion: "bundle-builder",+ receivedVersion: "bundle-cli",+ })+ ).toThrowError(+ "Published CLI contract mismatch for [email protected]. Expected bundle version bundle-builder, received bundle-cli. Publish a matching CLI before deploying Builder."+ );+ });++ test("rejects a CLI that does not send a bundle contract", () => {+ expect(() =>+ assertPublishedCliContract({+ cliVersion: "0.293.0",+ expectedVersion: "bundle-builder",+ receivedVersion: undefined,+ })+ ).toThrowError(+ "Published CLI contract mismatch for [email protected]. Expected bundle version bundle-builder, received missing. Publish a matching CLI before deploying Builder."+ );+ });++ test("reads the contract from the staged Builder runtime", async () => {+ const request = vi.fn(async () =>+ Response.json({ bundleVersion: "bundle-runtime" })+ );++ await expect(+ loadBuilderBundleVersion("https://candidate.example", {+ attempts: 1,+ request,+ })+ ).resolves.toBe("bundle-runtime");+ expect(request).toHaveBeenCalledWith(+ new URL("https://candidate.example/rest/cli-compatibility"),+ expect.objectContaining({ cache: "no-store" })+ );+ });++ test("rejects an invalid staged Builder contract response", async () => {+ const request = vi.fn(async () => Response.json({}));++ await expect(+ loadBuilderBundleVersion("https://candidate.example", {+ attempts: 1,+ request,+ })+ ).rejects.toThrowError(+ "Could not read the CLI contract from https://candidate.example/rest/cli-compatibility after 1 attempt: Builder CLI compatibility response has no bundle version."+ );+ });+});diff --git a/packages/cli/scripts/builder-contract-gate.ts b/packages/cli/scripts/builder-contract-gate.tsnew file mode 100644index 0000000000..3d6e14d070--- /dev/null+++ b/packages/cli/scripts/builder-contract-gate.ts@@ -0,0 +1,185 @@+import { execFile } from "node:child_process";+import { mkdir, mkdtemp, rm } from "node:fs/promises";+import { tmpdir } from "node:os";+import { join, resolve } from "node:path";+import { pathToFileURL } from "node:url";+import { promisify } from "node:util";+import { getBundleVersion } from "@webstudio-is/protocol";+import { createPublishedProjectBundleFixture } from "@webstudio-is/protocol/fixtures";+import { resolveRegistryTarget } from "./release-smoke-registry";+import { startRuntimeFixtureApi } from "./runtime-fixture-api";++const execFileAsync = promisify(execFile);++export const assertPublishedCliContract = ({+ cliVersion,+ expectedVersion,+ receivedVersion,+}: {+ cliVersion: string;+ expectedVersion: string | number;+ receivedVersion: string | number | undefined;+}) => {+ if (receivedVersion === expectedVersion) {+ return;+ }+ throw new Error(+ `Published CLI contract mismatch for webstudio@${cliVersion}. Expected bundle version ${expectedVersion}, received ${receivedVersion ?? "missing"}. Publish a matching CLI before deploying Builder.`+ );+};++const readNpmJson = async (args: string[]) =>+ JSON.parse((await execFileAsync("npm", args)).stdout) as unknown;++const contractEndpointPath = "/rest/cli-compatibility";+const defaultDelay = () =>+ new Promise<void>((resolveDelay) => setTimeout(resolveDelay, 3_000));++export const loadBuilderBundleVersion = async (+ origin: string,+ {+ attempts = 10,+ delay = defaultDelay,+ request = fetch,+ }: {+ attempts?: number;+ delay?: () => Promise<void>;+ request?: typeof fetch;+ } = {}+) => {+ const endpoint = new URL(contractEndpointPath, origin);+ let lastError: unknown;+ for (let attempt = 1; attempt <= attempts; attempt += 1) {+ try {+ const response = await request(endpoint, {+ cache: "no-store",+ headers: { Accept: "application/json" },+ signal: AbortSignal.timeout(10_000),+ });+ if (response.ok === false) {+ throw new Error(+ `Builder CLI compatibility endpoint returned HTTP ${response.status}.`+ );+ }+ const data = (await response.json()) as unknown;+ const receivedVersion = getBundleVersion(data);+ if (receivedVersion === undefined) {+ throw new Error(+ "Builder CLI compatibility response has no bundle version."+ );+ }+ return receivedVersion;+ } catch (error) {+ lastError = error;+ if (attempt < attempts) {+ await delay();+ }+ }+ }+ const message =+ lastError instanceof Error ? lastError.message : String(lastError);+ throw new Error(+ `Could not read the CLI contract from ${endpoint.href} after ${attempts} ${attempts === 1 ? "attempt" : "attempts"}: ${message}`,+ { cause: lastError }+ );+};++const checkPublishedCliContract = async ({+ builderOrigin,+}: {+ builderOrigin: string;+}) => {+ const expectedVersion = await loadBuilderBundleVersion(builderOrigin);+ const target = await resolveRegistryTarget("webstudio@latest", {+ attempts: 3,+ readNpmJson,+ });+ const directory = await mkdtemp(join(tmpdir(), "webstudio-contract-gate-"));+ let receivedVersion: string | number | undefined;+ let fixtureOrigin = "";+ try {+ const fixtureApi = await startRuntimeFixtureApi(+ async ({ operationPath, readInput }) => {+ if (operationPath !== "build.loadProjectBundleByBuildId") {+ throw new Error(`Unexpected CLI probe operation ${operationPath}.`);+ }+ receivedVersion = getBundleVersion(await readInput());+ return createPublishedProjectBundleFixture({ origin: fixtureOrigin });+ }+ );+ fixtureOrigin = fixtureApi.origin;+ try {+ await mkdir(join(directory, ".webstudio"), { recursive: true });+ let probeError: unknown;+ try {+ await execFileAsync(+ "npm",+ [+ "exec",+ "--yes",+ "--package",+ target.installSpec,+ "--",+ "webstudio",+ "sync",+ "--buildId",+ "contract-probe",+ "--origin",+ fixtureApi.origin,+ "--authToken",+ "contract-probe",+ ],+ {+ cwd: directory,+ maxBuffer: 20 * 1024 * 1024,+ timeout: 5 * 60_000,+ }+ );+ } catch (error) {+ probeError = error;+ }++ assertPublishedCliContract({+ cliVersion: target.version,+ expectedVersion,+ receivedVersion,+ });+ if (probeError !== undefined) {+ const stderr =+ typeof probeError === "object" &&+ probeError !== null &&+ "stderr" in probeError &&+ typeof probeError.stderr === "string"+ ? probeError.stderr.trim()+ : "";+ throw new Error(+ `Published CLI webstudio@${target.version} sent the expected bundle version ${expectedVersion} but could not complete the read-only sync probe: ${stderr || (probeError instanceof Error ? probeError.message : String(probeError))}`,+ { cause: probeError }+ );+ }+ console.info(+ `Published CLI webstudio@${target.version} matches Builder bundle version ${expectedVersion}.`+ );+ } finally {+ await fixtureApi.close();+ }+ } finally {+ await rm(directory, { force: true, recursive: true });+ }+};++const entrypoint = process.argv[1];+if (+ entrypoint !== undefined &&+ import.meta.url === pathToFileURL(resolve(entrypoint)).href+) {+ const builderOrigin = process.env.WEBSTUDIO_CLI_CONTRACT_BUILDER_ORIGIN;+ if (builderOrigin === undefined || builderOrigin === "") {+ throw new Error(+ "WEBSTUDIO_CLI_CONTRACT_BUILDER_ORIGIN must identify the staged Builder deployment."+ );+ }+ await checkPublishedCliContract({+ builderOrigin,+ });+}