peachsim 0.1.730 → 0.1.746

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.
Files changed (107) hide show
  1. package/cli/bridge-flow-runner.ts +15 -2
  2. package/cli/commands/control.ts +13 -3
  3. package/cli/commands/desktop.ts +1 -0
  4. package/cli/commands/electron.ts +12 -8
  5. package/cli/commands/flow.ts +1 -3
  6. package/cli/commands/install-desktop.ts +105 -66
  7. package/cli/commands/logout.ts +5 -1
  8. package/cli/commands/no-bridge-hint.ts +2 -2
  9. package/cli/commands/swift.ts +26 -9
  10. package/cli/desktop-companion.ts +36 -9
  11. package/cli/drivers/desktop.ts +65 -0
  12. package/cli/drivers/electron.ts +4 -66
  13. package/cli/drivers/index.ts +1 -0
  14. package/cli/drivers/playwright-sim-host.ts +2 -0
  15. package/cli/drivers/playwright.ts +3 -3
  16. package/cli/drivers/registry.ts +7 -2
  17. package/cli/drivers/types.ts +4 -3
  18. package/cli/maestro-js.ts +10 -0
  19. package/cli/outbound-endpoints.ts +2 -2
  20. package/dist-lib/agent-daemon-client.cjs +1 -1
  21. package/dist-lib/agent-events.cjs +1 -1
  22. package/dist-lib/agent-identity.cjs +1 -1
  23. package/dist-lib/agent-sessions.cjs +1 -1
  24. package/dist-lib/attached-projects.cjs +1 -1
  25. package/dist-lib/auth/shared-session.cjs +39 -9
  26. package/dist-lib/backend-origin.cjs +1 -1
  27. package/dist-lib/beta.cjs +1 -1
  28. package/dist-lib/beta.mjs +1 -1
  29. package/dist-lib/bridge-constants.cjs +1 -1
  30. package/dist-lib/bridge-contract-input.cjs +1 -1
  31. package/dist-lib/bridge-contract-input.mjs +1 -1
  32. package/dist-lib/bridge-contract.cjs +1 -1
  33. package/dist-lib/bridge-contract.mjs +1 -1
  34. package/dist-lib/capture-contract.cjs +13 -19
  35. package/dist-lib/capture-contract.mjs +13 -19
  36. package/dist-lib/cli-constants.cjs +1 -1
  37. package/dist-lib/cloud-contract.cjs +1 -1
  38. package/dist-lib/cloud-contract.mjs +1 -1
  39. package/dist-lib/cloud-test-args.cjs +1 -1
  40. package/dist-lib/cloud-test-args.mjs +1 -1
  41. package/dist-lib/cloud.cjs +1 -1
  42. package/dist-lib/cloud.mjs +1 -1
  43. package/dist-lib/config.cjs +1 -1
  44. package/dist-lib/detox/index.cjs +117 -11
  45. package/dist-lib/dev-bundle-resolution.cjs +1 -1
  46. package/dist-lib/home-paths.cjs +32 -1
  47. package/dist-lib/host/bridge-host.cjs +1741 -540
  48. package/dist-lib/host/fetch-proxy-handler.cjs +1 -1
  49. package/dist-lib/host/fetch-proxy-overrides.cjs +1 -1
  50. package/dist-lib/host/fetch-proxy-overrides.mjs +1 -1
  51. package/dist-lib/host/replacement-module-handler.cjs +1 -1
  52. package/dist-lib/host/websocket-proxy.cjs +1 -1
  53. package/dist-lib/index.cjs +17 -22
  54. package/dist-lib/jump-to-source-babel.cjs +1 -1
  55. package/dist-lib/jump-to-source-native.cjs +1 -1
  56. package/dist-lib/kotlin-host.cjs +1 -1
  57. package/dist-lib/menu.cjs +1 -1
  58. package/dist-lib/menu.mjs +1 -1
  59. package/dist-lib/metro-fingerprint-registry.cjs +1 -1
  60. package/dist-lib/metro-fingerprint-registry.mjs +1 -1
  61. package/dist-lib/metro-production-bundle.cjs +1 -1
  62. package/dist-lib/metro-production-bundle.mjs +1 -1
  63. package/dist-lib/metro.cjs +1 -1
  64. package/dist-lib/native-app-identity.cjs +1 -1
  65. package/dist-lib/native-app-identity.mjs +1 -1
  66. package/dist-lib/native-firebase-metadata.cjs +1 -1
  67. package/dist-lib/native-firebase-metadata.mjs +1 -1
  68. package/dist-lib/plist.cjs +1 -1
  69. package/dist-lib/plist.mjs +1 -1
  70. package/dist-lib/profiles.cjs +1 -1
  71. package/dist-lib/public-brand.cjs +1 -1
  72. package/dist-lib/react-native-host-modules.cjs +1 -1
  73. package/dist-lib/react-native-host-modules.mjs +1 -1
  74. package/dist-lib/render-mode.cjs +1 -1
  75. package/dist-lib/scripts/dev-server-scanner.cjs +1 -1
  76. package/dist-lib/sdk.cjs +2 -1
  77. package/dist-lib/sdk.mjs +2 -1
  78. package/dist-lib/skills.cjs +2431 -1075
  79. package/dist-lib/swift-compile.cjs +374 -65
  80. package/dist-lib/swift-compile.mjs +369 -66
  81. package/dist-lib/swift-package.cjs +146 -9
  82. package/dist-lib/swift-package.mjs +144 -9
  83. package/dist-lib/swift-submit.cjs +2 -2
  84. package/dist-lib/swift-submit.mjs +2 -2
  85. package/dist-lib/swift-toolchain.cjs +1 -1
  86. package/dist-lib/swift.cjs +1 -1
  87. package/dist-lib/vite-swift.cjs +1690 -233
  88. package/dist-lib/vite.cjs +4 -4
  89. package/package.json +1 -1
  90. package/skills/contrast/SKILL.md +1 -1
  91. package/src/auth/shared-session.ts +45 -9
  92. package/src/bridge-constants.ts +7 -7
  93. package/src/capture-contract.ts +18 -33
  94. package/src/desktop-host-delivery.ts +81 -0
  95. package/src/home-paths.ts +30 -0
  96. package/src/host/bridge-host.ts +11 -6
  97. package/src/native-seam-manifest.ts +0 -11
  98. package/src/sim-client.ts +1 -0
  99. package/src/swift-compile.ts +479 -76
  100. package/src/swift-distribution.lock.json +11 -11
  101. package/src/swift-host.ts +338 -45
  102. package/src/swift-package.ts +204 -19
  103. package/src/swift-remote-packages.ts +255 -0
  104. package/src/swift-submit.ts +4 -1
  105. package/src/vite-plugin-one.ts +2 -2
  106. package/src/vite-plugin.ts +29 -9
  107. package/src/xcode-project.ts +612 -0
@@ -1,22 +1,22 @@
1
1
  {
2
- "id": "42c53105b2e06d39",
2
+ "id": "d27a9b6d2aee64d4",
3
3
  "release": "swift-6.3-RELEASE",
4
- "wasmSdkId": "swift-6.3-RELEASE_wasm-peach-728c7263",
5
- "wasmSdkUrl": "https://r2.sootsim.com/swift-wasm-sdk/swift-6.3-RELEASE_wasm-peach-728c7263.artifactbundle.tar.gz",
6
- "wasmSdkChecksum": "5df8ab7be805d67d69d8dcd3ef7eec940b4eb1f3261410eba09d891ee170c559",
7
- "source": "d8c4bd1a0ffb45a9cad3fd15de02bc0d93e459fc",
4
+ "wasmSdkId": "swift-6.3-RELEASE_wasm-peach-41921598-peach-26be2317",
5
+ "wasmSdkUrl": "https://r2.sootsim.com/swift-wasm-sdk/swift-6.3-RELEASE_wasm-peach-41921598-peach-26be2317.artifactbundle.tar.gz",
6
+ "wasmSdkChecksum": "f34f29d9426f42b04d1c38585d61cf38ecb5357f4287c4843870bec63de6b08c",
7
+ "source": "8f9873608a950d832ec49cf4bbc32fb65689e5f7",
8
8
  "hosts": {
9
9
  "darwin-arm64": {
10
- "sha256": "d58abb1193369130460267e9cc57330f13de28e7a3aa404aeb7b9323cbd29384",
11
- "size": 7115901
10
+ "sha256": "b7fa68565ae1be6ee7964390b4d07949228894ac4a06fe7f58e4238dd0650197",
11
+ "size": 19319227
12
12
  },
13
13
  "linux-x64": {
14
- "sha256": "1cdd181fdd228e9f928367820cd39db2e703a7af47a819f7d26ed77b96839309",
15
- "size": 7121403
14
+ "sha256": "733ebee3a16142c97575532daf49990df57171ede17de6530a9c5906471d96ca",
15
+ "size": 19324099
16
16
  },
17
17
  "linux-arm64": {
18
- "sha256": "288e3ff43c881a63ec201856a61a90de323e8aa01714d47be65e8cac98af1738",
19
- "size": 7120448
18
+ "sha256": "84b258d95a71536392f25d2afb8c40835df771594a2c0e42049ee33781ce78ad",
19
+ "size": 19323438
20
20
  }
21
21
  }
22
22
  }
package/src/swift-host.ts CHANGED
@@ -15,11 +15,21 @@ import { authHeaderValue, resolveCliAuth } from '../cli/auth'
15
15
  import { resolveContrastBackendOrigin } from './backend-origin'
16
16
  import {
17
17
  compileSwiftProjectUnit,
18
+ compileTargets,
19
+ missingSwiftModules,
20
+ swiftAvailableModules,
21
+ swiftPeachModules,
18
22
  withNativeSourceGlue,
19
23
  swiftModuleInputFiles,
24
+ swiftApplicationEntry,
20
25
  } from './swift-compile'
21
26
  import { swiftDistributionRoot, swiftModuleToolchain } from './swift-distribution'
22
- import { swiftImplicitResourcePaths } from './swift-package'
27
+ import { swiftImplicitResourcePaths, swiftManifestTargets } from './swift-package'
28
+ import {
29
+ REMOTE_CHECKOUTS,
30
+ resolveRemotePackages,
31
+ type RemotePackages,
32
+ } from './swift-remote-packages'
23
33
  import {
24
34
  MAX_SWIFT_COMPILE_FILES,
25
35
  MAX_SWIFT_COMPILE_SOURCE_BYTES,
@@ -36,6 +46,7 @@ import {
36
46
  type SwiftImageUpload,
37
47
  type SwiftNativeModules,
38
48
  } from './swift-submit'
49
+ import { readXcodeAppTarget, type XcodeAppTarget } from './xcode-project'
39
50
  import type { SwiftEntryModuleArgs } from '../../sootsim-swift/src/entryModule'
40
51
  import type {
41
52
  AppCatalogColor,
@@ -162,10 +173,157 @@ export function projectFor(
162
173
  return created
163
174
  }
164
175
 
176
+ // an xcode project compiles its application target, not its directory: the
177
+ // project directory is the package path, and the target's member files, plus
178
+ // the local packages it links, are the input tree.
179
+ interface XcodeProjectEntry {
180
+ xcodeproj: string
181
+ target?: string
182
+ /** peach's own modules: a remote product named one links it, never its source. */
183
+ provided: ReadonlySet<string> | null
184
+ remote?: { key: string; packages: RemotePackages }
185
+ }
186
+
187
+ const xcodeProjects = new Map<string, XcodeProjectEntry>()
188
+
189
+ /**
190
+ * registers an xcode project's application target as a compile unit and
191
+ * returns the package path that names it. compiling locally against
192
+ * `moduleRoot`, its remote package products compile from their pinned source;
193
+ * a hosted compile has no local module to tell which products peach provides.
194
+ */
195
+ export function openXcodeProject(
196
+ xcodeproj: string,
197
+ target: string | undefined,
198
+ moduleRoot: string | undefined,
199
+ ): string {
200
+ const app = readXcodeAppTarget(xcodeproj, target)
201
+ const root = fs.realpathSync(app.projectDir)
202
+ xcodeProjects.set(root, {
203
+ xcodeproj,
204
+ target: app.name,
205
+ provided: moduleRoot ? new Set(swiftPeachModules(moduleRoot)) : null,
206
+ })
207
+ return root
208
+ }
209
+
210
+ // remote packages resolve once per set of linked products and pins.
211
+ function remotePackages(entry: XcodeProjectEntry, app: XcodeAppTarget): RemotePackages {
212
+ if (!entry.provided) return { targets: [], unavailable: new Map() }
213
+ const key = JSON.stringify([
214
+ app.remoteProducts,
215
+ app.packageResolved,
216
+ app.packageResolved ? fs.statSync(app.packageResolved).mtimeMs : 0,
217
+ ])
218
+ if (entry.remote?.key !== key) {
219
+ entry.remote = {
220
+ key,
221
+ packages: resolveRemotePackages(
222
+ fs.realpathSync(app.projectDir),
223
+ app.packageResolved,
224
+ app.remoteProducts,
225
+ entry.provided,
226
+ ),
227
+ }
228
+ }
229
+ return entry.remote.packages
230
+ }
231
+
232
+ // the app target's sources compile as one executable target under this
233
+ // directory, each at its project-relative path, so a diagnostic still names
234
+ // the file; local package targets keep their own project-relative paths.
235
+ export const XCODE_APP_DIR = '__app__'
236
+ // remote package checkouts compile here: swiftpm owns `.build` in the
237
+ // compile's package root as its scratch directory.
238
+ const XCODE_PACKAGES_DIR = '__packages__'
239
+ const CHECKOUTS_PREFIX = `${REMOTE_CHECKOUTS.split(path.sep).join(path.posix.sep)}/`
240
+
241
+ // the compile input key of a project-relative path in an xcode layout.
242
+ function xcodeInputKey(appSources: Set<string>, file: string, relative: string): string {
243
+ if (appSources.has(file)) return path.posix.join(XCODE_APP_DIR, relative)
244
+ return relative.startsWith(CHECKOUTS_PREFIX)
245
+ ? path.posix.join(XCODE_PACKAGES_DIR, relative.slice(CHECKOUTS_PREFIX.length))
246
+ : relative
247
+ }
248
+
249
+ interface XcodeLayout {
250
+ app: XcodeAppTarget
251
+ /** the local and remote package targets the app links, with project-relative paths. */
252
+ targets: ReturnType<typeof swiftManifestTargets>
253
+ /** remote products that cannot compile, with why. */
254
+ unavailable: RemotePackages['unavailable']
255
+ files: string[]
256
+ }
257
+
258
+ function xcodeLayout(root: string): XcodeLayout | null {
259
+ const registered = xcodeProjects.get(root)
260
+ if (!registered) return null
261
+ const app = readXcodeAppTarget(registered.xcodeproj, registered.target)
262
+ const files = new Set([app.pbxproj, ...app.sources, ...app.resources])
263
+ if (app.infoPlistFile) files.add(app.infoPlistFile)
264
+ const targets: XcodeLayout['targets'] = []
265
+ for (const dir of app.localPackages) {
266
+ const manifest = path.join(dir, 'Package.swift')
267
+ files.add(manifest)
268
+ const prefix = path.relative(root, dir).split(path.sep).join(path.posix.sep)
269
+ const all = packageFiles(dir).map((file) =>
270
+ path.relative(dir, file).split(path.sep).join(path.posix.sep),
271
+ )
272
+ for (const target of swiftManifestTargets(fs.readFileSync(manifest, 'utf8'), all)) {
273
+ if (target.executable) continue
274
+ targets.push({ ...target, path: path.posix.join(prefix, target.path) })
275
+ for (const relative of all) {
276
+ if (relative.startsWith(`${target.path}/`)) files.add(path.join(dir, relative))
277
+ }
278
+ }
279
+ }
280
+ const remote = remotePackages(registered, app)
281
+ for (const target of remote.targets) {
282
+ targets.push({
283
+ ...target,
284
+ path: path.posix.join(
285
+ XCODE_PACKAGES_DIR,
286
+ path
287
+ .relative(path.join(root, REMOTE_CHECKOUTS), target.dir)
288
+ .split(path.sep)
289
+ .join(path.posix.sep),
290
+ ),
291
+ })
292
+ for (const file of target.files) files.add(file)
293
+ }
294
+ return {
295
+ app,
296
+ targets,
297
+ unavailable: remote.unavailable,
298
+ files: [...files].sort(),
299
+ }
300
+ }
301
+
302
+ // the manifest the compile reads for an xcode app: the app target and each
303
+ // linked local package target, in the forms swiftManifestTargets scans.
304
+ function xcodeManifest(layout: XcodeLayout): string {
305
+ const settings = (mode: number, mainActor: boolean) =>
306
+ `swiftSettings: [.swiftLanguageMode(.v${mode})${mainActor ? ', .defaultIsolation(MainActor.self)' : ''}]`
307
+ return [
308
+ '// swift-tools-version: 6.2',
309
+ 'import PackageDescription',
310
+ 'let package = Package(name: "app", targets: [',
311
+ ` .executableTarget(name: "${layout.app.moduleName}", path: "${XCODE_APP_DIR}", ${settings(layout.app.languageMode, layout.app.mainActorIsolation)}),`,
312
+ ...layout.targets.map(
313
+ (target) =>
314
+ ` .target(name: "${target.name}", path: "${target.path}", ${target.resources ? 'resources: [], ' : ''}${settings(target.languageMode, target.mainActorIsolation)}),`,
315
+ ),
316
+ '])',
317
+ '',
318
+ ].join('\n')
319
+ }
320
+
165
321
  // preflight the entire input tree before reading any source or resource bytes.
166
322
  // a nested package is a separate compile unit; ignored build trees never enter it.
167
323
  function packageFiles(root: string): string[] {
168
324
  const canonicalRoot = fs.realpathSync(root)
325
+ const xcode = xcodeLayout(canonicalRoot)
326
+ if (xcode) return xcode.files
169
327
  const found: string[] = []
170
328
  const walk = (dir: string): void => {
171
329
  for (const entry of fs
@@ -249,20 +407,34 @@ function readPackageFile(root: string, file: string, maxBytes: number): Buffer {
249
407
  }
250
408
 
251
409
  export function sourceFiles(root: string): string[] {
252
- const files = packageFiles(root).filter((file) => file.endsWith('.swift'))
253
- if (
254
- files.length > MAX_SWIFT_COMPILE_FILES ||
255
- files.reduce((total, file) => total + fs.statSync(file).size, 0) >
256
- MAX_SWIFT_COMPILE_SOURCE_BYTES
257
- ) {
258
- throw new Error('swift sources exceed the compile service caps')
259
- }
260
- return files
410
+ return packageFiles(root).filter((file) => file.endsWith('.swift'))
261
411
  }
262
412
 
263
413
  function resourceCandidateFiles(root: string): string[] {
264
- return packageFiles(root).filter((file) => !file.endsWith('.swift'))
414
+ return packageFiles(root).filter(
415
+ (file) => !file.endsWith('.swift') && path.basename(file) !== 'project.pbxproj',
416
+ )
417
+ }
418
+
419
+ // the hosted compile service caps what a project sends it; a local compile
420
+ // reads the author's own files, so nothing caps them there.
421
+ const HOSTED_LIMITS = {
422
+ resources: MAX_SWIFT_COMPILE_RESOURCES,
423
+ resourceBytes: MAX_SWIFT_COMPILE_RESOURCE_BYTES,
424
+ resourceTotalBytes: MAX_SWIFT_COMPILE_RESOURCE_TOTAL_BYTES,
425
+ images: MAX_SWIFT_COMPILE_IMAGES,
426
+ imageBytes: MAX_SWIFT_COMPILE_IMAGE_BYTES,
427
+ imageTotalBytes: MAX_SWIFT_COMPILE_IMAGE_TOTAL_BYTES,
265
428
  }
429
+ const LOCAL_LIMITS: typeof HOSTED_LIMITS = {
430
+ resources: Infinity,
431
+ resourceBytes: Infinity,
432
+ resourceTotalBytes: Infinity,
433
+ images: Infinity,
434
+ imageBytes: Infinity,
435
+ imageTotalBytes: Infinity,
436
+ }
437
+ const inputLimits = (hosted: boolean) => (hosted ? HOSTED_LIMITS : LOCAL_LIMITS)
266
438
 
267
439
  const RESOURCE_CONTENT_TYPES: Record<string, string> = {
268
440
  '.avif': 'image/avif',
@@ -295,6 +467,7 @@ export function buildSwiftResourceTable(
295
467
  project: SwiftProject,
296
468
  baseUrl: string,
297
469
  ): SwiftResourceTable {
470
+ const limits = inputLimits(Boolean(project.hosted))
298
471
  const absolute = resourceCandidateFiles(project.packagePath)
299
472
  const relative = absolute.map((file) =>
300
473
  path.relative(project.packagePath, file).split(path.sep).join(path.posix.sep),
@@ -305,11 +478,9 @@ export function buildSwiftResourceTable(
305
478
  // catalog json, and the glyph template a symbolset names
306
479
  if (!file || !/(^|\/)Contents\.json$|\.symbolset\/[^/]+\.svg$/.test(candidate))
307
480
  return null
308
- return readPackageFile(
309
- project.packagePath,
310
- file,
311
- MAX_SWIFT_COMPILE_RESOURCE_BYTES,
312
- ).toString('utf8')
481
+ return readPackageFile(project.packagePath, file, limits.resourceBytes).toString(
482
+ 'utf8',
483
+ )
313
484
  }
314
485
  const found = enumerateBundleImages(relative, readContents)
315
486
  const colors = enumerateBundleColors(relative, readContents)
@@ -323,16 +494,12 @@ export function buildSwiftResourceTable(
323
494
  const disk = byRelative.get(file)
324
495
  if (!disk) continue
325
496
  if (
326
- Object.keys(images).length >= MAX_SWIFT_COMPILE_IMAGES ||
327
- imageBytes + fs.statSync(disk).size > MAX_SWIFT_COMPILE_IMAGE_TOTAL_BYTES
497
+ Object.keys(images).length >= limits.images ||
498
+ imageBytes + fs.statSync(disk).size > limits.imageTotalBytes
328
499
  ) {
329
500
  throw new Error('swift image pack exceeds the compile service caps')
330
501
  }
331
- const bytes = readPackageFile(
332
- project.packagePath,
333
- disk,
334
- MAX_SWIFT_COMPILE_IMAGE_BYTES,
335
- )
502
+ const bytes = readPackageFile(project.packagePath, disk, limits.imageBytes)
336
503
  imageBytes += bytes.length
337
504
  const hash = createHash('sha256').update(bytes).digest('hex').slice(0, 16)
338
505
  const ext = path.posix.extname(file).toLowerCase()
@@ -374,9 +541,6 @@ export function buildSwiftResourceTable(
374
541
  // paths pack first (the Resources root is the bundle root) with a lexical
375
542
  // tiebreak, and the first spelling of a basename wins a collision. data json
376
543
  // bundle caps match the hosted compiler; oversized input refuses before reading.
377
- const BUNDLE_MAX_FILES = MAX_SWIFT_COMPILE_RESOURCES
378
- const BUNDLE_MAX_FILE_BYTES = MAX_SWIFT_COMPILE_RESOURCE_BYTES
379
- const BUNDLE_MAX_TOTAL_BYTES = MAX_SWIFT_COMPILE_RESOURCE_TOTAL_BYTES
380
544
 
381
545
  export interface SwiftBundlePack {
382
546
  /** basename to base64 bytes for the mount's /res. */
@@ -385,8 +549,12 @@ export interface SwiftBundlePack {
385
549
  watched: string[]
386
550
  }
387
551
 
388
- export function buildSwiftBundleFiles(packagePath: string): SwiftBundlePack {
552
+ export function buildSwiftBundleFiles(
553
+ packagePath: string,
554
+ hosted: boolean,
555
+ ): SwiftBundlePack {
389
556
  packagePath = fs.realpathSync(packagePath)
557
+ const limits = inputLimits(hosted)
390
558
  // resourceCandidateFiles walks every non-ignored file with the nested
391
559
  // package exclusion; the shared enumeration decides namespace, order and
392
560
  // collisions, identically for every host that packs.
@@ -396,18 +564,30 @@ export function buildSwiftBundleFiles(packagePath: string): SwiftBundlePack {
396
564
  const files: Record<string, string> = {}
397
565
  const watched: string[] = []
398
566
  let total = 0
567
+ const xcode = xcodeLayout(packagePath)
568
+ if (xcode) {
569
+ const bytes = Buffer.from(xcode.app.infoPlist)
570
+ if (bytes.length > limits.resourceBytes || bytes.length > limits.resourceTotalBytes)
571
+ throw new Error('swift launch Info.plist exceeds bundle resource limits')
572
+ files['Info.plist'] = bytes.toString('base64')
573
+ total += bytes.length
574
+ if (xcode.app.infoPlistFile)
575
+ watched.push(
576
+ path.relative(packagePath, xcode.app.infoPlistFile).split(path.sep).join('/'),
577
+ )
578
+ }
399
579
  for (const { base, file } of enumerateBundleJson(relative)) {
400
580
  watched.push(file)
401
581
  const disk = path.join(packagePath, ...file.split('/'))
402
582
  if (
403
- Object.keys(files).length >= BUNDLE_MAX_FILES ||
404
- total + fs.statSync(disk).size > BUNDLE_MAX_TOTAL_BYTES
583
+ Object.keys(files).length >= limits.resources ||
584
+ total + fs.statSync(disk).size > limits.resourceTotalBytes
405
585
  ) {
406
586
  throw new Error(
407
- `swift bundle pack exceeds ${BUNDLE_MAX_FILES} files / ${BUNDLE_MAX_TOTAL_BYTES} bytes`,
587
+ `swift bundle pack exceeds ${limits.resources} files / ${limits.resourceTotalBytes} bytes`,
408
588
  )
409
589
  }
410
- const bytes = readPackageFile(packagePath, disk, BUNDLE_MAX_FILE_BYTES)
590
+ const bytes = readPackageFile(packagePath, disk, limits.resourceBytes)
411
591
  files[base] = bytes.toString('base64')
412
592
  total += bytes.length
413
593
  }
@@ -501,22 +681,19 @@ export function icuUrl(origin: string, artifact: SwiftArtifact): string {
501
681
  }
502
682
 
503
683
  function packageSources(packagePath: string): Record<string, string> {
504
- const sources = sourceFiles(packagePath)
505
- if (sources.length > MAX_SWIFT_COMPILE_FILES)
506
- throw new Error('swift source file cap exceeded')
507
- let total = 0
508
- for (const file of sources) total += fs.statSync(file).size
509
- if (total > MAX_SWIFT_COMPILE_SOURCE_BYTES)
510
- throw new Error('swift source byte cap exceeded')
684
+ const xcode = xcodeLayout(packagePath)
685
+ const appSources = new Set(xcode?.app.sources)
511
686
  const files: Record<string, string> = {}
512
- for (const file of sources) {
687
+ for (const file of sourceFiles(packagePath)) {
513
688
  const relative = path.relative(packagePath, file).split(path.sep).join(path.posix.sep)
514
- files[relative] = readPackageFile(
689
+ if (xcode && path.basename(file) === 'Package.swift') continue
690
+ files[xcode ? xcodeInputKey(appSources, file, relative) : relative] = readPackageFile(
515
691
  packagePath,
516
692
  file,
517
693
  MAX_SWIFT_COMPILE_SOURCE_BYTES,
518
694
  ).toString('utf8')
519
695
  }
696
+ if (xcode) files['Package.swift'] = xcodeManifest(xcode)
520
697
  return files
521
698
  }
522
699
 
@@ -540,7 +717,7 @@ function packageSnapshot(project: SwiftProject): SwiftPackageSnapshot {
540
717
  ),
541
718
  ),
542
719
  resources: buildSwiftResourceTable(project, ''),
543
- bundleFiles: buildSwiftBundleFiles(project.packagePath),
720
+ bundleFiles: buildSwiftBundleFiles(project.packagePath, Boolean(project.hosted)),
544
721
  native,
545
722
  ...(native
546
723
  ? {
@@ -613,7 +790,18 @@ async function compile(
613
790
  package: content,
614
791
  }
615
792
  const toolchain = swiftModuleToolchain(moduleRoot)
616
- const artifact = await compileSwiftProjectUnit(snapshotCompileInput(snapshot, entry), {
793
+ const input = snapshotCompileInput(snapshot, entry)
794
+ const targets = compileTargets(input).map((target) => target.name)
795
+ const missing = missingSwiftModules(
796
+ input.files,
797
+ new Set([
798
+ ...swiftAvailableModules(moduleRoot, toolchain.toolchainBin),
799
+ // a project without a manifest compiles as the template's one App target
800
+ ...(targets.length > 0 ? targets : ['App']),
801
+ ]),
802
+ )
803
+ if (missing.size > 0) throw new Error(missingModulesMessage(packagePath, missing))
804
+ const artifact = await compileSwiftProjectUnit(input, {
617
805
  // the compile rewrites Sources/App in its root before swiftpm builds, and
618
806
  // its lock orders one process only: two vites on one root (a lane's dev
619
807
  // server and a capture, or two checkouts) build each other's sources. so
@@ -631,6 +819,13 @@ async function compile(
631
819
  ),
632
820
  moduleRoot,
633
821
  ...toolchain,
822
+ }).catch((error: unknown) => {
823
+ const report = error instanceof Error ? swiftApiGapReport(error.message) : null
824
+ throw report
825
+ ? new Error(`${error instanceof Error ? error.message : ''}\n\n${report}`, {
826
+ cause: error,
827
+ })
828
+ : error
634
829
  })
635
830
  return { ...artifact, stamp, package: content }
636
831
  }
@@ -642,6 +837,92 @@ async function compile(
642
837
  // public content address, which is fetched once and served from this dev
643
838
  // server like a local build. resource bytes share the compile snapshot and
644
839
  // immutable local asset addresses.
840
+ // the compiler stops a file at its errors, so its output reads one site at a
841
+ // time. this groups every error by the api it names, so a project learns
842
+ // which symbols Peach lacks, how often and where, from one run. paths are the
843
+ // project's own: app sources lose their compile directory, a remote package
844
+ // keeps its identity.
845
+ const API_GAP_PATTERNS: [RegExp, (match: RegExpExecArray) => string][] = [
846
+ [/^cannot find (?:type )?'([^']+)' in scope/, (match) => match[1]!],
847
+ // a typealias reports its aliased type after `aka`, which is the api's owner
848
+ [
849
+ /^(?:value of )?type '([^']+)'(?: \(aka '([^']+)'\))? has no member '([^']+)'/,
850
+ (match) =>
851
+ `${(match[2] ?? match[1]!).replace(/^Optional<(.+)>$/, '$1').replace(/\?$/, '')}.${match[3]}`,
852
+ ],
853
+ [
854
+ /^module '([^']+)' has no member named '([^']+)'/,
855
+ (match) => `${match[1]}.${match[2]}`,
856
+ ],
857
+ [/^'([^']+)' is unavailable/, (match) => match[1]!],
858
+ [/^no such module '([^']+)'/, (match) => `module ${match[1]}`],
859
+ ]
860
+
861
+ export function swiftApiGapReport(output: string): string | null {
862
+ const gaps = new Map<string, { errors: number; files: Set<string> }>()
863
+ const other = new Map<string, { errors: number; files: Set<string> }>()
864
+ const seen = new Set<string>()
865
+ for (const line of output.split('\n')) {
866
+ const diagnostic = /^(\S[^:]*\.swift):(\d+):(\d+): error: (.+)$/.exec(line)
867
+ if (!diagnostic || seen.has(line)) continue
868
+ seen.add(line)
869
+ const file = diagnostic[1]!
870
+ .replace(new RegExp(`^${XCODE_APP_DIR}/`), '')
871
+ .replace(new RegExp(`^${XCODE_PACKAGES_DIR}/`), '')
872
+ const message = diagnostic[4]!
873
+ const rule = API_GAP_PATTERNS.find(([pattern]) => pattern.test(message))
874
+ const key = rule ? rule[1](rule[0].exec(message)!) : message
875
+ const table = rule ? gaps : other
876
+ const entry = table.get(key) ?? { errors: 0, files: new Set<string>() }
877
+ entry.errors++
878
+ entry.files.add(file)
879
+ table.set(key, entry)
880
+ }
881
+ if (gaps.size === 0) return null
882
+ const rows = (table: typeof gaps) => {
883
+ const sorted = [...table].sort(
884
+ ([left, a], [right, b]) => b.errors - a.errors || left.localeCompare(right),
885
+ )
886
+ const width = Math.min(48, Math.max(...sorted.map(([key]) => key.length)))
887
+ return sorted.map(
888
+ ([key, entry]) =>
889
+ ` ${key.padEnd(width)} ${String(entry.errors).padStart(3)} error${entry.errors === 1 ? ' ' : 's'} ${[...entry.files].sort().join(', ')}`,
890
+ )
891
+ }
892
+ const errors = [...gaps.values()].reduce((sum, entry) => sum + entry.errors, 0)
893
+ return [
894
+ `Peach lacks ${gaps.size} API${gaps.size === 1 ? '' : 's'} this app uses (${errors} error${errors === 1 ? '' : 's'}):`,
895
+ ...rows(gaps),
896
+ ...(other.size > 0
897
+ ? ['other errors, often caused by the ones above:', ...rows(other)]
898
+ : []),
899
+ ].join('\n')
900
+ }
901
+
902
+ // one report of every module the app imports that Peach lacks, so a project
903
+ // learns its whole gap from one run instead of one compile error at a time.
904
+ function missingModulesMessage(
905
+ packagePath: string,
906
+ missing: Map<string, string[]>,
907
+ ): string {
908
+ const layout = xcodeLayout(packagePath)
909
+ const remote = new Map(
910
+ (layout?.app.remoteProducts ?? []).map((entry) => [
911
+ entry.product,
912
+ entry.repositoryURL,
913
+ ]),
914
+ )
915
+ const width = Math.max(...[...missing.keys()].map((name) => name.length))
916
+ return [
917
+ `Peach does not provide ${missing.size} module${missing.size === 1 ? '' : 's'} this app imports:`,
918
+ ...[...missing].map(([name, files]) => {
919
+ const source = remote.get(name)
920
+ const reason = layout?.unavailable.get(name)?.reason
921
+ return ` ${name.padEnd(width)} ${files.length} file${files.length === 1 ? '' : 's'}${source ? `, package ${source}` : ''}${reason ? ` (${reason})` : ''}`
922
+ }),
923
+ ].join('\n')
924
+ }
925
+
645
926
  async function compileRemote(
646
927
  packagePath: string,
647
928
  entry: string,
@@ -669,6 +950,16 @@ async function compileRemote(
669
950
  signal,
670
951
  })
671
952
  if (!submit) throw new Error('swift compile: no credential to submit with')
953
+ const sources = Object.values(snapshot.files)
954
+ const bytes = sources.reduce((total, text) => total + Buffer.byteLength(text), 0)
955
+ if (
956
+ sources.length > MAX_SWIFT_COMPILE_FILES ||
957
+ bytes > MAX_SWIFT_COMPILE_SOURCE_BYTES
958
+ ) {
959
+ throw new Error(
960
+ `${path.basename(packagePath)} has ${sources.length} Swift files (${bytes} bytes), over Contrast's compile limits of ${MAX_SWIFT_COMPILE_FILES} files and ${MAX_SWIFT_COMPILE_SOURCE_BYTES} bytes; compile it on this machine with the Swift distribution instead`,
961
+ )
962
+ }
672
963
  const descriptor = snapshot.native
673
964
  const address = await compileSwiftProject(
674
965
  submit,
@@ -781,7 +1072,7 @@ export function swiftPackageWatchFiles(
781
1072
  ): string[] {
782
1073
  packagePath = fs.realpathSync(packagePath)
783
1074
  return [
784
- path.join(packagePath, 'Package.swift'),
1075
+ ...(xcodeProjects.has(packagePath) ? [] : [path.join(packagePath, 'Package.swift')]),
785
1076
  ...sourceFiles(packagePath),
786
1077
  ...nativeSourceFiles(packagePath),
787
1078
  ...resourceCandidateFiles(packagePath),
@@ -800,14 +1091,15 @@ export async function swiftPackageMetadata(
800
1091
  ): Promise<SwiftEntryModuleArgs> {
801
1092
  packagePath = fs.realpathSync(packagePath)
802
1093
  const mains = sourceFiles(packagePath).filter((file) =>
803
- /@main\s+(?:public\s+)?struct\s+\w+\s*:/.test(
1094
+ swiftApplicationEntry(
1095
+ file,
804
1096
  readPackageFile(packagePath, file, MAX_SWIFT_COMPILE_SOURCE_BYTES).toString('utf8'),
805
1097
  ),
806
1098
  )
807
1099
  const native = nativeSourceDescriptor(packagePath)
808
1100
  if (mains.length > 1 || (mains.length === 0 && !native)) {
809
1101
  throw new Error(
810
- `swift package ${packageName} needs exactly one @main source or generated native modules; found ${mains.length}`,
1102
+ `swift package ${packageName} needs exactly one application entry source or generated native modules; found ${mains.length}`,
811
1103
  )
812
1104
  }
813
1105
  const project = projectFor(packagePath, hosted)
@@ -897,4 +1189,5 @@ export function serveSwiftPackageAsset(pathname: string, res: ServerResponse): b
897
1189
 
898
1190
  export function disposeSwiftProject(packagePath: string): void {
899
1191
  projects.delete(fs.realpathSync(packagePath))
1192
+ xcodeProjects.delete(fs.realpathSync(packagePath))
900
1193
  }