Skip to content

Commit 18fb29d

Browse files
refactor(cli): restore upstream-style param destructure in indexDeployment
The discriminated `OnlineBootstrap | OfflineBootstrap` union forced indexDeployment's param to stay opaque (`result: BootstrapResult`) and required `result.mode === "offline"` checks at every branch. Collapse the union into a single shape with the API-mode fields optional, then restore upstream's destructured param signature: async function indexDeployment({ cliApiClient, projectRef, deploymentId, buildManifest, }: BootstrapResult) Offline mode is detected by `!cliApiClient` instead of a `mode` tag. Bootstrap returns `{ buildManifest }` for offline and the full record for online. The three offline branches (env vars / metadata write / error write) all use `!cliApiClient` (or pair it with `!deploymentId` where the type checker needs both narrowed). Net: same behavior, but `indexDeployment`'s shape now matches upstream. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent a5eacc7 commit 18fb29d

1 file changed

Lines changed: 28 additions & 33 deletions

File tree

packages/cli-v3/src/entryPoints/managed-index-controller.ts

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,16 @@ async function loadBuildManifest() {
4343
return BuildManifest.parse(raw);
4444
}
4545

46-
type OnlineBootstrap = {
47-
mode: "online";
46+
// In offline mode (TRIGGER_INDEX_OFFLINE=1) the bootstrap skips API client
47+
// construction entirely. Downstream code treats `cliApiClient === undefined`
48+
// as the signal that we're running offline.
49+
type BootstrapResult = {
4850
buildManifest: BuildManifest;
49-
cliApiClient: CliApiClient;
50-
projectRef: string;
51-
deploymentId: string;
51+
cliApiClient?: CliApiClient;
52+
projectRef?: string;
53+
deploymentId?: string;
5254
};
5355

54-
type OfflineBootstrap = {
55-
mode: "offline";
56-
buildManifest: BuildManifest;
57-
};
58-
59-
type BootstrapResult = OnlineBootstrap | OfflineBootstrap;
60-
6156
/**
6257
* Returns the same shape as `cliApiClient.getEnvironmentVariables` for the
6358
* offline path. We never have project env vars at index time in offline mode
@@ -71,13 +66,10 @@ const offlineEnvShim = () =>
7166
async function bootstrap(): Promise<BootstrapResult> {
7267
const buildManifest = await loadBuildManifest();
7368

74-
// Offline mode: API access is unavailable; the host CLI will read the
75-
// index artifacts after the build and register them via --register-only.
69+
// Offline mode: API access is unavailable. The artifacts produced by
70+
// indexDeployment are baked into the final image by the Containerfile.
7671
if (env.TRIGGER_INDEX_OFFLINE === "1") {
77-
return {
78-
mode: "offline",
79-
buildManifest,
80-
};
72+
return { buildManifest };
8173
}
8274

8375
// Online mode (default): use the API for env vars and registration.
@@ -103,24 +95,27 @@ async function bootstrap(): Promise<BootstrapResult> {
10395
}
10496

10597
return {
106-
mode: "online",
10798
buildManifest,
10899
cliApiClient,
109100
projectRef: env.TRIGGER_PROJECT_REF,
110101
deploymentId: env.TRIGGER_DEPLOYMENT_ID,
111102
};
112103
}
113104

114-
async function indexDeployment(result: BootstrapResult) {
115-
const { buildManifest } = result;
105+
async function indexDeployment({
106+
cliApiClient,
107+
projectRef,
108+
deploymentId,
109+
buildManifest,
110+
}: BootstrapResult) {
116111
const stdout: string[] = [];
117112
const stderr: string[] = [];
118113

119114
try {
120115
const $env =
121-
result.mode === "offline"
122-
? offlineEnvShim()
123-
: await result.cliApiClient.getEnvironmentVariables(result.projectRef);
116+
cliApiClient && projectRef
117+
? await cliApiClient.getEnvironmentVariables(projectRef)
118+
: offlineEnvShim();
124119

125120
if (!$env.success) {
126121
throw new Error(`Failed to fetch environment variables: ${$env.error}`);
@@ -154,9 +149,9 @@ async function indexDeployment(result: BootstrapResult) {
154149
const buildPlatform = process.env.BUILDPLATFORM;
155150
const targetPlatform = process.env.TARGETPLATFORM;
156151

157-
if (result.mode === "offline") {
158-
// Two-phase deploy: write metadata to disk for the host CLI to register
159-
// after the build via `trigger deploy --register-only`.
152+
if (!cliApiClient || !deploymentId) {
153+
// Offline mode: write metadata to disk; the multi-stage Containerfile
154+
// copies it into the final image where downstream tooling reads it.
160155
const indexMetadata = {
161156
contentHash: buildManifest.contentHash,
162157
packageVersion: buildManifest.packageVersion,
@@ -207,8 +202,8 @@ async function indexDeployment(result: BootstrapResult) {
207202
targetPlatform,
208203
};
209204

210-
const createResponse = await result.cliApiClient.createDeploymentBackgroundWorker(
211-
result.deploymentId,
205+
const createResponse = await cliApiClient.createDeploymentBackgroundWorker(
206+
deploymentId,
212207
backgroundWorkerBody
213208
);
214209

@@ -238,14 +233,14 @@ async function indexDeployment(result: BootstrapResult) {
238233

239234
console.error("Failed to index deployment", serialiedIndexError);
240235

241-
if (result.mode === "offline") {
242-
// Write error to a file so the host CLI can retrieve it after the build.
236+
if (cliApiClient && deploymentId) {
237+
await cliApiClient.failDeployment(deploymentId, { error: serialiedIndexError });
238+
} else {
239+
// Offline mode: write error to disk so downstream tooling can surface it.
243240
await writeFile(
244241
join(process.cwd(), "index-error.json"),
245242
JSON.stringify({ error: serialiedIndexError }, null, 2)
246243
);
247-
} else {
248-
await result.cliApiClient.failDeployment(result.deploymentId, { error: serialiedIndexError });
249244
}
250245

251246
process.exit(1);

0 commit comments

Comments
 (0)