@native-sdk/cli 0.4.3 → 0.5.0

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 (100) hide show
  1. package/build/app.zig +308 -72
  2. package/build/ts_run.mjs +63 -0
  3. package/build.zig +336 -7
  4. package/build.zig.zon +1 -0
  5. package/package.json +15 -10
  6. package/packages/core/package-lock.json +43 -0
  7. package/packages/core/package.json +37 -0
  8. package/packages/core/rt/rt.zig +1579 -0
  9. package/packages/core/sdk/bytes_text_methods.d.ts +88 -0
  10. package/packages/core/sdk/core.ts +694 -0
  11. package/packages/core/sdk/events.ts +137 -0
  12. package/packages/core/sdk/text.ts +519 -0
  13. package/packages/core/src/checker.ts +1869 -0
  14. package/packages/core/src/cli.ts +68 -0
  15. package/packages/core/src/devhost.ts +291 -0
  16. package/packages/core/src/devhost_resolver.mjs +22 -0
  17. package/packages/core/src/diagnostics.ts +417 -0
  18. package/packages/core/src/emitter.ts +8815 -0
  19. package/packages/core/src/infer.ts +1515 -0
  20. package/packages/core/src/modules.ts +173 -0
  21. package/packages/core/src/ownership.ts +740 -0
  22. package/packages/core/src/text_polyfill.ts +337 -0
  23. package/packages/core/src/text_tables.ts +408 -0
  24. package/packages/core/src/transpile.ts +146 -0
  25. package/packages/core/src/typed_ast.ts +536 -0
  26. package/packages/core/src/types.ts +815 -0
  27. package/skill-data/core/SKILL.md +3 -0
  28. package/skill-data/native-ui/SKILL.md +30 -0
  29. package/skill-data/ts-core/SKILL.md +493 -0
  30. package/skill-data/zig/SKILL.md +208 -0
  31. package/skills/native-sdk/SKILL.md +1 -1
  32. package/src/app_runner/root.zig +75 -9
  33. package/src/app_runner/ts_core_main.zig +231 -0
  34. package/src/platform/linux/gtk_host.c +226 -21
  35. package/src/platform/windows/webview2_host.cpp +16 -10
  36. package/src/primitives/app_manifest/comptime_scene.zig +341 -0
  37. package/src/primitives/app_manifest/root.zig +13 -0
  38. package/src/primitives/app_manifest/types.zig +14 -0
  39. package/src/primitives/app_manifest/validation.zig +18 -0
  40. package/src/primitives/app_manifest/web_layer.zig +366 -0
  41. package/src/primitives/canvas/commands.zig +14 -7
  42. package/src/primitives/canvas/lazy_tls.zig +73 -0
  43. package/src/primitives/canvas/render_generic_resources.zig +22 -15
  44. package/src/primitives/canvas/root.zig +6 -0
  45. package/src/primitives/canvas/tests.zig +1 -0
  46. package/src/primitives/canvas/text_atlas.zig +40 -32
  47. package/src/primitives/canvas/text_layout_cache.zig +29 -22
  48. package/src/primitives/canvas/text_measure_cache.zig +56 -41
  49. package/src/primitives/canvas/text_spans.zig +39 -26
  50. package/src/primitives/canvas/tokens.zig +54 -0
  51. package/src/primitives/canvas/ui.zig +125 -0
  52. package/src/primitives/canvas/ui_markup.zig +22 -3
  53. package/src/primitives/canvas/ui_markup_compiled.zig +97 -7
  54. package/src/primitives/canvas/ui_markup_compiled_tests.zig +275 -0
  55. package/src/primitives/canvas/ui_markup_contract.zig +68 -14
  56. package/src/primitives/canvas/ui_markup_contract_tests.zig +257 -2
  57. package/src/primitives/canvas/ui_markup_reflect.zig +214 -0
  58. package/src/primitives/canvas/ui_markup_view.zig +105 -1
  59. package/src/primitives/canvas/ui_markup_view_tests.zig +336 -0
  60. package/src/primitives/canvas/ui_schema.zig +8 -0
  61. package/src/primitives/canvas/ui_schema_tests.zig +5 -4
  62. package/src/primitives/canvas/widget_invalidation.zig +14 -7
  63. package/src/primitives/canvas/widget_render.zig +25 -15
  64. package/src/primitives/canvas/widget_render_controls.zig +15 -9
  65. package/src/primitives/canvas/widget_runtime_tests.zig +44 -0
  66. package/src/root.zig +7 -0
  67. package/src/runtime/api.zig +7 -0
  68. package/src/runtime/bridge_responses.zig +2 -0
  69. package/src/runtime/builtin_bridge.zig +6 -0
  70. package/src/runtime/canvas_frame.zig +57 -36
  71. package/src/runtime/canvas_images.zig +9 -3
  72. package/src/runtime/canvas_widget_control_tests.zig +5 -6
  73. package/src/runtime/canvas_widget_runtime.zig +7 -4
  74. package/src/runtime/effects.zig +490 -2
  75. package/src/runtime/effects_host_tests.zig +594 -0
  76. package/src/runtime/flow.zig +8 -0
  77. package/src/runtime/platform_bridge_tests.zig +1 -0
  78. package/src/runtime/root.zig +18 -0
  79. package/src/runtime/session_replay.zig +7 -1
  80. package/src/runtime/session_tests.zig +35 -8
  81. package/src/runtime/tests.zig +2 -0
  82. package/src/runtime/ts_core_host.zig +1505 -0
  83. package/src/runtime/ts_core_host_tests.zig +1408 -0
  84. package/src/runtime/ts_ui_app.zig +469 -0
  85. package/src/runtime/ui_app.zig +58 -0
  86. package/src/runtime/view_canvas.zig +24 -17
  87. package/src/runtime/view_widget_tree.zig +2 -2
  88. package/src/runtime/web_layer_gate_tests.zig +242 -0
  89. package/src/runtime/window_storage.zig +5 -0
  90. package/src/runtime/window_view_runtime.zig +1 -0
  91. package/src/tooling/buildgraph.zig +7 -1
  92. package/src/tooling/codesign.zig +212 -105
  93. package/src/tooling/doctor.zig +78 -0
  94. package/src/tooling/manifest.zig +314 -1
  95. package/src/tooling/package.zig +722 -18
  96. package/src/tooling/raw_manifest.zig +16 -0
  97. package/src/tooling/root.zig +1 -0
  98. package/src/tooling/templates.zig +911 -88
  99. package/src/tooling/ts_core.zig +486 -0
  100. package/src/tooling/verbs.zig +22 -0
@@ -66,6 +66,13 @@ pub const PackageOptions = struct {
66
66
  assets_dir: []const u8 = "assets",
67
67
  frontend: ?manifest_tool.FrontendMetadata = null,
68
68
  web_engine: WebEngine = .system,
69
+ /// The `--web-layer` flag, when the caller passed one: beats app.zon's
70
+ /// `.webview_layer` the same way `-Dweb-layer` beats it in the build
71
+ /// graph. The standard build graphs always pass their RESOLVED layer
72
+ /// decision here (include|exclude), so a graph-driven package never
73
+ /// re-infers what the graph already compiled into the exe. Null means
74
+ /// the manifest field decides.
75
+ web_layer_setting: ?manifest_tool.WebViewLayerSetting = null,
69
76
  cef_dir: []const u8 = web_engine_tool.default_cef_dir,
70
77
  signing: SigningConfig = .{},
71
78
  archive: bool = false,
@@ -82,11 +89,38 @@ pub const PackageStats = struct {
82
89
  artifact_name: []const u8 = "",
83
90
  target: PackageTarget = .macos,
84
91
  signing_mode: SigningMode = .none,
92
+ /// True when a signing mode that claims to sign (adhoc or identity)
93
+ /// ran AND the result passed `codesign --verify --deep --strict`:
94
+ /// the proof behind the report's "signed, verified" line. A package
95
+ /// whose signing or verification fails never produces stats at all.
96
+ signing_verified: bool = false,
85
97
  asset_count: usize = 0,
86
98
  web_engine: WebEngine = .system,
99
+ web_layer: ?manifest_tool.WebLayer = null,
87
100
  archive_path: ?[]const u8 = null,
88
101
  };
89
102
 
103
+ /// The web-layer verdict for a package, from the same declare-to-use
104
+ /// inference the build graph runs — fed the RESOLVED engine
105
+ /// (`--web-engine` orelse app.zon, resolved by the CLI before packaging)
106
+ /// so a Chromium flag on a system manifest still ships the layer, and
107
+ /// the RESOLVED layer setting (`--web-layer` orelse app.zon) so a
108
+ /// flag-overridden exe is packaged under the decision it was built with.
109
+ /// Metadata that cannot be inferred (invalid or contradictory
110
+ /// `.webview_layer`) keeps the layer here — `createPackage` refuses
111
+ /// those loudly up front, and the direct artifact helpers must not
112
+ /// silently strip a layer on bad input.
113
+ fn webLayerFor(options: PackageOptions) manifest_tool.WebLayer {
114
+ return manifest_tool.webLayerResolved(options.metadata, options.web_engine, options.web_layer_setting) catch .{ .enabled = true, .reason = .declared_include };
115
+ }
116
+
117
+ /// The verdict line's engine half: what web layer this artifact ships.
118
+ fn webLayerEngineName(layer: manifest_tool.WebLayer, target: PackageTarget, web_engine: WebEngine) []const u8 {
119
+ if (!layer.enabled) return "none";
120
+ if (web_engine == .chromium) return "chromium";
121
+ return if (target == .windows) "webview2" else "system";
122
+ }
123
+
90
124
  pub fn artifactName(buffer: []u8, metadata: manifest_tool.Metadata, target: PackageTarget, optimize: []const u8) ![]const u8 {
91
125
  return std.fmt.bufPrint(buffer, "{s}-{s}-{s}-{s}{s}", .{
92
126
  metadata.name,
@@ -98,6 +132,16 @@ pub fn artifactName(buffer: []u8, metadata: manifest_tool.Metadata, target: Pack
98
132
  }
99
133
 
100
134
  pub fn createPackage(allocator: std.mem.Allocator, io: std.Io, options: PackageOptions) !PackageStats {
135
+ // The package boundary of the reject-conflicts contract: an exclude
136
+ // (from `--web-layer` or app.zon) against declared web content — or
137
+ // against a resolved Chromium engine — never becomes an artifact.
138
+ _ = manifest_tool.webLayerResolved(options.metadata, options.web_engine, options.web_layer_setting) catch |err| {
139
+ switch (err) {
140
+ error.WebViewLayerConflict => std.debug.print("error: {s}\n", .{if (options.web_layer_setting == .exclude) manifest_tool.web_layer_flag_conflict_message else manifest_tool.web_layer_conflict_message}),
141
+ error.InvalidWebViewLayer => std.debug.print("error: app.zon webview_layer is invalid - expected \"auto\", \"include\", or \"exclude\"\n", .{}),
142
+ }
143
+ return err;
144
+ };
101
145
  try validateWebEngineTarget(options.target, options.web_engine);
102
146
  var stats = switch (options.target) {
103
147
  .macos => try createMacosApp(allocator, io, options),
@@ -132,6 +176,12 @@ pub fn printDiagnostic(stats: PackageStats) void {
132
176
  .message = std.fmt.bufPrint(&message_buffer, "created {s} artifact at {s}", .{ @tagName(stats.target), stats.path }) catch "created package",
133
177
  }, &writer) catch return;
134
178
  std.debug.print("{s}\n", .{writer.buffered()});
179
+ if (stats.web_layer) |layer| {
180
+ std.debug.print(" web layer: {s} ({s})\n", .{ webLayerEngineName(layer, stats.target, stats.web_engine), layer.sourceText() });
181
+ }
182
+ if (stats.signing_verified) {
183
+ std.debug.print(" signing: {s} (signed, verified)\n", .{@tagName(stats.signing_mode)});
184
+ }
135
185
  if (stats.archive_path) |archive| {
136
186
  std.debug.print(" archive: {s}\n", .{archive});
137
187
  }
@@ -183,15 +233,17 @@ pub fn createMacosApp(allocator: std.mem.Allocator, io: std.Io, options: Package
183
233
  try cef.ensureLayout(io, options.cef_dir);
184
234
  try copyMacosCefRuntime(allocator, io, package_dir, options.cef_dir);
185
235
  }
186
- try runSigning(allocator, io, package_dir, options);
236
+ const signing_verified = try runSigning(allocator, io, package_dir, options);
187
237
 
188
238
  return .{
189
239
  .path = options.output_path,
190
240
  .artifact_name = std.fs.path.basename(options.output_path),
191
241
  .target = .macos,
192
242
  .signing_mode = options.signing.mode,
243
+ .signing_verified = signing_verified,
193
244
  .asset_count = bundle_stats.asset_count,
194
245
  .web_engine = options.web_engine,
246
+ .web_layer = webLayerFor(options),
195
247
  };
196
248
  }
197
249
 
@@ -209,14 +261,48 @@ fn createDesktopArtifact(allocator: std.mem.Allocator, io: std.Io, options: Pack
209
261
  try allocator.dupe(u8, options.metadata.name);
210
262
  defer allocator.free(executable_name);
211
263
 
264
+ // Native-only apps ship no WebView2 loader: their host was compiled
265
+ // without the embedded web layer, so the loader would be dead bytes
266
+ // pretending the app can spawn a webview.
267
+ const wants_webview2_loader = options.target == .windows and options.web_engine == .system and webLayerFor(options).enabled;
268
+ // The package-time half of the web-layer audit (the build-time half
269
+ // is tools/audit_web_layer.zig): a Windows exe that references
270
+ // WebView2Loader.dll was compiled WITH the embedded web layer, so
271
+ // packaging it loaderless ships an app whose every webview call
272
+ // fails at runtime. Refuse the mismatch and teach the fix — this
273
+ // catches exes built under an override the packaging decision never
274
+ // saw (a stale zig-out binary, a hand-supplied --binary).
275
+ if (options.target == .windows and !wants_webview2_loader) {
276
+ if (options.binary_path) |binary_path| {
277
+ if (try peReferencesWebView2Loader(allocator, io, binary_path)) {
278
+ std.debug.print("error: {s} references WebView2Loader.dll but this package ships no web layer, so its webviews would fail to spawn - the exe was built with the embedded web layer (for example `zig build -Dweb-layer=include`)\n" ++
279
+ " package with `--web-layer include`, or rebuild the binary to match the packaging decision\n", .{binary_path});
280
+ return error.WebViewLayerMismatch;
281
+ }
282
+ }
283
+ }
284
+ // The Linux twin of that guard (the build-time half is the ELF scan
285
+ // in tools/audit_web_layer.zig): an executable that links WebKitGTK
286
+ // was compiled WITH the embedded web layer, so packaging it under a
287
+ // native-only decision ships an app that still demands libwebkitgtk
288
+ // on every user machine while its webviews would fail to spawn.
289
+ if (options.target == .linux and options.web_engine == .system and !webLayerFor(options).enabled) {
290
+ if (options.binary_path) |binary_path| {
291
+ if (try elfReferencesWebKitGtk(allocator, io, binary_path)) {
292
+ std.debug.print("error: {s} references WebKitGTK but this package ships no web layer, so its webviews would fail to spawn - the binary was built with the embedded web layer (for example `zig build -Dweb-layer=include`)\n" ++
293
+ " package with `--web-layer include`, or rebuild the binary to match the packaging decision\n", .{binary_path});
294
+ return error.WebViewLayerMismatch;
295
+ }
296
+ }
297
+ }
212
298
  if (options.binary_path) |binary_path| {
213
299
  const binary_subpath = try std.fmt.allocPrint(allocator, "bin/{s}", .{executable_name});
214
300
  defer allocator.free(binary_subpath);
215
301
  try copyFileToDir(allocator, io, dir, binary_path, binary_subpath);
216
- if (options.target == .windows and options.web_engine == .system) {
302
+ if (wants_webview2_loader) {
217
303
  try copyWindowsWebView2Loader(allocator, io, dir, options, binary_path);
218
304
  }
219
- } else if (options.target == .windows and options.web_engine == .system) {
305
+ } else if (wants_webview2_loader) {
220
306
  try writeFile(dir, io, "bin/README.txt", "Build the app binary separately and place it here for this target, together with the WebView2Loader.dll for its architecture (vendored in the SDK under third_party/webview2/).\n");
221
307
  } else {
222
308
  try writeFile(dir, io, "bin/README.txt", "Build the app binary separately and place it here for this target.\n");
@@ -258,7 +344,7 @@ fn createDesktopArtifact(allocator: std.mem.Allocator, io: std.Io, options: Pack
258
344
  try copyDesktopCefRuntime(allocator, io, dir, options.target, options.cef_dir);
259
345
  }
260
346
  try writeReport(allocator, dir, io, "package-manifest.zon", options, executable_name, bundle_stats.asset_count);
261
- return .{ .path = options.output_path, .artifact_name = std.fs.path.basename(options.output_path), .target = options.target, .asset_count = bundle_stats.asset_count, .web_engine = options.web_engine };
347
+ return .{ .path = options.output_path, .artifact_name = std.fs.path.basename(options.output_path), .target = options.target, .asset_count = bundle_stats.asset_count, .web_engine = options.web_engine, .web_layer = webLayerFor(options) };
262
348
  }
263
349
 
264
350
  /// The iOS host tier: a COMPLETE Xcode project the user never edits —
@@ -330,7 +416,7 @@ fn createIosArtifact(allocator: std.mem.Allocator, io: std.Io, options: PackageO
330
416
  defer allocator.free(readme);
331
417
  try writeFile(dir, io, "README.md", readme);
332
418
  try writeReport(allocator, dir, io, "package-manifest.zon", options, "libnative-sdk.a", bundle_stats.asset_count);
333
- return .{ .path = options.output_path, .artifact_name = std.fs.path.basename(options.output_path), .target = .ios, .asset_count = bundle_stats.asset_count, .web_engine = options.web_engine };
419
+ return .{ .path = options.output_path, .artifact_name = std.fs.path.basename(options.output_path), .target = .ios, .asset_count = bundle_stats.asset_count, .web_engine = options.web_engine, .web_layer = webLayerFor(options) };
334
420
  }
335
421
 
336
422
  fn iosProjectReadme(allocator: std.mem.Allocator, metadata: manifest_tool.Metadata) ![]const u8 {
@@ -397,7 +483,7 @@ fn createAndroidArtifact(allocator: std.mem.Allocator, io: std.Io, options: Pack
397
483
  if (try assembleAndroidApk(allocator, io, options)) |apk_name| {
398
484
  artifact_name = apk_name;
399
485
  }
400
- return .{ .path = options.output_path, .artifact_name = artifact_name, .target = .android, .asset_count = bundle_stats.asset_count, .web_engine = options.web_engine };
486
+ return .{ .path = options.output_path, .artifact_name = artifact_name, .target = .android, .asset_count = bundle_stats.asset_count, .web_engine = options.web_engine, .web_layer = webLayerFor(options) };
401
487
  }
402
488
 
403
489
  /// Assemble the debug APK inside the generated project when the caller
@@ -961,6 +1047,104 @@ fn copyWindowsWebView2Loader(allocator: std.mem.Allocator, io: std.Io, dir: std.
961
1047
  try copyFileToDir(allocator, io, dir, loader_path, "bin/WebView2Loader.dll");
962
1048
  }
963
1049
 
1050
+ /// Whether a Windows executable carries the embedded web layer: the host
1051
+ /// loads WebView2Loader.dll through LoadLibraryW, so the honest evidence
1052
+ /// is the loader's name in the binary — stored as UTF-16 in a web build
1053
+ /// and compiled out entirely (with the whole layer) in a native-only
1054
+ /// build. The same probe as the build-time auditor
1055
+ /// (tools/audit_web_layer.zig); a non-PE file proves nothing about the
1056
+ /// layer, so it scans as false rather than refusing the package.
1057
+ fn peReferencesWebView2Loader(allocator: std.mem.Allocator, io: std.Io, path: []const u8) !bool {
1058
+ const bytes = try readPath(allocator, io, path);
1059
+ defer allocator.free(bytes);
1060
+ if (bytes.len < 0x40 or bytes[0] != 'M' or bytes[1] != 'Z') return false;
1061
+ const pe_offset: usize = std.mem.readInt(u32, bytes[0x3c..0x40], .little);
1062
+ if (pe_offset + 4 > bytes.len) return false;
1063
+ if (!std.mem.eql(u8, bytes[pe_offset..][0..4], "PE\x00\x00")) return false;
1064
+ const needle_ascii = "WebView2Loader.dll";
1065
+ var needle_wide: [needle_ascii.len * 2]u8 = undefined;
1066
+ for (needle_ascii, 0..) |ch, index| {
1067
+ needle_wide[index * 2] = ch;
1068
+ needle_wide[index * 2 + 1] = 0;
1069
+ }
1070
+ return std.mem.indexOf(u8, bytes, &needle_wide) != null or
1071
+ std.mem.indexOf(u8, bytes, needle_ascii) != null;
1072
+ }
1073
+
1074
+ /// Whether a Linux executable carries the embedded web layer: the GTK
1075
+ /// host links webkitgtk-6.0 directly, so the honest evidence is a
1076
+ /// libwebkitgtk/libjavascriptcoregtk DT_NEEDED entry or a webkit_*/jsc_*
1077
+ /// dynamic-symbol name — all removed by the WebKitGTK compile seam in a
1078
+ /// native-only build. The same probe as the build-time auditor
1079
+ /// (tools/audit_web_layer.zig), hand-rolled over the section headers;
1080
+ /// a non-ELF file (or one this minimal parse cannot walk) proves
1081
+ /// nothing about the layer, so it scans as false rather than refusing
1082
+ /// the package.
1083
+ fn elfReferencesWebKitGtk(allocator: std.mem.Allocator, io: std.Io, path: []const u8) !bool {
1084
+ const bytes = try readPath(allocator, io, path);
1085
+ defer allocator.free(bytes);
1086
+ return elfBytesReferenceWebKitGtk(bytes);
1087
+ }
1088
+
1089
+ fn elfBytesReferenceWebKitGtk(bytes: []const u8) bool {
1090
+ // ELF64 little endian only — every Linux target this toolkit builds.
1091
+ if (bytes.len < 0x40 or !std.mem.eql(u8, bytes[0..4], "\x7fELF")) return false;
1092
+ if (bytes[4] != 2 or bytes[5] != 1) return false;
1093
+ const sh_offset: u64 = std.mem.readInt(u64, bytes[0x28..0x30], .little);
1094
+ const sh_entsize: u16 = std.mem.readInt(u16, bytes[0x3a..0x3c], .little);
1095
+ const sh_count: u16 = std.mem.readInt(u16, bytes[0x3c..0x3e], .little);
1096
+ if (sh_offset == 0 or sh_count == 0 or sh_entsize < 0x40) return false;
1097
+
1098
+ var index: u16 = 0;
1099
+ while (index < sh_count) : (index += 1) {
1100
+ const header = elfSectionSlice(bytes, sh_offset + @as(u64, index) * sh_entsize, 0x40) orelse return false;
1101
+ const sh_type = std.mem.readInt(u32, header[0x04..0x08], .little);
1102
+ const link = std.mem.readInt(u32, header[0x28..0x2c], .little);
1103
+ const offset = std.mem.readInt(u64, header[0x18..0x20], .little);
1104
+ const size = std.mem.readInt(u64, header[0x20..0x28], .little);
1105
+ const entsize = std.mem.readInt(u64, header[0x38..0x40], .little);
1106
+ // SHT_DYNAMIC = 6, SHT_DYNSYM = 11.
1107
+ if (sh_type != 6 and sh_type != 11) continue;
1108
+ if (link >= sh_count) continue;
1109
+ const link_header = elfSectionSlice(bytes, sh_offset + @as(u64, link) * sh_entsize, 0x40) orelse return false;
1110
+ const strtab = elfSectionSlice(bytes, std.mem.readInt(u64, link_header[0x18..0x20], .little), std.mem.readInt(u64, link_header[0x20..0x28], .little)) orelse return false;
1111
+ const table = elfSectionSlice(bytes, offset, size) orelse return false;
1112
+ if (sh_type == 6) {
1113
+ var cursor: usize = 0;
1114
+ while (cursor + 16 <= table.len) : (cursor += 16) {
1115
+ const tag: i64 = @bitCast(std.mem.readInt(u64, table[cursor..][0..8], .little));
1116
+ if (tag != 1) continue; // DT_NEEDED
1117
+ const name = elfStringAt(strtab, std.mem.readInt(u64, table[cursor + 8 ..][0..8], .little)) orelse continue;
1118
+ if (std.mem.indexOf(u8, name, "webkitgtk") != null or std.mem.indexOf(u8, name, "javascriptcoregtk") != null) return true;
1119
+ }
1120
+ } else {
1121
+ const stride: usize = if (entsize >= 24) @intCast(entsize) else 24;
1122
+ var cursor: usize = 0;
1123
+ while (cursor + 24 <= table.len) : (cursor += stride) {
1124
+ const name_offset: u32 = std.mem.readInt(u32, table[cursor..][0..4], .little);
1125
+ if (name_offset == 0) continue;
1126
+ const name = elfStringAt(strtab, name_offset) orelse continue;
1127
+ if (std.mem.startsWith(u8, name, "webkit_") or std.mem.startsWith(u8, name, "jsc_")) return true;
1128
+ }
1129
+ }
1130
+ }
1131
+ return false;
1132
+ }
1133
+
1134
+ fn elfSectionSlice(bytes: []const u8, offset: u64, size: u64) ?[]const u8 {
1135
+ if (offset > bytes.len) return null;
1136
+ const start: usize = @intCast(offset);
1137
+ if (size > bytes.len - start) return null;
1138
+ return bytes[start .. start + @as(usize, @intCast(size))];
1139
+ }
1140
+
1141
+ fn elfStringAt(strtab: []const u8, offset: u64) ?[]const u8 {
1142
+ if (offset >= strtab.len) return null;
1143
+ const start: usize = @intCast(offset);
1144
+ const end = std.mem.indexOfScalarPos(u8, strtab, start, 0) orelse return null;
1145
+ return strtab[start..end];
1146
+ }
1147
+
964
1148
  /// Whether a PE executable targets arm64, read from the COFF machine
965
1149
  /// field. Anything unrecognized falls back to x64, the default Windows
966
1150
  /// build target.
@@ -1021,6 +1205,15 @@ fn writeReport(allocator: std.mem.Allocator, dir: std.Io.Dir, io: std.Io, subpat
1021
1205
  defer allocator.free(optimize);
1022
1206
  const web_engine = try zonStringAlloc(allocator, @tagName(options.web_engine));
1023
1207
  defer allocator.free(web_engine);
1208
+ // The web-layer verdict the artifact was staged under, in the same
1209
+ // "engine (source)" shape the package diagnostic prints — e.g.
1210
+ // "none (inferred: nothing in app.zon declares web use)" or
1211
+ // "webview2 (declared: capabilities)".
1212
+ const layer = webLayerFor(options);
1213
+ const web_layer_value = try std.fmt.allocPrint(allocator, "{s} ({s})", .{ webLayerEngineName(layer, options.target, options.web_engine), layer.sourceText() });
1214
+ defer allocator.free(web_layer_value);
1215
+ const web_layer = try zonStringAlloc(allocator, web_layer_value);
1216
+ defer allocator.free(web_layer);
1024
1217
  const signing = try zonStringAlloc(allocator, @tagName(options.signing.mode));
1025
1218
  defer allocator.free(signing);
1026
1219
  const report = try std.fmt.allocPrint(allocator,
@@ -1032,6 +1225,7 @@ fn writeReport(allocator: std.mem.Allocator, dir: std.Io.Dir, io: std.Io, subpat
1032
1225
  \\ .executable = {s},
1033
1226
  \\ .optimize = {s},
1034
1227
  \\ .web_engine = {s},
1228
+ \\ .web_layer = {s},
1035
1229
  \\ .signing = {s},
1036
1230
  \\ .asset_count = {d},
1037
1231
  \\{s}
@@ -1048,6 +1242,7 @@ fn writeReport(allocator: std.mem.Allocator, dir: std.Io.Dir, io: std.Io, subpat
1048
1242
  executable,
1049
1243
  optimize,
1050
1244
  web_engine,
1245
+ web_layer,
1051
1246
  signing,
1052
1247
  asset_count,
1053
1248
  frontend,
@@ -1173,33 +1368,65 @@ fn copyTree(allocator: std.mem.Allocator, io: std.Io, source_path: []const u8, d
1173
1368
  /// the signature on the bundle is the proof it happened — and only a
1174
1369
  /// FAILED signing rewrites it, which is safe because a failed codesign
1175
1370
  /// leaves the bundle without a seal to break.
1176
- fn runSigning(allocator: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, options: PackageOptions) !void {
1371
+ /// Returns true when the bundle was signed AND the signature verified —
1372
+ /// the value the report's "signed, verified" line states. A signing mode
1373
+ /// that claims to sign (adhoc or identity) either delivers a verified
1374
+ /// signature or fails the whole package with codesign's own reason:
1375
+ /// exiting 0 while shipping an unsigned bundle is the release-breaking
1376
+ /// failure this pipeline exists to prevent.
1377
+ fn runSigning(allocator: std.mem.Allocator, io: std.Io, dir: std.Io.Dir, options: PackageOptions) !bool {
1177
1378
  const plan_path = "Contents/Resources/signing-plan.txt";
1178
1379
  switch (options.signing.mode) {
1179
- .none => try writeFile(dir, io, plan_path, "signing=none\nunsigned local package\n"),
1380
+ .none => {
1381
+ try writeFile(dir, io, plan_path, "signing=none\nunsigned local package\n");
1382
+ return false;
1383
+ },
1180
1384
  .adhoc => {
1181
1385
  try writeFile(dir, io, plan_path, "signing=adhoc\nad-hoc signed\n");
1182
- const result = codesign.signAdHoc(io, options.output_path) catch {
1386
+ const result = try codesign.signAdHoc(allocator, io, options.output_path);
1387
+ defer allocator.free(result.message);
1388
+ if (!result.ok) {
1183
1389
  try writeFile(dir, io, plan_path, "signing=adhoc\ncodesign --sign - failed; bundle is unsigned\n");
1184
- return;
1185
- };
1186
- if (!result.ok) try writeFile(dir, io, plan_path, "signing=adhoc\ncodesign --sign - failed; bundle is unsigned\n");
1390
+ std.debug.print("error: ad-hoc code signing failed for {s}, so this package would ship unsigned - codesign said:\n{s}\n fix what codesign reports above, or package with `--signing none` if an unsigned bundle is what you want\n", .{ options.output_path, trimmedToolOutput(result.message) });
1391
+ return error.SigningFailed;
1392
+ }
1187
1393
  },
1188
1394
  .identity => {
1189
1395
  const identity = options.signing.identity orelse {
1190
1396
  try writeFile(dir, io, plan_path, "signing=identity\nno identity provided; bundle is unsigned\n");
1191
- return;
1397
+ std.debug.print("error: --signing identity needs the identity to sign with, so this package would ship unsigned - pass --identity \"Developer ID Application: Your Name (TEAMID)\"\n `security find-identity -v -p codesigning` lists the identities this machine can sign with\n", .{});
1398
+ return error.SigningFailed;
1192
1399
  };
1193
1400
  const plan_text = try std.fmt.allocPrint(allocator, "signing=identity\nsigned with {s}\n", .{identity});
1194
1401
  defer allocator.free(plan_text);
1195
1402
  try writeFile(dir, io, plan_path, plan_text);
1196
- const result = codesign.signIdentity(io, options.output_path, identity, options.signing.entitlements) catch {
1403
+ const result = try codesign.signIdentity(allocator, io, options.output_path, identity, options.signing.entitlements);
1404
+ defer allocator.free(result.message);
1405
+ if (!result.ok) {
1197
1406
  try writeFile(dir, io, plan_path, "signing=identity\ncodesign failed; bundle is unsigned\n");
1198
- return;
1199
- };
1200
- if (!result.ok) try writeFile(dir, io, plan_path, "signing=identity\ncodesign failed; bundle is unsigned\n");
1407
+ std.debug.print("error: code signing with \"{s}\" failed for {s}, so this package would ship unsigned - codesign said:\n{s}\n `security find-identity -v -p codesigning` lists the identities this machine can sign with\n", .{ identity, options.output_path, trimmedToolOutput(result.message) });
1408
+ return error.SigningFailed;
1409
+ }
1201
1410
  },
1202
1411
  }
1412
+ // The signature just applied must actually hold — the same strict
1413
+ // deep check an Apple silicon launch effectively runs. A bundle that
1414
+ // signs but does not verify (a stale seal, unsigned nested code) is
1415
+ // a packaging failure, not a report footnote.
1416
+ const verified = try codesign.verify(allocator, io, options.output_path);
1417
+ defer allocator.free(verified.message);
1418
+ if (!verified.ok) {
1419
+ std.debug.print("error: {s} was signed but failed `codesign --verify --deep --strict`, so it would be rejected at launch - codesign said:\n{s}\n fix what codesign reports above and rerun `native package`\n", .{ options.output_path, trimmedToolOutput(verified.message) });
1420
+ return error.SignatureVerificationFailed;
1421
+ }
1422
+ return true;
1423
+ }
1424
+
1425
+ /// codesign's output, trimmed of trailing newlines so the teaching
1426
+ /// message's fix line lands directly under it (the output itself stays
1427
+ /// verbatim).
1428
+ fn trimmedToolOutput(output: []const u8) []const u8 {
1429
+ return std.mem.trimEnd(u8, output, "\r\n");
1203
1430
  }
1204
1431
 
1205
1432
  fn hasRegistrationMetadata(metadata: manifest_tool.Metadata) bool {
@@ -1715,7 +1942,6 @@ test "mobile package templates ship the toolkit hosts" {
1715
1942
  try std.testing.expect(std.mem.indexOf(u8, android_bridge, "WINDOW_FORMAT_RGBA_8888") != null);
1716
1943
  }
1717
1944
 
1718
-
1719
1945
  test "mobile package artifacts use manifest identity metadata" {
1720
1946
  var cwd = std.Io.Dir.cwd();
1721
1947
  try cwd.deleteTree(std.testing.io, ".zig-cache/test-package-mobile-identity");
@@ -2152,6 +2378,484 @@ test "package report records target signing and assets" {
2152
2378
  try std.testing.expect(std.mem.indexOf(u8, buffer[0..len], ".asset_count = 2") != null);
2153
2379
  }
2154
2380
 
2381
+ test "adhoc packaging into a spaced output path signs and verifies" {
2382
+ // The reported release-breaker: `--output "<path with spaces>.app"`
2383
+ // packaged, exited 0, and shipped an UNSIGNED bundle because the
2384
+ // codesign command was a shell string the spaces split apart. The
2385
+ // pipeline now execs argv arrays, so the spaced path must sign — and
2386
+ // the strict deep verify (real codesign, darwin hosts only) is the
2387
+ // proof.
2388
+ if (@import("builtin").os.tag != .macos) return error.SkipZigTest;
2389
+ const gpa = std.testing.allocator;
2390
+ var cwd = std.Io.Dir.cwd();
2391
+ const root = ".zig-cache/test-package signed spaced";
2392
+ try cwd.deleteTree(std.testing.io, root);
2393
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2394
+ try cwd.createDirPath(std.testing.io, root ++ "/assets");
2395
+
2396
+ const metadata: manifest_tool.Metadata = .{ .id = "dev.example.spaced-sign", .name = "spaced-demo", .version = "1.0.0" };
2397
+ const app_path = root ++ "/My Spaced Demo.app";
2398
+ const stats = try createMacosApp(gpa, std.testing.io, .{
2399
+ .metadata = metadata,
2400
+ .output_path = app_path,
2401
+ // A real Mach-O executable, so codesign has honest code to sign.
2402
+ .binary_path = "/bin/ls",
2403
+ .assets_dir = root ++ "/assets",
2404
+ .signing = .{ .mode = .adhoc },
2405
+ });
2406
+ try std.testing.expectEqual(SigningMode.adhoc, stats.signing_mode);
2407
+ try std.testing.expect(stats.signing_verified);
2408
+
2409
+ // Pin the claim with an independent codesign run, not just the flag.
2410
+ const verified = try codesign.verify(gpa, std.testing.io, app_path);
2411
+ defer gpa.free(verified.message);
2412
+ try std.testing.expect(verified.ok);
2413
+ }
2414
+
2415
+ test "a codesign failure fails packaging instead of shipping unsigned" {
2416
+ // An identity codesign cannot resolve exits nonzero; packaging must
2417
+ // surface that as an error (with codesign's reason printed), never
2418
+ // as a created artifact. Real codesign run: darwin hosts only.
2419
+ if (@import("builtin").os.tag != .macos) return error.SkipZigTest;
2420
+ var cwd = std.Io.Dir.cwd();
2421
+ const root = ".zig-cache/test-package-signing-failure";
2422
+ try cwd.deleteTree(std.testing.io, root);
2423
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2424
+ try cwd.createDirPath(std.testing.io, root ++ "/assets");
2425
+
2426
+ const metadata: manifest_tool.Metadata = .{ .id = "dev.example.sign-fail", .name = "sign-fail-demo", .version = "1.0.0" };
2427
+ try std.testing.expectError(error.SigningFailed, createMacosApp(std.testing.allocator, std.testing.io, .{
2428
+ .metadata = metadata,
2429
+ .output_path = root ++ "/Sign Fail Demo.app",
2430
+ .binary_path = "/bin/ls",
2431
+ .assets_dir = root ++ "/assets",
2432
+ .signing = .{ .mode = .identity, .identity = "native-sdk-no-such-identity" },
2433
+ }));
2434
+ }
2435
+
2436
+ test "identity signing without an identity is a loud failure, not a silent unsigned bundle" {
2437
+ var cwd = std.Io.Dir.cwd();
2438
+ const root = ".zig-cache/test-package-signing-no-identity";
2439
+ try cwd.deleteTree(std.testing.io, root);
2440
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2441
+ try cwd.createDirPath(std.testing.io, root ++ "/assets");
2442
+
2443
+ const metadata: manifest_tool.Metadata = .{ .id = "dev.example.no-identity", .name = "no-identity-demo", .version = "1.0.0" };
2444
+ try std.testing.expectError(error.SigningFailed, createMacosApp(std.testing.allocator, std.testing.io, .{
2445
+ .metadata = metadata,
2446
+ .output_path = root ++ "/NoIdentity.app",
2447
+ .assets_dir = root ++ "/assets",
2448
+ .signing = .{ .mode = .identity },
2449
+ }));
2450
+ }
2451
+
2452
+ test "native-only windows package ships no WebView2 loader and reports web layer none" {
2453
+ var cwd = std.Io.Dir.cwd();
2454
+ const root = ".zig-cache/test-package-native-only-windows";
2455
+ try cwd.deleteTree(std.testing.io, root);
2456
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2457
+ try cwd.createDirPath(std.testing.io, root ++ "/assets");
2458
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/app-binary", .data = "not a real exe" });
2459
+
2460
+ // A canvas app shape: gpu capabilities, no frontend, no webview.
2461
+ const capabilities = [_][]const u8{ "native_views", "gpu_surfaces" };
2462
+ const metadata: manifest_tool.Metadata = .{
2463
+ .id = "dev.example.canvas",
2464
+ .name = "canvas-demo",
2465
+ .version = "1.0.0",
2466
+ .capabilities = &capabilities,
2467
+ };
2468
+
2469
+ const stats = try createPackage(std.testing.allocator, std.testing.io, .{
2470
+ .metadata = metadata,
2471
+ .target = .windows,
2472
+ .output_path = root ++ "/demo-windows",
2473
+ .binary_path = root ++ "/app-binary",
2474
+ .assets_dir = root ++ "/assets",
2475
+ });
2476
+ try std.testing.expect(!stats.web_layer.?.enabled);
2477
+
2478
+ // No loader staged next to the binary.
2479
+ var dir = try cwd.openDir(std.testing.io, root ++ "/demo-windows", .{});
2480
+ defer dir.close(std.testing.io);
2481
+ try std.testing.expectError(error.FileNotFound, dir.openFile(std.testing.io, "bin/WebView2Loader.dll", .{}));
2482
+
2483
+ // No frontend dist directory (native-only packages bundle plain assets).
2484
+ try std.testing.expectError(error.FileNotFound, dir.openDir(std.testing.io, "resources/dist", .{}));
2485
+
2486
+ // The report carries the verdict.
2487
+ const report = try readPath(std.testing.allocator, std.testing.io, root ++ "/demo-windows/package-manifest.zon");
2488
+ defer std.testing.allocator.free(report);
2489
+ try std.testing.expect(std.mem.indexOf(u8, report, ".web_layer = \"none (inferred: nothing in app.zon declares web use)\"") != null);
2490
+ }
2491
+
2492
+ test "webview-declaring package reports the web layer as declared" {
2493
+ var cwd = std.Io.Dir.cwd();
2494
+ const root = ".zig-cache/test-package-web-layer-declared";
2495
+ try cwd.deleteTree(std.testing.io, root);
2496
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2497
+ try cwd.createDirPath(std.testing.io, root ++ "/assets");
2498
+
2499
+ const capabilities = [_][]const u8{ "webview", "js_bridge" };
2500
+ const metadata: manifest_tool.Metadata = .{
2501
+ .id = "dev.example.web",
2502
+ .name = "web-demo",
2503
+ .version = "1.0.0",
2504
+ .capabilities = &capabilities,
2505
+ };
2506
+
2507
+ // No binary: the loader copy is not exercised (it needs a framework
2508
+ // root), but the report and stats still carry the declared verdict.
2509
+ const stats = try createPackage(std.testing.allocator, std.testing.io, .{
2510
+ .metadata = metadata,
2511
+ .target = .windows,
2512
+ .output_path = root ++ "/demo-windows",
2513
+ .assets_dir = root ++ "/assets",
2514
+ });
2515
+ try std.testing.expect(stats.web_layer.?.enabled);
2516
+ try std.testing.expectEqual(manifest_tool.WebLayerReason.capability, stats.web_layer.?.reason);
2517
+
2518
+ const report = try readPath(std.testing.allocator, std.testing.io, root ++ "/demo-windows/package-manifest.zon");
2519
+ defer std.testing.allocator.free(report);
2520
+ try std.testing.expect(std.mem.indexOf(u8, report, ".web_layer = \"webview2 (declared: capabilities)\"") != null);
2521
+ }
2522
+
2523
+ test "package refuses a manifest that excludes the web layer while declaring web content" {
2524
+ const capabilities = [_][]const u8{"webview"};
2525
+ const metadata: manifest_tool.Metadata = .{
2526
+ .id = "dev.example.conflict",
2527
+ .name = "conflict-demo",
2528
+ .version = "1.0.0",
2529
+ .capabilities = &capabilities,
2530
+ .webview_layer = "exclude",
2531
+ };
2532
+ try std.testing.expectError(error.WebViewLayerConflict, createPackage(std.testing.allocator, std.testing.io, .{
2533
+ .metadata = metadata,
2534
+ .target = .windows,
2535
+ .output_path = ".zig-cache/test-package-web-layer-conflict",
2536
+ }));
2537
+ }
2538
+
2539
+ test "package web layer follows the resolved engine, not the raw manifest" {
2540
+ var cwd = std.Io.Dir.cwd();
2541
+ const root = ".zig-cache/test-package-web-layer-resolved-engine";
2542
+ try cwd.deleteTree(std.testing.io, root);
2543
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2544
+ try cwd.createDirPath(std.testing.io, root ++ "/assets");
2545
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/assets/index.html", .data = "<h1>Web</h1>" });
2546
+
2547
+ // A system manifest with no web declarations, packaged with the
2548
+ // engine the CLI resolved from `--web-engine chromium`: the layer
2549
+ // ships, and the verdict names the engine as the cause.
2550
+ const metadata: manifest_tool.Metadata = .{
2551
+ .id = "dev.example.resolved",
2552
+ .name = "resolved-demo",
2553
+ .version = "1.0.0",
2554
+ };
2555
+ const stats = try createPackage(std.testing.allocator, std.testing.io, .{
2556
+ .metadata = metadata,
2557
+ .target = .ios,
2558
+ .output_path = root ++ "/ios",
2559
+ .assets_dir = root ++ "/assets",
2560
+ .web_engine = .chromium,
2561
+ });
2562
+ try std.testing.expect(stats.web_layer.?.enabled);
2563
+ try std.testing.expectEqual(manifest_tool.WebLayerReason.chromium_engine, stats.web_layer.?.reason);
2564
+ }
2565
+
2566
+ test "package refuses an exclude against a resolved Chromium engine" {
2567
+ // `.webview_layer = "exclude"` with `--web-engine chromium` is the
2568
+ // same contradiction as exclude + a manifest web declaration: the
2569
+ // package boundary rejects it exactly like build configure does.
2570
+ const metadata: manifest_tool.Metadata = .{
2571
+ .id = "dev.example.exclude-chromium",
2572
+ .name = "exclude-chromium-demo",
2573
+ .version = "1.0.0",
2574
+ .webview_layer = "exclude",
2575
+ };
2576
+ try std.testing.expectError(error.WebViewLayerConflict, createPackage(std.testing.allocator, std.testing.io, .{
2577
+ .metadata = metadata,
2578
+ .target = .macos,
2579
+ .output_path = ".zig-cache/test-package-web-layer-exclude-chromium",
2580
+ .web_engine = .chromium,
2581
+ }));
2582
+ }
2583
+
2584
+ /// A minimal Windows executable for web-layer packaging tests: a valid
2585
+ /// PE header (so the loader-reference scan and the arch sniff both treat
2586
+ /// it as a real exe) followed by the UTF-16 loader name a web build
2587
+ /// carries. The COFF machine field stays zero, which the arch sniff
2588
+ /// reads as x64.
2589
+ fn testWebLayerPeBytes() [0x80 + "WebView2Loader.dll".len * 2]u8 {
2590
+ var bytes = [_]u8{0} ** (0x80 + "WebView2Loader.dll".len * 2);
2591
+ bytes[0] = 'M';
2592
+ bytes[1] = 'Z';
2593
+ std.mem.writeInt(u32, bytes[0x3c..0x40], 0x40, .little);
2594
+ bytes[0x40] = 'P';
2595
+ bytes[0x41] = 'E';
2596
+ for ("WebView2Loader.dll", 0..) |ch, index| {
2597
+ bytes[0x80 + index * 2] = ch;
2598
+ }
2599
+ return bytes;
2600
+ }
2601
+
2602
+ test "package --web-layer include on a canvas manifest stages the loader and names the flag" {
2603
+ var cwd = std.Io.Dir.cwd();
2604
+ const root = ".zig-cache/test-package-web-layer-include-flag";
2605
+ try cwd.deleteTree(std.testing.io, root);
2606
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2607
+ try cwd.createDirPath(std.testing.io, root ++ "/assets");
2608
+ // The exe a `zig build package -Dweb-layer=include` graph produces on
2609
+ // a canvas manifest: a real PE that references the loader.
2610
+ const exe_bytes = testWebLayerPeBytes();
2611
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/app.exe", .data = &exe_bytes });
2612
+
2613
+ const capabilities = [_][]const u8{ "native_views", "gpu_surfaces" };
2614
+ const metadata: manifest_tool.Metadata = .{
2615
+ .id = "dev.example.canvas",
2616
+ .name = "canvas-demo",
2617
+ .version = "1.0.0",
2618
+ .capabilities = &capabilities,
2619
+ };
2620
+
2621
+ // The loader copy resolves the framework root from the environment.
2622
+ var env_map = std.process.Environ.Map.init(std.testing.allocator);
2623
+ defer env_map.deinit();
2624
+ try env_map.put("NATIVE_SDK_PATH", ".");
2625
+
2626
+ const stats = try createPackage(std.testing.allocator, std.testing.io, .{
2627
+ .metadata = metadata,
2628
+ .target = .windows,
2629
+ .output_path = root ++ "/demo-windows",
2630
+ .binary_path = root ++ "/app.exe",
2631
+ .assets_dir = root ++ "/assets",
2632
+ .web_layer_setting = .include,
2633
+ .env_map = &env_map,
2634
+ });
2635
+ try std.testing.expect(stats.web_layer.?.enabled);
2636
+ try std.testing.expectEqual(manifest_tool.WebLayerReason.declared_include, stats.web_layer.?.reason);
2637
+
2638
+ // The loader is staged next to the binary, exactly like a manifest
2639
+ // that declares web use.
2640
+ var dir = try cwd.openDir(std.testing.io, root ++ "/demo-windows", .{});
2641
+ defer dir.close(std.testing.io);
2642
+ var loader = try dir.openFile(std.testing.io, "bin/WebView2Loader.dll", .{});
2643
+ loader.close(std.testing.io);
2644
+
2645
+ // The report names the flag as the cause, not app.zon.
2646
+ const report = try readPath(std.testing.allocator, std.testing.io, root ++ "/demo-windows/package-manifest.zon");
2647
+ defer std.testing.allocator.free(report);
2648
+ try std.testing.expect(std.mem.indexOf(u8, report, ".web_layer = \"webview2 (declared: --web-layer include)\"") != null);
2649
+ }
2650
+
2651
+ test "package --web-layer exclude on a web manifest is the same refused conflict" {
2652
+ const capabilities = [_][]const u8{"webview"};
2653
+ const metadata: manifest_tool.Metadata = .{
2654
+ .id = "dev.example.flag-conflict",
2655
+ .name = "flag-conflict-demo",
2656
+ .version = "1.0.0",
2657
+ .capabilities = &capabilities,
2658
+ };
2659
+ try std.testing.expectError(error.WebViewLayerConflict, createPackage(std.testing.allocator, std.testing.io, .{
2660
+ .metadata = metadata,
2661
+ .target = .windows,
2662
+ .output_path = ".zig-cache/test-package-web-layer-flag-conflict",
2663
+ .web_layer_setting = .exclude,
2664
+ }));
2665
+ }
2666
+
2667
+ test "package forwarded include on a web manifest keeps the manifest's reason" {
2668
+ var cwd = std.Io.Dir.cwd();
2669
+ const root = ".zig-cache/test-package-web-layer-forwarded-include";
2670
+ try cwd.deleteTree(std.testing.io, root);
2671
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2672
+ try cwd.createDirPath(std.testing.io, root ++ "/assets");
2673
+
2674
+ // The build graphs forward `--web-layer include` for every web app;
2675
+ // the verdict must keep reporting the manifest's own declaration, so
2676
+ // graph-driven and hand-run packages read identically.
2677
+ const capabilities = [_][]const u8{"webview"};
2678
+ const metadata: manifest_tool.Metadata = .{
2679
+ .id = "dev.example.forwarded",
2680
+ .name = "forwarded-demo",
2681
+ .version = "1.0.0",
2682
+ .capabilities = &capabilities,
2683
+ };
2684
+ const stats = try createPackage(std.testing.allocator, std.testing.io, .{
2685
+ .metadata = metadata,
2686
+ .target = .windows,
2687
+ .output_path = root ++ "/demo-windows",
2688
+ .assets_dir = root ++ "/assets",
2689
+ .web_layer_setting = .include,
2690
+ });
2691
+ try std.testing.expect(stats.web_layer.?.enabled);
2692
+ try std.testing.expectEqual(manifest_tool.WebLayerReason.capability, stats.web_layer.?.reason);
2693
+ }
2694
+
2695
+ test "package refuses to strip the loader from an exe that references it" {
2696
+ var cwd = std.Io.Dir.cwd();
2697
+ const root = ".zig-cache/test-package-web-layer-mismatch";
2698
+ try cwd.deleteTree(std.testing.io, root);
2699
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2700
+ try cwd.createDirPath(std.testing.io, root ++ "/assets");
2701
+ const exe_bytes = testWebLayerPeBytes();
2702
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/app.exe", .data = &exe_bytes });
2703
+
2704
+ // A loader-referencing exe (built with the web layer) packaged under
2705
+ // a native-only decision would ship broken webviews: refused, with
2706
+ // `--web-layer include` as the way out. Non-PE payloads (the other
2707
+ // tests' fake binaries) prove nothing and stay packageable.
2708
+ const capabilities = [_][]const u8{ "native_views", "gpu_surfaces" };
2709
+ const metadata: manifest_tool.Metadata = .{
2710
+ .id = "dev.example.mismatch",
2711
+ .name = "mismatch-demo",
2712
+ .version = "1.0.0",
2713
+ .capabilities = &capabilities,
2714
+ };
2715
+ try std.testing.expectError(error.WebViewLayerMismatch, createPackage(std.testing.allocator, std.testing.io, .{
2716
+ .metadata = metadata,
2717
+ .target = .windows,
2718
+ .output_path = root ++ "/demo-windows",
2719
+ .binary_path = root ++ "/app.exe",
2720
+ .assets_dir = root ++ "/assets",
2721
+ }));
2722
+ }
2723
+
2724
+ /// A minimal but structurally valid ELF64 executable for the WebKitGTK
2725
+ /// scan: one .dynstr, one .dynamic with a single DT_NEEDED naming
2726
+ /// `needed_lib`, and one .dynsym whose single real symbol is named
2727
+ /// `symbol_name` — the two evidence channels the Linux web-layer guard
2728
+ /// reads. Caller frees.
2729
+ fn testWebLayerElfBytes(gpa: std.mem.Allocator, needed_lib: []const u8, symbol_name: []const u8) ![]u8 {
2730
+ const dynstr_offset: u64 = 0x100;
2731
+ const dynamic_offset: u64 = 0x180;
2732
+ const dynsym_offset: u64 = 0x1c0;
2733
+ const shdr_offset: u64 = 0x200;
2734
+ const bytes = try gpa.alloc(u8, 0x2c0);
2735
+ @memset(bytes, 0);
2736
+
2737
+ // ELF header: magic, ELFCLASS64, little endian, section table.
2738
+ @memcpy(bytes[0..4], "\x7fELF");
2739
+ bytes[4] = 2;
2740
+ bytes[5] = 1;
2741
+ std.mem.writeInt(u64, bytes[0x28..0x30], shdr_offset, .little);
2742
+ std.mem.writeInt(u16, bytes[0x3a..0x3c], 0x40, .little);
2743
+ std.mem.writeInt(u16, bytes[0x3c..0x3e], 3, .little);
2744
+
2745
+ // .dynstr: "\0<needed_lib>\0<symbol_name>\0".
2746
+ const lib_name_offset: u64 = 1;
2747
+ const symbol_name_offset: u64 = 1 + needed_lib.len + 1;
2748
+ const dynstr_len: u64 = symbol_name_offset + symbol_name.len + 1;
2749
+ @memcpy(bytes[@intCast(dynstr_offset + lib_name_offset)..][0..needed_lib.len], needed_lib);
2750
+ @memcpy(bytes[@intCast(dynstr_offset + symbol_name_offset)..][0..symbol_name.len], symbol_name);
2751
+
2752
+ // .dynamic: DT_NEEDED -> needed_lib, then DT_NULL.
2753
+ std.mem.writeInt(u64, bytes[@intCast(dynamic_offset)..][0..8], 1, .little); // DT_NEEDED
2754
+ std.mem.writeInt(u64, bytes[@intCast(dynamic_offset + 8)..][0..8], lib_name_offset, .little);
2755
+
2756
+ // .dynsym: the null symbol, then one named symbol.
2757
+ std.mem.writeInt(u32, bytes[@intCast(dynsym_offset + 24)..][0..4], @intCast(symbol_name_offset), .little);
2758
+
2759
+ // Section headers: [0] .dynstr (SHT_STRTAB), [1] .dynamic, [2] .dynsym.
2760
+ const shdr = struct {
2761
+ fn write(buffer: []u8, base: u64, index: u64, sh_type: u32, link: u32, offset: u64, size: u64, entsize: u64) void {
2762
+ const header = buffer[@intCast(base + index * 0x40)..][0..0x40];
2763
+ std.mem.writeInt(u32, header[0x04..0x08], sh_type, .little);
2764
+ std.mem.writeInt(u64, header[0x18..0x20], offset, .little);
2765
+ std.mem.writeInt(u64, header[0x20..0x28], size, .little);
2766
+ std.mem.writeInt(u32, header[0x28..0x2c], link, .little);
2767
+ std.mem.writeInt(u64, header[0x38..0x40], entsize, .little);
2768
+ }
2769
+ };
2770
+ shdr.write(bytes, shdr_offset, 0, 3, 0, dynstr_offset, dynstr_len, 0);
2771
+ shdr.write(bytes, shdr_offset, 1, 6, 0, dynamic_offset, 32, 16);
2772
+ shdr.write(bytes, shdr_offset, 2, 11, 0, dynsym_offset, 48, 24);
2773
+ return bytes;
2774
+ }
2775
+
2776
+ test "the linux web-layer scan reads DT_NEEDED and dynamic symbols" {
2777
+ const gpa = std.testing.allocator;
2778
+ // The library link is evidence on its own.
2779
+ const linked = try testWebLayerElfBytes(gpa, "libwebkitgtk-6.0.so.4", "gtk_init");
2780
+ defer gpa.free(linked);
2781
+ try std.testing.expect(elfBytesReferenceWebKitGtk(linked));
2782
+ // So is a lone webkit_/jsc_ dynamic symbol (a hand-linked binary
2783
+ // that dodged the DT_NEEDED entry still calls into WebKit).
2784
+ const symboled = try testWebLayerElfBytes(gpa, "libgtk-4.so.1", "webkit_web_view_new");
2785
+ defer gpa.free(symboled);
2786
+ try std.testing.expect(elfBytesReferenceWebKitGtk(symboled));
2787
+ const jsc = try testWebLayerElfBytes(gpa, "libgtk-4.so.1", "jsc_value_to_string");
2788
+ defer gpa.free(jsc);
2789
+ try std.testing.expect(elfBytesReferenceWebKitGtk(jsc));
2790
+ // A WebKit-free GTK binary scans clean, and a non-ELF payload
2791
+ // proves nothing (stays packageable), mirroring the PE probe.
2792
+ const clean = try testWebLayerElfBytes(gpa, "libgtk-4.so.1", "gtk_init");
2793
+ defer gpa.free(clean);
2794
+ try std.testing.expect(!elfBytesReferenceWebKitGtk(clean));
2795
+ try std.testing.expect(!elfBytesReferenceWebKitGtk("not an executable"));
2796
+ }
2797
+
2798
+ test "package refuses to strip WebKitGTK from a Linux binary that links it" {
2799
+ var cwd = std.Io.Dir.cwd();
2800
+ const root = ".zig-cache/test-package-web-layer-linux-mismatch";
2801
+ try cwd.deleteTree(std.testing.io, root);
2802
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2803
+ try cwd.createDirPath(std.testing.io, root ++ "/assets");
2804
+ const elf_bytes = try testWebLayerElfBytes(std.testing.allocator, "libwebkitgtk-6.0.so.4", "webkit_web_view_new");
2805
+ defer std.testing.allocator.free(elf_bytes);
2806
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/app", .data = elf_bytes });
2807
+
2808
+ // A WebKitGTK-linking binary (built with the web layer) packaged
2809
+ // under a native-only decision would ship broken webviews AND a
2810
+ // libwebkitgtk runtime requirement the package claims not to have:
2811
+ // refused, with `--web-layer include` as the way out.
2812
+ const capabilities = [_][]const u8{ "native_views", "gpu_surfaces" };
2813
+ const metadata: manifest_tool.Metadata = .{
2814
+ .id = "dev.example.linux-mismatch",
2815
+ .name = "linux-mismatch-demo",
2816
+ .version = "1.0.0",
2817
+ .capabilities = &capabilities,
2818
+ };
2819
+ try std.testing.expectError(error.WebViewLayerMismatch, createPackage(std.testing.allocator, std.testing.io, .{
2820
+ .metadata = metadata,
2821
+ .target = .linux,
2822
+ .output_path = root ++ "/demo-linux",
2823
+ .binary_path = root ++ "/app",
2824
+ .assets_dir = root ++ "/assets",
2825
+ }));
2826
+ }
2827
+
2828
+ test "native-only linux package accepts a WebKit-free ELF and reports web layer none" {
2829
+ var cwd = std.Io.Dir.cwd();
2830
+ const root = ".zig-cache/test-package-web-layer-linux-clean";
2831
+ try cwd.deleteTree(std.testing.io, root);
2832
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2833
+ try cwd.createDirPath(std.testing.io, root ++ "/assets");
2834
+ const elf_bytes = try testWebLayerElfBytes(std.testing.allocator, "libgtk-4.so.1", "gtk_init");
2835
+ defer std.testing.allocator.free(elf_bytes);
2836
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/app", .data = elf_bytes });
2837
+
2838
+ const capabilities = [_][]const u8{ "native_views", "gpu_surfaces" };
2839
+ const metadata: manifest_tool.Metadata = .{
2840
+ .id = "dev.example.linux-clean",
2841
+ .name = "linux-clean-demo",
2842
+ .version = "1.0.0",
2843
+ .capabilities = &capabilities,
2844
+ };
2845
+ const stats = try createPackage(std.testing.allocator, std.testing.io, .{
2846
+ .metadata = metadata,
2847
+ .target = .linux,
2848
+ .output_path = root ++ "/demo-linux",
2849
+ .binary_path = root ++ "/app",
2850
+ .assets_dir = root ++ "/assets",
2851
+ });
2852
+ try std.testing.expect(!stats.web_layer.?.enabled);
2853
+
2854
+ const report = try readPath(std.testing.allocator, std.testing.io, root ++ "/demo-linux/package-manifest.zon");
2855
+ defer std.testing.allocator.free(report);
2856
+ try std.testing.expect(std.mem.indexOf(u8, report, ".web_layer = \"none (inferred: nothing in app.zon declares web use)\"") != null);
2857
+ }
2858
+
2155
2859
  // ---------------------------------------------------------------------------
2156
2860
  // App icon pipeline tests
2157
2861
  // ---------------------------------------------------------------------------