@limrun/api 0.31.2 → 0.31.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/rbe-bep.mjs CHANGED
@@ -8,21 +8,55 @@
8
8
  * downloads the bytes (`--remote_download_outputs`), because the URI names the
9
9
  * output's CAS identity, not its local availability. `lim xcode rbe install`
10
10
  * reads that digest and hands it to the instance, which fetches the blob from
11
- * its own RBE cache and installs it — no round-trip. The printed build command
12
- * passes `--remote_download_outputs=minimal` to skip the (unneeded) local
13
- * download by default; dropping it does not affect this parser.
11
+ * its own RBE cache and installs it — no round-trip. The generated bazelrc sets
12
+ * `--remote_download_outputs=minimal` to skip the (unneeded) local download by
13
+ * default; overriding it (e.g. `--remote_download_outputs=toplevel` on the command
14
+ * line) to materialize the .ipa locally does not affect this parser.
15
+ *
16
+ * A fully-cached rebuild (0 actions executed) still emits the bytestream URI and
17
+ * reproduces the same digest, so the auto-install watcher can rely on this parser
18
+ * after a no-op or revert (e.g. `git stash`) rebuild. Verified empirically.
14
19
  */
20
+ export class RbeBepError extends Error {
21
+ constructor(kind, message) {
22
+ super(message);
23
+ this.name = 'RbeBepError';
24
+ this.kind = kind;
25
+ }
26
+ /**
27
+ * True when re-reading the same (completed) BEP cannot help — the build was
28
+ * produced with flags the instance cache can't use (local download or a
29
+ * non-SHA256 digest). The watcher logs these once instead of retrying.
30
+ */
31
+ get terminal() {
32
+ return this.kind === 'local-only' || this.kind === 'non-sha256';
33
+ }
34
+ }
15
35
  /** A SHA-256 digest is exactly 64 lowercase hex chars; the instance CAS is keyed by it. */
16
36
  const SHA256_HEX = /^[0-9a-f]{64}$/;
17
- /** Extracts (hash, size) from a `bytestream://…/blobs/<hash>/<size>` URI. */
37
+ /**
38
+ * Extracts (digestFunction, hash, size) from a Bazel/REAPI bytestream blob URI:
39
+ * `bytestream://…/blobs/[<digest_function>/]<hash>/<size>`. The digest-function
40
+ * segment is OMITTED for functions inferable from hash length (SHA-256 among
41
+ * them) and PRESENT otherwise — notably BLAKE3, whose 256-bit output is also 64
42
+ * hex chars and so cannot be told apart from SHA-256 by length. Capturing the
43
+ * function lets the caller reject a non-SHA256 build (e.g. Bazel 9's BLAKE3
44
+ * default) with the right guidance instead of misreading the URI.
45
+ */
18
46
  function parseBytestreamDigest(uri) {
19
- // Tolerate an optional instance-name segment before /blobs/ by anchoring on
20
- // the /blobs/<hash>/<size> tail rather than a fixed segment count. The hash is
21
- // left length-agnostic here (validated as SHA-256 by the caller).
22
- const m = uri.match(/\/blobs\/([0-9a-f]+)\/(\d+)(?:$|\/)/);
23
- if (!m || m[1] === undefined || m[2] === undefined)
47
+ // Anchor on the /blobs/[func/]<hash>/<size> tail (tolerating an instance-name
48
+ // prefix). The optional function group only matches a real function segment:
49
+ // for the SHA-256 form (`/blobs/<hash>/<size>`) it backtracks to absent, and
50
+ // for `/blobs/blake3/<hash>/<size>` the non-hex 'l'/'k' force it to be captured
51
+ // as the function rather than the hash.
52
+ const m = uri.match(/\/blobs\/(?:([a-z0-9_]+)\/)?([0-9a-fA-F]+)\/(\d+)(?:$|\/)/);
53
+ if (!m || m[2] === undefined || m[3] === undefined)
24
54
  return null;
25
- return { hash: m[1], sizeBytes: Number(m[2]) };
55
+ return {
56
+ ...(m[1] !== undefined ? { digestFunction: m[1] } : {}),
57
+ hash: m[2].toLowerCase(),
58
+ sizeBytes: Number(m[3]),
59
+ };
26
60
  }
27
61
  /**
28
62
  * Canonicalizes a Bazel target label to the `//pkg:name` form BEP records.
@@ -41,14 +75,8 @@ function canonicalizeLabel(label) {
41
75
  const name = pkg.split('/').pop() ?? '';
42
76
  return name ? `${pkg}:${name}` : pkg;
43
77
  }
44
- /**
45
- * Finds the `.ipa` produced for `label` in a BEP stream and returns its CAS
46
- * digest. Throws a descriptive Error when the target/.ipa is absent, its output
47
- * was materialized locally (a `file://` URI) instead of left in CAS, or it was
48
- * built with a non-SHA256 digest the instance cache cannot resolve.
49
- */
50
- export function parseTopLevelIpaDigest(bepJson, label) {
51
- const canonical = canonicalizeLabel(label);
78
+ /** Parses the BEP stream into events, ignoring partial/garbled lines. */
79
+ function parseEvents(bepJson) {
52
80
  const events = [];
53
81
  for (const line of bepJson.split('\n')) {
54
82
  const trimmed = line.trim();
@@ -61,28 +89,64 @@ export function parseTopLevelIpaDigest(bepJson, label) {
61
89
  // Ignore partial/garbled lines (BEP is flushed incrementally).
62
90
  }
63
91
  }
64
- // The successful TargetComplete for the requested label (matched against the
65
- // canonical //pkg:name form Bazel records, so `//App` matches `//App:App`).
66
- const completed = events.find((e) => e.id?.targetCompleted?.label === canonical && e.completed?.success)
67
- ?.completed;
68
- if (!completed) {
69
- throw new Error(`No successful build of ${label} found in the build event log. ` +
70
- `Build it first with --config=limrun (e.g. \`bazelisk --digest_function=sha256 build --config=limrun ${label}\`).`);
92
+ return events;
93
+ }
94
+ /**
95
+ * Inspects a BEP stream for build completion. The watcher reads the BEP on every
96
+ * change but only acts once `complete` is true (the stream is fully flushed) and
97
+ * `success` is true (the build did not fail). `invocationId` lets the watcher act
98
+ * once per build even when consecutive builds reproduce the same .ipa digest
99
+ * (e.g. a `git stash` revert that rebuilds to an earlier digest).
100
+ */
101
+ export function inspectBuildCompletion(bepJson) {
102
+ let complete = false;
103
+ let sawFinished = false;
104
+ let success = false;
105
+ let invocationId;
106
+ for (const e of parseEvents(bepJson)) {
107
+ if (e.started?.uuid)
108
+ invocationId = e.started.uuid;
109
+ if (e.id?.buildFinished !== undefined || e.finished !== undefined) {
110
+ sawFinished = true;
111
+ // proto3 JSON omits zero-valued scalars, so a SUCCESS exit code serializes
112
+ // as `{name:"SUCCESS"}` with `code` omitted — checking `code === 0` would be
113
+ // a dead branch. Trust overallSuccess (present=true on success, omitted on
114
+ // failure) and fall back to the exit code's symbolic name.
115
+ success = e.finished?.overallSuccess === true || e.finished?.exitCode?.name === 'SUCCESS';
116
+ }
117
+ if (e.lastMessage === true)
118
+ complete = true;
119
+ }
120
+ return {
121
+ complete,
122
+ success: sawFinished && success,
123
+ ...(invocationId !== undefined ? { invocationId } : {}),
124
+ };
125
+ }
126
+ /** Indexes namedSetOfFiles events by id for transitive fileset resolution. */
127
+ function indexNamedSets(events) {
128
+ const namedSets = new Map();
129
+ for (const e of events) {
130
+ const id = e.id?.namedSet?.id;
131
+ if (id && e.namedSetOfFiles)
132
+ namedSets.set(id, e.namedSetOfFiles);
71
133
  }
134
+ return namedSets;
135
+ }
136
+ /**
137
+ * Resolves the top-level `.ipa` CAS digest for one successful target's
138
+ * `completed` event, walking its default output group's filesets transitively.
139
+ * Throws a typed RbeBepError when no .ipa is present, it was materialized
140
+ * locally (`file://`), or it carries a non-SHA256 (e.g. BLAKE3) digest.
141
+ */
142
+ function resolveIpaDigest(label, completed, namedSets) {
72
143
  // Default output group -> the fileSet (namedSetOfFiles) ids holding its outputs.
73
144
  const rootFileSetIds = (completed.outputGroup ?? [])
74
145
  .filter((og) => og.name === 'default' || !og.name)
75
146
  .flatMap((og) => (og.fileSets ?? []).map((fs) => fs.id))
76
147
  .filter((id) => !!id);
77
148
  if (rootFileSetIds.length === 0) {
78
- throw new Error(`Build of ${label} reported no default output files.`);
79
- }
80
- // Index namedSetOfFiles by id and walk transitively (sets can nest).
81
- const namedSets = new Map();
82
- for (const e of events) {
83
- const id = e.id?.namedSet?.id;
84
- if (id && e.namedSetOfFiles)
85
- namedSets.set(id, e.namedSetOfFiles);
149
+ throw new RbeBepError('no-output', `Build of ${label} reported no default output files.`);
86
150
  }
87
151
  const seen = new Set();
88
152
  const queue = [...rootFileSetIds];
@@ -102,14 +166,20 @@ export function parseTopLevelIpaDigest(bepJson, label) {
102
166
  continue;
103
167
  const digest = f.uri ? parseBytestreamDigest(f.uri) : null;
104
168
  if (digest) {
105
- // The instance cache is keyed by SHA-256. A non-64-hex digest means the
106
- // build used a different digest function (Bazel 9 defaults to BLAKE3),
107
- // which the server CAS cannot resolve — surface that clearly here rather
108
- // than letting it fail later as a cryptic cache miss.
109
- if (!SHA256_HEX.test(digest.hash)) {
110
- throw new Error(`Built ${f.name} with a non-SHA256 digest (got ${digest.hash.length} hex chars, expected 64; ` +
111
- `likely BLAKE3 on Bazel 9). The instance cache is keyed by SHA-256 — rebuild with ` +
112
- `--digest_function=sha256, e.g. \`bazelisk --digest_function=sha256 build --config=limrun ${label}\`.`);
169
+ // The instance cache is keyed by SHA-256. Reject any other digest
170
+ // function — Bazel 9 defaults to BLAKE3, whose URI carries an explicit
171
+ // `blake3` function segment (and whose 64-hex hash is indistinguishable
172
+ // from SHA-256 by length, so the hash form alone can't catch it). Surface
173
+ // it here with the fix rather than letting it fail later as a cryptic
174
+ // server cache miss.
175
+ const isSha256 = digest.digestFunction === undefined ?
176
+ SHA256_HEX.test(digest.hash)
177
+ : digest.digestFunction === 'sha256';
178
+ if (!isSha256) {
179
+ const got = digest.digestFunction ?? `${digest.hash.length} hex chars`;
180
+ throw new RbeBepError('non-sha256', `Built ${f.name} with a non-SHA256 digest (${got}; Bazel 9 defaults to BLAKE3). The instance ` +
181
+ `cache is keyed by SHA-256 — rebuild with --digest_function=sha256, e.g. ` +
182
+ `\`bazelisk --digest_function=sha256 build --config=limrun ${label}\`.`);
113
183
  }
114
184
  return { hash: digest.hash, sizeBytes: digest.sizeBytes, ipaName: f.name };
115
185
  }
@@ -121,10 +191,29 @@ export function parseTopLevelIpaDigest(bepJson, label) {
121
191
  }
122
192
  }
123
193
  if (localOnlyIpa) {
124
- throw new Error(`Found ${label}'s .ipa (${localOnlyIpa}) but it has no remote (bytestream) digest — it was ` +
194
+ throw new RbeBepError('local-only', `Found ${label}'s .ipa (${localOnlyIpa}) but it has no remote (bytestream) digest — it was ` +
125
195
  `built locally, not remotely executed. Ensure you built with --config=limrun so the .ipa is ` +
126
196
  `produced in the instance's cache.`);
127
197
  }
128
- throw new Error(`No .ipa output found for ${label} in the build event log.`);
198
+ throw new RbeBepError('no-ipa', `No .ipa output found for ${label} in the build event log.`);
199
+ }
200
+ /**
201
+ * Finds the `.ipa` produced for `label` in a BEP stream and returns its CAS
202
+ * digest. Throws a typed RbeBepError when the target/.ipa is absent, its output
203
+ * was materialized locally (a `file://` URI) instead of left in CAS, or it was
204
+ * built with a non-SHA256 digest the instance cache cannot resolve.
205
+ */
206
+ export function parseTopLevelIpaDigest(bepJson, label) {
207
+ const canonical = canonicalizeLabel(label);
208
+ const events = parseEvents(bepJson);
209
+ // The successful TargetComplete for the requested label (matched against the
210
+ // canonical //pkg:name form Bazel records, so `//App` matches `//App:App`).
211
+ const completed = events.find((e) => e.id?.targetCompleted?.label === canonical && e.completed?.success)
212
+ ?.completed;
213
+ if (!completed) {
214
+ throw new RbeBepError('no-build', `No successful build of ${label} found in the build event log. ` +
215
+ `Build it first with --config=limrun (e.g. \`bazelisk --digest_function=sha256 build --config=limrun ${label}\`).`);
216
+ }
217
+ return resolveIpaDigest(label, completed, indexNamedSets(events));
129
218
  }
130
219
  //# sourceMappingURL=rbe-bep.mjs.map
package/rbe-bep.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"rbe-bep.mjs","sourceRoot":"","sources":["src/rbe-bep.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AA0BH,2FAA2F;AAC3F,MAAM,UAAU,GAAG,gBAAgB,CAAC;AAEpC,6EAA6E;AAC7E,SAAS,qBAAqB,CAAC,GAAW;IACxC,4EAA4E;IAC5E,+EAA+E;IAC/E,kEAAkE;IAClE,MAAM,CAAC,GAAG,GAAG,CAAC,KAAK,CAAC,qCAAqC,CAAC,CAAC;IAC3D,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,SAAS,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,SAAS;QAAE,OAAO,IAAI,CAAC;IAChE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;AACjD,CAAC;AAED;;;;GAIG;AACH,SAAS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AC,QAAQ,IAAI,EAAE,EAAE,CAAC;YACxC,IAAI,MAAM,CAAC,EAAE;gBAAE,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;QACvC,CAAC;IACH,CAAC;IAED,IAAI,YAAY,EAAE,CAAC;QACjB,MAAM,IAAI,WAAW,CACnB,YAAY,EACZ,SAAS,KAAK,YAAY,YAAY,sDAAsD;YAC1F,6FAA6F;YAC7F,mCAAmC,CACtC,CAAC;IACJ,CAAC;IACD,MAAM,IAAI,WAAW,CAAC,QAAQ,EAAE,4BAA4B,KAAK,0BAA0B,CAAC,CAAC;AAC/F,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,sBAAsB,CAAC,OAAe,EAAE,KAAa;IACnE,MAAM,SAAS,GAAG,iBAAiB,CAAC,KAAK,CAAC,CAAC;IAC3C,MAAM,MAAM,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC;IAEpC,6EAA6E;IAC7E,4EAA4E;IAC5E,MAAM,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,eAAe,EAAE,KAAK,KAAK,SAAS,IAAI,CAAC,CAAC,SAAS,EAAE,OAAO,CAAC;QACtG,EAAE,SAAS,CAAC;IACd,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,MAAM,IAAI,WAAW,CACnB,UAAU,EACV,0BAA0B,KAAK,iCAAiC;YAC9D,uGAAuG,KAAK,MAAM,CACrH,CAAC;IACJ,CAAC;IAED,OAAO,gBAAgB,CAAC,KAAK,EAAE,SAAS,EAAE,cAAc,CAAC,MAAM,CAAC,CAAC,CAAC;AACpE,CAAC"}
@@ -178,9 +178,9 @@ export type XcodeClient = {
178
178
  xcodebuild: (settings?: XcodeProjectConfig, options?: XcodeBuildOptions) => ExecChildProcess;
179
179
  /**
180
180
  * Attach a simulator to this xcode instance.
181
- * After attaching, the latest xcodebuild build (if any) is installed on the
182
- * simulator. RBE builds are not auto-installed on attach — install them
183
- * explicitly with `installRbeBuildFromBep` while a simulator is attached.
181
+ * After attaching, the latest installable build is installed on the simulator:
182
+ * the latest xcodebuild build or the latest RBE build (whichever is newer),
183
+ * unless that build's exact digest is already on the simulator.
184
184
  */
185
185
  attachSimulator: (simulator: IosInstance | {
186
186
  apiUrl: string;
@@ -178,9 +178,9 @@ export type XcodeClient = {
178
178
  xcodebuild: (settings?: XcodeProjectConfig, options?: XcodeBuildOptions) => ExecChildProcess;
179
179
  /**
180
180
  * Attach a simulator to this xcode instance.
181
- * After attaching, the latest xcodebuild build (if any) is installed on the
182
- * simulator. RBE builds are not auto-installed on attach — install them
183
- * explicitly with `installRbeBuildFromBep` while a simulator is attached.
181
+ * After attaching, the latest installable build is installed on the simulator:
182
+ * the latest xcodebuild build or the latest RBE build (whichever is newer),
183
+ * unless that build's exact digest is already on the simulator.
184
184
  */
185
185
  attachSimulator: (simulator: IosInstance | {
186
186
  apiUrl: string;
package/src/client.ts CHANGED
@@ -739,11 +739,19 @@ export class Limrun {
739
739
  return () => controller.abort();
740
740
  }
741
741
 
742
- private buildBody({ options: { body, headers: rawHeaders } }: { options: FinalRequestOptions }): {
742
+ private buildBody({ options }: { options: FinalRequestOptions }): {
743
743
  bodyHeaders: HeadersLike;
744
744
  body: BodyInit | undefined;
745
745
  } {
746
+ const { body, headers: rawHeaders } = options;
746
747
  if (!body) {
748
+ // A resource method always passes a `body` key when its operation defines a
749
+ // request body, even if the caller omitted an optional body param. Keep the
750
+ // content-type for those, and only elide it for operations with no body at
751
+ // all (e.g. GET/DELETE).
752
+ if (body == null && 'body' in options) {
753
+ return this.#encoder({ body, headers: buildHeaders([rawHeaders]) });
754
+ }
747
755
  return { bodyHeaders: undefined, body: undefined };
748
756
  }
749
757
  const headers = buildHeaders([rawHeaders]);
package/src/index.ts CHANGED
@@ -10,7 +10,7 @@ export * from './instance-client';
10
10
  export * as Ios from './ios-client';
11
11
  export { startHttpProxy, type HttpProxy, type StartHttpProxyOptions } from './http-proxy';
12
12
  export { buildSettingKeyPattern, parseBuildSettingEntries, validateBuildSettings } from './build-settings';
13
- export { parseTopLevelIpaDigest, type BepIpaDigest } from './rbe-bep';
13
+ export { parseTopLevelIpaDigest, inspectBuildCompletion, RbeBepError, type BepIpaDigest } from './rbe-bep';
14
14
  export {
15
15
  exec,
16
16
  type ExecRequest,
package/src/ios-shim.ts CHANGED
@@ -13,7 +13,6 @@ const FORWARDED_SIMCTL_COMMANDS = new Set([
13
13
  'privacy',
14
14
  'location',
15
15
  'status_bar',
16
- 'spawn',
17
16
  ]);
18
17
 
19
18
  export type IosXcrunShimServer = {
package/src/rbe-bep.ts CHANGED
@@ -8,9 +8,14 @@
8
8
  * downloads the bytes (`--remote_download_outputs`), because the URI names the
9
9
  * output's CAS identity, not its local availability. `lim xcode rbe install`
10
10
  * reads that digest and hands it to the instance, which fetches the blob from
11
- * its own RBE cache and installs it — no round-trip. The printed build command
12
- * passes `--remote_download_outputs=minimal` to skip the (unneeded) local
13
- * download by default; dropping it does not affect this parser.
11
+ * its own RBE cache and installs it — no round-trip. The generated bazelrc sets
12
+ * `--remote_download_outputs=minimal` to skip the (unneeded) local download by
13
+ * default; overriding it (e.g. `--remote_download_outputs=toplevel` on the command
14
+ * line) to materialize the .ipa locally does not affect this parser.
15
+ *
16
+ * A fully-cached rebuild (0 actions executed) still emits the bytestream URI and
17
+ * reproduces the same digest, so the auto-install watcher can rely on this parser
18
+ * after a no-op or revert (e.g. `git stash`) rebuild. Verified empirically.
14
19
  */
15
20
 
16
21
  export type BepIpaDigest = {
@@ -26,7 +31,12 @@ type BepEvent = {
26
31
  id?: {
27
32
  targetCompleted?: { label?: string };
28
33
  namedSet?: { id?: string };
34
+ buildFinished?: unknown;
29
35
  };
36
+ started?: { uuid?: string };
37
+ finished?: { overallSuccess?: boolean; exitCode?: { name?: string; code?: number } };
38
+ /** Bazel sets this on the final BEP event once the stream is fully flushed. */
39
+ lastMessage?: boolean;
30
40
  completed?: {
31
41
  success?: boolean;
32
42
  outputGroup?: Array<{ name?: string; fileSets?: Array<{ id?: string }> }>;
@@ -37,17 +47,62 @@ type BepEvent = {
37
47
  };
38
48
  };
39
49
 
50
+ /**
51
+ * Why a BEP could not yield an .ipa digest for a label.
52
+ *
53
+ * `terminal` failures will not resolve by re-reading the same completed BEP
54
+ * (the build used the wrong flags); `no-build` / `no-output` / `no-ipa` are
55
+ * transient while the build is still flushing and become terminal only once
56
+ * the build is confirmed complete. The watcher uses this to decide whether to
57
+ * retry the read or to log-once and give up.
58
+ */
59
+ export type RbeBepErrorKind = 'no-build' | 'no-output' | 'no-ipa' | 'local-only' | 'non-sha256';
60
+
61
+ export class RbeBepError extends Error {
62
+ readonly kind: RbeBepErrorKind;
63
+ constructor(kind: RbeBepErrorKind, message: string) {
64
+ super(message);
65
+ this.name = 'RbeBepError';
66
+ this.kind = kind;
67
+ }
68
+
69
+ /**
70
+ * True when re-reading the same (completed) BEP cannot help — the build was
71
+ * produced with flags the instance cache can't use (local download or a
72
+ * non-SHA256 digest). The watcher logs these once instead of retrying.
73
+ */
74
+ get terminal(): boolean {
75
+ return this.kind === 'local-only' || this.kind === 'non-sha256';
76
+ }
77
+ }
78
+
40
79
  /** A SHA-256 digest is exactly 64 lowercase hex chars; the instance CAS is keyed by it. */
41
80
  const SHA256_HEX = /^[0-9a-f]{64}$/;
42
81
 
43
- /** Extracts (hash, size) from a `bytestream://…/blobs/<hash>/<size>` URI. */
44
- function parseBytestreamDigest(uri: string): { hash: string; sizeBytes: number } | null {
45
- // Tolerate an optional instance-name segment before /blobs/ by anchoring on
46
- // the /blobs/<hash>/<size> tail rather than a fixed segment count. The hash is
47
- // left length-agnostic here (validated as SHA-256 by the caller).
48
- const m = uri.match(/\/blobs\/([0-9a-f]+)\/(\d+)(?:$|\/)/);
49
- if (!m || m[1] === undefined || m[2] === undefined) return null;
50
- return { hash: m[1], sizeBytes: Number(m[2]) };
82
+ /**
83
+ * Extracts (digestFunction, hash, size) from a Bazel/REAPI bytestream blob URI:
84
+ * `bytestream://…/blobs/[<digest_function>/]<hash>/<size>`. The digest-function
85
+ * segment is OMITTED for functions inferable from hash length (SHA-256 among
86
+ * them) and PRESENT otherwise — notably BLAKE3, whose 256-bit output is also 64
87
+ * hex chars and so cannot be told apart from SHA-256 by length. Capturing the
88
+ * function lets the caller reject a non-SHA256 build (e.g. Bazel 9's BLAKE3
89
+ * default) with the right guidance instead of misreading the URI.
90
+ */
91
+ function parseBytestreamDigest(
92
+ uri: string,
93
+ ): { digestFunction?: string; hash: string; sizeBytes: number } | null {
94
+ // Anchor on the /blobs/[func/]<hash>/<size> tail (tolerating an instance-name
95
+ // prefix). The optional function group only matches a real function segment:
96
+ // for the SHA-256 form (`/blobs/<hash>/<size>`) it backtracks to absent, and
97
+ // for `/blobs/blake3/<hash>/<size>` the non-hex 'l'/'k' force it to be captured
98
+ // as the function rather than the hash.
99
+ const m = uri.match(/\/blobs\/(?:([a-z0-9_]+)\/)?([0-9a-fA-F]+)\/(\d+)(?:$|\/)/);
100
+ if (!m || m[2] === undefined || m[3] === undefined) return null;
101
+ return {
102
+ ...(m[1] !== undefined ? { digestFunction: m[1] } : {}),
103
+ hash: m[2].toLowerCase(),
104
+ sizeBytes: Number(m[3]),
105
+ };
51
106
  }
52
107
 
53
108
  /**
@@ -66,14 +121,8 @@ function canonicalizeLabel(label: string): string {
66
121
  return name ? `${pkg}:${name}` : pkg;
67
122
  }
68
123
 
69
- /**
70
- * Finds the `.ipa` produced for `label` in a BEP stream and returns its CAS
71
- * digest. Throws a descriptive Error when the target/.ipa is absent, its output
72
- * was materialized locally (a `file://` URI) instead of left in CAS, or it was
73
- * built with a non-SHA256 digest the instance cache cannot resolve.
74
- */
75
- export function parseTopLevelIpaDigest(bepJson: string, label: string): BepIpaDigest {
76
- const canonical = canonicalizeLabel(label);
124
+ /** Parses the BEP stream into events, ignoring partial/garbled lines. */
125
+ function parseEvents(bepJson: string): BepEvent[] {
77
126
  const events: BepEvent[] = [];
78
127
  for (const line of bepJson.split('\n')) {
79
128
  const trimmed = line.trim();
@@ -84,32 +133,83 @@ export function parseTopLevelIpaDigest(bepJson: string, label: string): BepIpaDi
84
133
  // Ignore partial/garbled lines (BEP is flushed incrementally).
85
134
  }
86
135
  }
136
+ return events;
137
+ }
87
138
 
88
- // The successful TargetComplete for the requested label (matched against the
89
- // canonical //pkg:name form Bazel records, so `//App` matches `//App:App`).
90
- const completed = events.find((e) => e.id?.targetCompleted?.label === canonical && e.completed?.success)
91
- ?.completed;
92
- if (!completed) {
93
- throw new Error(
94
- `No successful build of ${label} found in the build event log. ` +
95
- `Build it first with --config=limrun (e.g. \`bazelisk --digest_function=sha256 build --config=limrun ${label}\`).`,
96
- );
139
+ export type BepCompletion = {
140
+ /**
141
+ * True once Bazel has flushed the terminal event (`lastMessage`), so every
142
+ * earlier event — including the .ipa's namedSetOfFiles — is durably on disk.
143
+ * Gate the digest read on this, NOT on the presence of `buildFinished`:
144
+ * `buildFinished` is followed by `buildToolLogs` and `buildMetrics`
145
+ * (the `lastMessage` carrier), so it is not the last line.
146
+ */
147
+ complete: boolean;
148
+ /** Whether the build itself succeeded (overallSuccess / exit code 0). */
149
+ success: boolean;
150
+ /** Bazel's invocation UUID — distinguishes one build from the next. */
151
+ invocationId?: string;
152
+ };
153
+
154
+ /**
155
+ * Inspects a BEP stream for build completion. The watcher reads the BEP on every
156
+ * change but only acts once `complete` is true (the stream is fully flushed) and
157
+ * `success` is true (the build did not fail). `invocationId` lets the watcher act
158
+ * once per build even when consecutive builds reproduce the same .ipa digest
159
+ * (e.g. a `git stash` revert that rebuilds to an earlier digest).
160
+ */
161
+ export function inspectBuildCompletion(bepJson: string): BepCompletion {
162
+ let complete = false;
163
+ let sawFinished = false;
164
+ let success = false;
165
+ let invocationId: string | undefined;
166
+ for (const e of parseEvents(bepJson)) {
167
+ if (e.started?.uuid) invocationId = e.started.uuid;
168
+ if (e.id?.buildFinished !== undefined || e.finished !== undefined) {
169
+ sawFinished = true;
170
+ // proto3 JSON omits zero-valued scalars, so a SUCCESS exit code serializes
171
+ // as `{name:"SUCCESS"}` with `code` omitted — checking `code === 0` would be
172
+ // a dead branch. Trust overallSuccess (present=true on success, omitted on
173
+ // failure) and fall back to the exit code's symbolic name.
174
+ success = e.finished?.overallSuccess === true || e.finished?.exitCode?.name === 'SUCCESS';
175
+ }
176
+ if (e.lastMessage === true) complete = true;
97
177
  }
178
+ return {
179
+ complete,
180
+ success: sawFinished && success,
181
+ ...(invocationId !== undefined ? { invocationId } : {}),
182
+ };
183
+ }
98
184
 
185
+ /** Indexes namedSetOfFiles events by id for transitive fileset resolution. */
186
+ function indexNamedSets(events: BepEvent[]): Map<string, NonNullable<BepEvent['namedSetOfFiles']>> {
187
+ const namedSets = new Map<string, NonNullable<BepEvent['namedSetOfFiles']>>();
188
+ for (const e of events) {
189
+ const id = e.id?.namedSet?.id;
190
+ if (id && e.namedSetOfFiles) namedSets.set(id, e.namedSetOfFiles);
191
+ }
192
+ return namedSets;
193
+ }
194
+
195
+ /**
196
+ * Resolves the top-level `.ipa` CAS digest for one successful target's
197
+ * `completed` event, walking its default output group's filesets transitively.
198
+ * Throws a typed RbeBepError when no .ipa is present, it was materialized
199
+ * locally (`file://`), or it carries a non-SHA256 (e.g. BLAKE3) digest.
200
+ */
201
+ function resolveIpaDigest(
202
+ label: string,
203
+ completed: NonNullable<BepEvent['completed']>,
204
+ namedSets: Map<string, NonNullable<BepEvent['namedSetOfFiles']>>,
205
+ ): BepIpaDigest {
99
206
  // Default output group -> the fileSet (namedSetOfFiles) ids holding its outputs.
100
207
  const rootFileSetIds = (completed.outputGroup ?? [])
101
208
  .filter((og) => og.name === 'default' || !og.name)
102
209
  .flatMap((og) => (og.fileSets ?? []).map((fs) => fs.id))
103
210
  .filter((id): id is string => !!id);
104
211
  if (rootFileSetIds.length === 0) {
105
- throw new Error(`Build of ${label} reported no default output files.`);
106
- }
107
-
108
- // Index namedSetOfFiles by id and walk transitively (sets can nest).
109
- const namedSets = new Map<string, NonNullable<BepEvent['namedSetOfFiles']>>();
110
- for (const e of events) {
111
- const id = e.id?.namedSet?.id;
112
- if (id && e.namedSetOfFiles) namedSets.set(id, e.namedSetOfFiles);
212
+ throw new RbeBepError('no-output', `Build of ${label} reported no default output files.`);
113
213
  }
114
214
 
115
215
  const seen = new Set<string>();
@@ -127,15 +227,23 @@ export function parseTopLevelIpaDigest(bepJson: string, label: string): BepIpaDi
127
227
  if (!f.name || !f.name.endsWith('.ipa')) continue;
128
228
  const digest = f.uri ? parseBytestreamDigest(f.uri) : null;
129
229
  if (digest) {
130
- // The instance cache is keyed by SHA-256. A non-64-hex digest means the
131
- // build used a different digest function (Bazel 9 defaults to BLAKE3),
132
- // which the server CAS cannot resolve — surface that clearly here rather
133
- // than letting it fail later as a cryptic cache miss.
134
- if (!SHA256_HEX.test(digest.hash)) {
135
- throw new Error(
136
- `Built ${f.name} with a non-SHA256 digest (got ${digest.hash.length} hex chars, expected 64; ` +
137
- `likely BLAKE3 on Bazel 9). The instance cache is keyed by SHA-256 — rebuild with ` +
138
- `--digest_function=sha256, e.g. \`bazelisk --digest_function=sha256 build --config=limrun ${label}\`.`,
230
+ // The instance cache is keyed by SHA-256. Reject any other digest
231
+ // function — Bazel 9 defaults to BLAKE3, whose URI carries an explicit
232
+ // `blake3` function segment (and whose 64-hex hash is indistinguishable
233
+ // from SHA-256 by length, so the hash form alone can't catch it). Surface
234
+ // it here with the fix rather than letting it fail later as a cryptic
235
+ // server cache miss.
236
+ const isSha256 =
237
+ digest.digestFunction === undefined ?
238
+ SHA256_HEX.test(digest.hash)
239
+ : digest.digestFunction === 'sha256';
240
+ if (!isSha256) {
241
+ const got = digest.digestFunction ?? `${digest.hash.length} hex chars`;
242
+ throw new RbeBepError(
243
+ 'non-sha256',
244
+ `Built ${f.name} with a non-SHA256 digest (${got}; Bazel 9 defaults to BLAKE3). The instance ` +
245
+ `cache is keyed by SHA-256 — rebuild with --digest_function=sha256, e.g. ` +
246
+ `\`bazelisk --digest_function=sha256 build --config=limrun ${label}\`.`,
139
247
  );
140
248
  }
141
249
  return { hash: digest.hash, sizeBytes: digest.sizeBytes, ipaName: f.name };
@@ -148,11 +256,37 @@ export function parseTopLevelIpaDigest(bepJson: string, label: string): BepIpaDi
148
256
  }
149
257
 
150
258
  if (localOnlyIpa) {
151
- throw new Error(
259
+ throw new RbeBepError(
260
+ 'local-only',
152
261
  `Found ${label}'s .ipa (${localOnlyIpa}) but it has no remote (bytestream) digest — it was ` +
153
262
  `built locally, not remotely executed. Ensure you built with --config=limrun so the .ipa is ` +
154
263
  `produced in the instance's cache.`,
155
264
  );
156
265
  }
157
- throw new Error(`No .ipa output found for ${label} in the build event log.`);
266
+ throw new RbeBepError('no-ipa', `No .ipa output found for ${label} in the build event log.`);
267
+ }
268
+
269
+ /**
270
+ * Finds the `.ipa` produced for `label` in a BEP stream and returns its CAS
271
+ * digest. Throws a typed RbeBepError when the target/.ipa is absent, its output
272
+ * was materialized locally (a `file://` URI) instead of left in CAS, or it was
273
+ * built with a non-SHA256 digest the instance cache cannot resolve.
274
+ */
275
+ export function parseTopLevelIpaDigest(bepJson: string, label: string): BepIpaDigest {
276
+ const canonical = canonicalizeLabel(label);
277
+ const events = parseEvents(bepJson);
278
+
279
+ // The successful TargetComplete for the requested label (matched against the
280
+ // canonical //pkg:name form Bazel records, so `//App` matches `//App:App`).
281
+ const completed = events.find((e) => e.id?.targetCompleted?.label === canonical && e.completed?.success)
282
+ ?.completed;
283
+ if (!completed) {
284
+ throw new RbeBepError(
285
+ 'no-build',
286
+ `No successful build of ${label} found in the build event log. ` +
287
+ `Build it first with --config=limrun (e.g. \`bazelisk --digest_function=sha256 build --config=limrun ${label}\`).`,
288
+ );
289
+ }
290
+
291
+ return resolveIpaDigest(label, completed, indexNamedSets(events));
158
292
  }
@@ -220,9 +220,9 @@ export type XcodeClient = {
220
220
 
221
221
  /**
222
222
  * Attach a simulator to this xcode instance.
223
- * After attaching, the latest xcodebuild build (if any) is installed on the
224
- * simulator. RBE builds are not auto-installed on attach — install them
225
- * explicitly with `installRbeBuildFromBep` while a simulator is attached.
223
+ * After attaching, the latest installable build is installed on the simulator:
224
+ * the latest xcodebuild build or the latest RBE build (whichever is newer),
225
+ * unless that build's exact digest is already on the simulator.
226
226
  */
227
227
  attachSimulator: (
228
228
  simulator: IosInstance | { apiUrl: string; token: string },
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const VERSION = '0.31.2'; // x-release-please-version
1
+ export const VERSION = '0.31.4'; // x-release-please-version