@native-sdk/cli 0.5.0 → 0.5.1

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.
@@ -84,6 +84,15 @@ pub const PackageOptions = struct {
84
84
  env_map: ?*std.process.Environ.Map = null,
85
85
  };
86
86
 
87
+ /// The Windows subsystem verdict the packaging posture check reaches:
88
+ /// `gui` and `console` only when the PE optional header actually said
89
+ /// so, `unknown` when the probe proved nothing — non-PE or truncated
90
+ /// bytes, a headerless image, an `e_lfanew` past the header ceiling, a
91
+ /// benign read error, or some other declared subsystem entirely
92
+ /// (native, EFI, ...). No surface may claim a subsystem the parse
93
+ /// never established.
94
+ pub const WindowsSubsystem = enum { gui, console, unknown };
95
+
87
96
  pub const PackageStats = struct {
88
97
  path: []const u8,
89
98
  artifact_name: []const u8 = "",
@@ -98,6 +107,16 @@ pub const PackageStats = struct {
98
107
  web_engine: WebEngine = .system,
99
108
  web_layer: ?manifest_tool.WebLayer = null,
100
109
  archive_path: ?[]const u8 = null,
110
+ /// The Windows subsystem verdict: `console` when the package
111
+ /// wrapped a CONSOLE-subsystem exe (the app will flash a terminal
112
+ /// window behind itself on every launch), `gui` for a GUI-subsystem
113
+ /// exe, `unknown` when the probe could not establish either, and
114
+ /// null when the check never ran (non-Windows target, or no binary
115
+ /// to probe). Release-shaped `native build` exes are GUI-subsystem,
116
+ /// so console only fires for stale or hand-supplied binaries — the
117
+ /// packager warns and the report carries the truth (pinned by tests
118
+ /// over a synthetic PE header).
119
+ windows_subsystem: ?WindowsSubsystem = null,
101
120
  };
102
121
 
103
122
  /// The web-layer verdict for a package, from the same declare-to-use
@@ -182,6 +201,13 @@ pub fn printDiagnostic(stats: PackageStats) void {
182
201
  if (stats.signing_verified) {
183
202
  std.debug.print(" signing: {s} (signed, verified)\n", .{@tagName(stats.signing_mode)});
184
203
  }
204
+ if (stats.windows_subsystem) |subsystem| {
205
+ switch (subsystem) {
206
+ .console => std.debug.print(" subsystem: console (a terminal window opens behind the app - rebuild with `native build`)\n", .{}),
207
+ .gui => std.debug.print(" subsystem: gui\n", .{}),
208
+ .unknown => std.debug.print(" subsystem: unknown (unrecognized executable format)\n", .{}),
209
+ }
210
+ }
185
211
  if (stats.archive_path) |archive| {
186
212
  std.debug.print(" archive: {s}\n", .{archive});
187
213
  }
@@ -228,7 +254,7 @@ pub fn createMacosApp(allocator: std.mem.Allocator, io: std.Io, options: Package
228
254
  const bundle_stats = try assets_tool.bundle(allocator, io, options.assets_dir, assets_output);
229
255
  try copyMacosIcon(allocator, io, package_dir, options);
230
256
  try copyMacosDocumentIcons(allocator, io, package_dir, options.metadata);
231
- try writeReport(allocator, package_dir, io, "Contents/Resources/package-manifest.zon", options, executable_name, bundle_stats.asset_count);
257
+ try writeReport(allocator, package_dir, io, "Contents/Resources/package-manifest.zon", options, executable_name, bundle_stats.asset_count, null);
232
258
  if (options.web_engine == .chromium) {
233
259
  try cef.ensureLayout(io, options.cef_dir);
234
260
  try copyMacosCefRuntime(allocator, io, package_dir, options.cef_dir);
@@ -295,6 +321,21 @@ fn createDesktopArtifact(allocator: std.mem.Allocator, io: std.Io, options: Pack
295
321
  }
296
322
  }
297
323
  }
324
+ // The subsystem posture check: `native build` produces GUI-subsystem
325
+ // release exes on Windows, so a console-subsystem exe here is a stale
326
+ // zig-out binary or a hand-supplied --binary — packaged as-is it
327
+ // flashes a terminal window behind the app on every launch. Warn and
328
+ // carry the truth in the stats/report; the package still builds
329
+ // (the app works, it just looks unfinished). Null means the check
330
+ // never ran: a non-Windows target, or no binary to probe.
331
+ const windows_subsystem: ?WindowsSubsystem = if (options.target == .windows)
332
+ if (options.binary_path) |binary_path| try peSubsystemVerdictAtPath(allocator, io, binary_path) else null
333
+ else
334
+ null;
335
+ if (windows_subsystem == .console) {
336
+ std.debug.print("warning[package.console-subsystem]: {s} is a console-subsystem exe, so a terminal window will open behind the app on every launch\n" ++
337
+ " rebuild with `native build` (release exes are GUI-subsystem) or pass a GUI-subsystem --binary\n", .{options.binary_path.?});
338
+ }
298
339
  if (options.binary_path) |binary_path| {
299
340
  const binary_subpath = try std.fmt.allocPrint(allocator, "bin/{s}", .{executable_name});
300
341
  defer allocator.free(binary_subpath);
@@ -343,8 +384,8 @@ fn createDesktopArtifact(allocator: std.mem.Allocator, io: std.Io, options: Pack
343
384
  try cef.ensureLayoutFor(io, cef_platform, options.cef_dir);
344
385
  try copyDesktopCefRuntime(allocator, io, dir, options.target, options.cef_dir);
345
386
  }
346
- try writeReport(allocator, dir, io, "package-manifest.zon", options, executable_name, bundle_stats.asset_count);
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) };
387
+ try writeReport(allocator, dir, io, "package-manifest.zon", options, executable_name, bundle_stats.asset_count, windows_subsystem);
388
+ 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), .windows_subsystem = windows_subsystem };
348
389
  }
349
390
 
350
391
  /// The iOS host tier: a COMPLETE Xcode project the user never edits —
@@ -415,7 +456,7 @@ fn createIosArtifact(allocator: std.mem.Allocator, io: std.Io, options: PackageO
415
456
  const readme = try iosProjectReadme(allocator, options.metadata);
416
457
  defer allocator.free(readme);
417
458
  try writeFile(dir, io, "README.md", readme);
418
- try writeReport(allocator, dir, io, "package-manifest.zon", options, "libnative-sdk.a", bundle_stats.asset_count);
459
+ try writeReport(allocator, dir, io, "package-manifest.zon", options, "libnative-sdk.a", bundle_stats.asset_count, null);
419
460
  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) };
420
461
  }
421
462
 
@@ -477,7 +518,7 @@ fn createAndroidArtifact(allocator: std.mem.Allocator, io: std.Io, options: Pack
477
518
  const readme = try androidProjectReadme(allocator, options.metadata);
478
519
  defer allocator.free(readme);
479
520
  try writeFile(dir, io, "README.md", readme);
480
- try writeReport(allocator, dir, io, "package-manifest.zon", options, "libnative-sdk.a", bundle_stats.asset_count);
521
+ try writeReport(allocator, dir, io, "package-manifest.zon", options, "libnative-sdk.a", bundle_stats.asset_count, null);
481
522
 
482
523
  var artifact_name: []const u8 = std.fs.path.basename(options.output_path);
483
524
  if (try assembleAndroidApk(allocator, io, options)) |apk_name| {
@@ -1071,6 +1112,84 @@ fn peReferencesWebView2Loader(allocator: std.mem.Allocator, io: std.Io, path: []
1071
1112
  std.mem.indexOf(u8, bytes, needle_ascii) != null;
1072
1113
  }
1073
1114
 
1115
+ /// The Windows subsystem a PE executable declares (IMAGE_OPTIONAL_HEADER
1116
+ /// Subsystem: 2 = GUI, 3 = console), or null when the file is not a PE
1117
+ /// with an optional header large enough to say. The packaging check
1118
+ /// reads this because a console-subsystem GUI app flashes a terminal
1119
+ /// window behind itself on every launch — `native build` produces
1120
+ /// GUI-subsystem release exes, so a console one here means a stale or
1121
+ /// hand-built binary.
1122
+ pub fn peSubsystem(bytes: []const u8) ?u16 {
1123
+ if (bytes.len < 0x40 or bytes[0] != 'M' or bytes[1] != 'Z') return null;
1124
+ const pe_offset: usize = std.mem.readInt(u32, bytes[0x3c..0x40], .little);
1125
+ if (pe_offset + 24 > bytes.len) return null;
1126
+ if (!std.mem.eql(u8, bytes[pe_offset..][0..4], "PE\x00\x00")) return null;
1127
+ const optional_size = std.mem.readInt(u16, bytes[pe_offset + 20 ..][0..2], .little);
1128
+ // Subsystem sits 68 bytes into the optional header (same offset in
1129
+ // PE32 and PE32+).
1130
+ if (optional_size < 70) return null;
1131
+ const subsystem_offset = pe_offset + 24 + 68;
1132
+ if (subsystem_offset + 2 > bytes.len) return null;
1133
+ return std.mem.readInt(u16, bytes[subsystem_offset..][0..2], .little);
1134
+ }
1135
+
1136
+ const pe_subsystem_gui: u16 = 2;
1137
+ const pe_subsystem_console: u16 = 3;
1138
+
1139
+ /// The farthest into a file a real executable's PE header is allowed to
1140
+ /// start for the subsystem probe: linkers place `e_lfanew` right after
1141
+ /// the DOS stub (well under a page), so an offset past this ceiling is
1142
+ /// not a real Windows exe and proves nothing about the subsystem —
1143
+ /// exactly like non-PE bytes.
1144
+ const max_pe_header_offset: usize = 1024 * 1024;
1145
+
1146
+ /// The Subsystem field's distance past the PE signature: 24 header
1147
+ /// bytes (signature + COFF), then 68 bytes into the optional header,
1148
+ /// plus the field's own 2 bytes.
1149
+ const pe_subsystem_span: usize = 24 + 70;
1150
+
1151
+ /// `peSubsystem` over a file, reading ONLY the headers: the DOS header
1152
+ /// names where the PE header starts (`e_lfanew` at 0x3c), and the
1153
+ /// Subsystem field sits a fixed `pe_subsystem_span` past that — so
1154
+ /// `min(file size, e_lfanew + span)` bytes bound the read no matter how
1155
+ /// large the executable is. The whole-file slurp this replaces
1156
+ /// (`readPath`, capped at 128 MiB) allocated the entire binary to read
1157
+ /// 2 bytes and silently skipped exes over its cap.
1158
+ fn peSubsystemAtPath(allocator: std.mem.Allocator, io: std.Io, path: []const u8) !?u16 {
1159
+ var file = try std.Io.Dir.cwd().openFile(io, path, .{});
1160
+ defer file.close(io);
1161
+ var read_buffer: [4096]u8 = undefined;
1162
+ var reader = file.reader(io, &read_buffer);
1163
+ var dos_header: [0x40]u8 = undefined;
1164
+ if (try reader.interface.readSliceShort(&dos_header) < dos_header.len) return null;
1165
+ if (dos_header[0] != 'M' or dos_header[1] != 'Z') return null;
1166
+ const pe_offset: usize = std.mem.readInt(u32, dos_header[0x3c..0x40], .little);
1167
+ if (pe_offset > max_pe_header_offset) return null;
1168
+ const bytes = try allocator.alloc(u8, pe_offset + pe_subsystem_span);
1169
+ defer allocator.free(bytes);
1170
+ @memcpy(bytes[0..dos_header.len], &dos_header);
1171
+ const rest = try reader.interface.readSliceShort(bytes[dos_header.len..]);
1172
+ return peSubsystem(bytes[0 .. dos_header.len + rest]);
1173
+ }
1174
+
1175
+ /// The posture check's verdict over a file: only a parsed Subsystem of
1176
+ /// GUI (2) or console (3) earns a named claim; everything else — a file
1177
+ /// the parse proves non-PE or headerless, an unreadable file, or a
1178
+ /// declared subsystem this check does not model (native, EFI, ...) —
1179
+ /// is honestly `unknown`. OutOfMemory still propagates: an allocation
1180
+ /// failure says nothing about the exe.
1181
+ fn peSubsystemVerdictAtPath(allocator: std.mem.Allocator, io: std.Io, path: []const u8) !WindowsSubsystem {
1182
+ const subsystem = peSubsystemAtPath(allocator, io, path) catch |err| switch (err) {
1183
+ error.OutOfMemory => return err,
1184
+ else => return .unknown,
1185
+ };
1186
+ return switch (subsystem orelse return .unknown) {
1187
+ pe_subsystem_gui => .gui,
1188
+ pe_subsystem_console => .console,
1189
+ else => .unknown,
1190
+ };
1191
+ }
1192
+
1074
1193
  /// Whether a Linux executable carries the embedded web layer: the GTK
1075
1194
  /// host links webkitgtk-6.0 directly, so the honest evidence is a
1076
1195
  /// libwebkitgtk/libjavascriptcoregtk DT_NEEDED entry or a webkit_*/jsc_*
@@ -1186,11 +1305,13 @@ fn readPath(allocator: std.mem.Allocator, io: std.Io, path: []const u8) ![]u8 {
1186
1305
  return reader.interface.allocRemaining(allocator, .limited(128 * 1024 * 1024));
1187
1306
  }
1188
1307
 
1189
- fn writeReport(allocator: std.mem.Allocator, dir: std.Io.Dir, io: std.Io, subpath: []const u8, options: PackageOptions, executable_name: []const u8, asset_count: usize) !void {
1308
+ fn writeReport(allocator: std.mem.Allocator, dir: std.Io.Dir, io: std.Io, subpath: []const u8, options: PackageOptions, executable_name: []const u8, asset_count: usize, windows_subsystem: ?WindowsSubsystem) !void {
1190
1309
  const capabilities = try capabilityLines(allocator, options.metadata.capabilities);
1191
1310
  defer allocator.free(capabilities);
1192
1311
  const frontend = try frontendLines(allocator, options.frontend);
1193
1312
  defer allocator.free(frontend);
1313
+ const subsystem = try subsystemLine(allocator, windows_subsystem);
1314
+ defer allocator.free(subsystem);
1194
1315
  const artifact = try zonStringAlloc(allocator, std.fs.path.basename(options.output_path));
1195
1316
  defer allocator.free(artifact);
1196
1317
  const target = try zonStringAlloc(allocator, @tagName(options.target));
@@ -1227,7 +1348,7 @@ fn writeReport(allocator: std.mem.Allocator, dir: std.Io.Dir, io: std.Io, subpat
1227
1348
  \\ .web_engine = {s},
1228
1349
  \\ .web_layer = {s},
1229
1350
  \\ .signing = {s},
1230
- \\ .asset_count = {d},
1351
+ \\{s} .asset_count = {d},
1231
1352
  \\{s}
1232
1353
  \\ .capabilities = .{{
1233
1354
  \\{s}
@@ -1244,6 +1365,7 @@ fn writeReport(allocator: std.mem.Allocator, dir: std.Io.Dir, io: std.Io, subpat
1244
1365
  web_engine,
1245
1366
  web_layer,
1246
1367
  signing,
1368
+ subsystem,
1247
1369
  asset_count,
1248
1370
  frontend,
1249
1371
  capabilities,
@@ -1265,6 +1387,18 @@ fn capabilityLines(allocator: std.mem.Allocator, capabilities: []const []const u
1265
1387
  return out.toOwnedSlice(allocator);
1266
1388
  }
1267
1389
 
1390
+ /// The report's subsystem-verdict line, present only when the posture
1391
+ /// check actually ran (a Windows package with a binary to probe): the
1392
+ /// durable twin of the transient warning[package.console-subsystem],
1393
+ /// in the same string style the report's other verdicts use.
1394
+ fn subsystemLine(allocator: std.mem.Allocator, windows_subsystem: ?WindowsSubsystem) ![]const u8 {
1395
+ const subsystem = windows_subsystem orelse return allocator.dupe(u8, "");
1396
+ return std.fmt.allocPrint(allocator,
1397
+ \\ .subsystem = "{s}",
1398
+ \\
1399
+ , .{@tagName(subsystem)});
1400
+ }
1401
+
1268
1402
  fn frontendLines(allocator: std.mem.Allocator, frontend: ?manifest_tool.FrontendMetadata) ![]const u8 {
1269
1403
  if (frontend) |config| {
1270
1404
  const dist = try zonStringAlloc(allocator, config.dist);
@@ -2369,13 +2503,15 @@ test "package report records target signing and assets" {
2369
2503
  .target = .linux,
2370
2504
  .output_path = ".zig-cache/test-package-report",
2371
2505
  .signing = .{ .mode = .none },
2372
- }, "demo", 2);
2506
+ }, "demo", 2, null);
2373
2507
  var buffer: [512]u8 = undefined;
2374
2508
  var file = try dir.openFile(std.testing.io, "package-manifest.zon", .{});
2375
2509
  defer file.close(std.testing.io);
2376
2510
  const len = try file.readPositionalAll(std.testing.io, &buffer, 0);
2377
2511
  try std.testing.expect(std.mem.indexOf(u8, buffer[0..len], ".target = \"linux\"") != null);
2378
2512
  try std.testing.expect(std.mem.indexOf(u8, buffer[0..len], ".asset_count = 2") != null);
2513
+ // No subsystem check ran, so the report makes no subsystem claim.
2514
+ try std.testing.expect(std.mem.indexOf(u8, buffer[0..len], ".subsystem") == null);
2379
2515
  }
2380
2516
 
2381
2517
  test "adhoc packaging into a spaced output path signs and verifies" {
@@ -2514,10 +2650,13 @@ test "webview-declaring package reports the web layer as declared" {
2514
2650
  });
2515
2651
  try std.testing.expect(stats.web_layer.?.enabled);
2516
2652
  try std.testing.expectEqual(manifest_tool.WebLayerReason.capability, stats.web_layer.?.reason);
2653
+ // No binary means the subsystem check never ran: no verdict to carry.
2654
+ try std.testing.expectEqual(@as(?WindowsSubsystem, null), stats.windows_subsystem);
2517
2655
 
2518
2656
  const report = try readPath(std.testing.allocator, std.testing.io, root ++ "/demo-windows/package-manifest.zon");
2519
2657
  defer std.testing.allocator.free(report);
2520
2658
  try std.testing.expect(std.mem.indexOf(u8, report, ".web_layer = \"webview2 (declared: capabilities)\"") != null);
2659
+ try std.testing.expect(std.mem.indexOf(u8, report, ".subsystem") == null);
2521
2660
  }
2522
2661
 
2523
2662
  test "package refuses a manifest that excludes the web layer while declaring web content" {
@@ -2599,6 +2738,170 @@ fn testWebLayerPeBytes() [0x80 + "WebView2Loader.dll".len * 2]u8 {
2599
2738
  return bytes;
2600
2739
  }
2601
2740
 
2741
+ /// A minimal Windows executable with a real optional header for the
2742
+ /// subsystem tests: MZ + PE + a COFF header declaring a 240-byte
2743
+ /// optional header, with the Subsystem field set as asked.
2744
+ fn testSubsystemPeBytes(subsystem: u16) [0x200]u8 {
2745
+ var bytes = [_]u8{0} ** 0x200;
2746
+ bytes[0] = 'M';
2747
+ bytes[1] = 'Z';
2748
+ std.mem.writeInt(u32, bytes[0x3c..0x40], 0x40, .little);
2749
+ bytes[0x40] = 'P';
2750
+ bytes[0x41] = 'E';
2751
+ // COFF SizeOfOptionalHeader (PE32+ is 240 bytes).
2752
+ std.mem.writeInt(u16, bytes[0x54..0x56], 240, .little);
2753
+ // Optional header starts at 0x58; Subsystem sits 68 bytes in.
2754
+ std.mem.writeInt(u16, bytes[0x9c..0x9e], subsystem, .little);
2755
+ return bytes;
2756
+ }
2757
+
2758
+ test "peSubsystem reads the optional header and refuses non-PE bytes" {
2759
+ const gui = testSubsystemPeBytes(2);
2760
+ try std.testing.expectEqual(@as(?u16, 2), peSubsystem(&gui));
2761
+ const console = testSubsystemPeBytes(3);
2762
+ try std.testing.expectEqual(@as(?u16, 3), peSubsystem(&console));
2763
+ // The loader-scan fixture has no optional header: no subsystem claim.
2764
+ const headerless = testWebLayerPeBytes();
2765
+ try std.testing.expectEqual(@as(?u16, null), peSubsystem(&headerless));
2766
+ try std.testing.expectEqual(@as(?u16, null), peSubsystem("not a pe"));
2767
+ }
2768
+
2769
+ test "the subsystem probe reads only the headers, so an exe past the old slurp cap still warns" {
2770
+ // A console-subsystem exe stretched past readPath's 128 MiB cap
2771
+ // (sparse: valid headers, then a hole). The whole-file slurp this
2772
+ // probe replaced hit the cap's StreamTooLong, swallowed it as
2773
+ // false, and packaged the exe WITHOUT the promised console warning
2774
+ // — the bounded header read answers from the first 94-ish bytes and
2775
+ // never sees the far end of the file.
2776
+ var cwd = std.Io.Dir.cwd();
2777
+ const root = ".zig-cache/test-package-subsystem-huge";
2778
+ try cwd.deleteTree(std.testing.io, root);
2779
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2780
+ try cwd.createDirPath(std.testing.io, root);
2781
+ const console_bytes = testSubsystemPeBytes(3);
2782
+ const path = root ++ "/huge-console.exe";
2783
+ try cwd.writeFile(std.testing.io, .{ .sub_path = path, .data = &console_bytes });
2784
+ {
2785
+ var file = try cwd.openFile(std.testing.io, path, .{ .mode = .read_write });
2786
+ defer file.close(std.testing.io);
2787
+ try file.setLength(std.testing.io, 128 * 1024 * 1024 + 4096);
2788
+ }
2789
+ try std.testing.expectEqual(WindowsSubsystem.console, try peSubsystemVerdictAtPath(std.testing.allocator, std.testing.io, path));
2790
+ }
2791
+
2792
+ test "a PE offset past the header ceiling proves nothing and allocates nothing" {
2793
+ // An `e_lfanew` claiming the PE header sits 8 MiB into the file is
2794
+ // not a real Windows exe (linkers put it right after the DOS stub):
2795
+ // the probe answers null before sizing any buffer to the claimed
2796
+ // offset — the failing allocator turns any allocation into a test
2797
+ // failure — and packaging treats it like non-PE bytes: an unknown
2798
+ // verdict, no warning, and never a gui claim.
2799
+ var cwd = std.Io.Dir.cwd();
2800
+ const root = ".zig-cache/test-package-subsystem-offset";
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);
2804
+ var bytes = [_]u8{0} ** 0x40;
2805
+ bytes[0] = 'M';
2806
+ bytes[1] = 'Z';
2807
+ std.mem.writeInt(u32, bytes[0x3c..0x40], 8 * 1024 * 1024, .little);
2808
+ const path = root ++ "/bogus-offset.exe";
2809
+ try cwd.writeFile(std.testing.io, .{ .sub_path = path, .data = &bytes });
2810
+ try std.testing.expectEqual(WindowsSubsystem.unknown, try peSubsystemVerdictAtPath(std.testing.failing_allocator, std.testing.io, path));
2811
+ }
2812
+
2813
+ test "windows package pins the exe's subsystem: GUI is quiet, console warns in the stats" {
2814
+ var cwd = std.Io.Dir.cwd();
2815
+ const root = ".zig-cache/test-package-subsystem";
2816
+ try cwd.deleteTree(std.testing.io, root);
2817
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2818
+ try cwd.createDirPath(std.testing.io, root ++ "/assets");
2819
+ const metadata: manifest_tool.Metadata = .{
2820
+ .id = "dev.example.subsystem",
2821
+ .name = "subsystem-demo",
2822
+ .version = "1.0.0",
2823
+ };
2824
+
2825
+ // The exe `native build` installs: GUI subsystem, no console flash —
2826
+ // the package carries no console finding.
2827
+ const gui_bytes = testSubsystemPeBytes(2);
2828
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/gui.exe", .data = &gui_bytes });
2829
+ const gui_stats = try createPackage(std.testing.allocator, std.testing.io, .{
2830
+ .metadata = metadata,
2831
+ .target = .windows,
2832
+ .output_path = root ++ "/gui-package",
2833
+ .binary_path = root ++ "/gui.exe",
2834
+ .assets_dir = root ++ "/assets",
2835
+ });
2836
+ try std.testing.expectEqual(@as(?WindowsSubsystem, .gui), gui_stats.windows_subsystem);
2837
+ const gui_report = try readPath(std.testing.allocator, std.testing.io, root ++ "/gui-package/package-manifest.zon");
2838
+ defer std.testing.allocator.free(gui_report);
2839
+ try std.testing.expect(std.mem.indexOf(u8, gui_report, ".subsystem = \"gui\"") != null);
2840
+
2841
+ // A stale or hand-built console-subsystem exe: packaged (the app
2842
+ // still works), but the stats carry the finding the warning teaches.
2843
+ const console_bytes = testSubsystemPeBytes(3);
2844
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/console.exe", .data = &console_bytes });
2845
+ const console_stats = try createPackage(std.testing.allocator, std.testing.io, .{
2846
+ .metadata = metadata,
2847
+ .target = .windows,
2848
+ .output_path = root ++ "/console-package",
2849
+ .binary_path = root ++ "/console.exe",
2850
+ .assets_dir = root ++ "/assets",
2851
+ });
2852
+ try std.testing.expectEqual(@as(?WindowsSubsystem, .console), console_stats.windows_subsystem);
2853
+ const console_report = try readPath(std.testing.allocator, std.testing.io, root ++ "/console-package/package-manifest.zon");
2854
+ defer std.testing.allocator.free(console_report);
2855
+ try std.testing.expect(std.mem.indexOf(u8, console_report, ".subsystem = \"console\"") != null);
2856
+ }
2857
+
2858
+ test "a binary the probe proves nothing about reports subsystem unknown, never gui" {
2859
+ // The parse failing is not evidence of a GUI exe: non-PE bytes and a
2860
+ // PE truncated before its optional header both earn an honest
2861
+ // "unknown" in the stats and report — the old bool verdict folded
2862
+ // these into false and the report affirmatively claimed "gui".
2863
+ var cwd = std.Io.Dir.cwd();
2864
+ const root = ".zig-cache/test-package-subsystem-unknown";
2865
+ try cwd.deleteTree(std.testing.io, root);
2866
+ defer cwd.deleteTree(std.testing.io, root) catch {};
2867
+ try cwd.createDirPath(std.testing.io, root ++ "/assets");
2868
+ const metadata: manifest_tool.Metadata = .{
2869
+ .id = "dev.example.subsystem-unknown",
2870
+ .name = "subsystem-unknown-demo",
2871
+ .version = "1.0.0",
2872
+ };
2873
+
2874
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/not-a-pe.exe", .data = "not a pe at all" });
2875
+ const full_bytes = testSubsystemPeBytes(3);
2876
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/truncated.exe", .data = full_bytes[0..0x50] });
2877
+ const fixtures = [_]struct { binary: []const u8, output: []const u8 }{
2878
+ .{ .binary = root ++ "/not-a-pe.exe", .output = root ++ "/not-a-pe-package" },
2879
+ .{ .binary = root ++ "/truncated.exe", .output = root ++ "/truncated-package" },
2880
+ };
2881
+ for (fixtures) |fixture| {
2882
+ const stats = try createPackage(std.testing.allocator, std.testing.io, .{
2883
+ .metadata = metadata,
2884
+ .target = .windows,
2885
+ .output_path = fixture.output,
2886
+ .binary_path = fixture.binary,
2887
+ .assets_dir = root ++ "/assets",
2888
+ });
2889
+ try std.testing.expectEqual(@as(?WindowsSubsystem, .unknown), stats.windows_subsystem);
2890
+ const report_path = try std.fmt.allocPrint(std.testing.allocator, "{s}/package-manifest.zon", .{fixture.output});
2891
+ defer std.testing.allocator.free(report_path);
2892
+ const report = try readPath(std.testing.allocator, std.testing.io, report_path);
2893
+ defer std.testing.allocator.free(report);
2894
+ try std.testing.expect(std.mem.indexOf(u8, report, ".subsystem = \"unknown\"") != null);
2895
+ try std.testing.expect(std.mem.indexOf(u8, report, ".subsystem = \"gui\"") == null);
2896
+ }
2897
+
2898
+ // A subsystem the check does not model (1 = native) parses fine but
2899
+ // still proves nothing about console-vs-GUI posture.
2900
+ const native_bytes = testSubsystemPeBytes(1);
2901
+ try cwd.writeFile(std.testing.io, .{ .sub_path = root ++ "/native.exe", .data = &native_bytes });
2902
+ try std.testing.expectEqual(WindowsSubsystem.unknown, try peSubsystemVerdictAtPath(std.testing.allocator, std.testing.io, root ++ "/native.exe"));
2903
+ }
2904
+
2602
2905
  test "package --web-layer include on a canvas manifest stages the loader and names the flag" {
2603
2906
  var cwd = std.Io.Dir.cwd();
2604
2907
  const root = ".zig-cache/test-package-web-layer-include-flag";
@@ -433,7 +433,15 @@ fn tsAppMarkup() []const u8 {
433
433
  \\ <text grow="1">ticks {tickCount}</text>
434
434
  \\ <button size="sm" on-press="stamp">Stamp</button>
435
435
  \\ </row>
436
- \\ <status-bar>total: {total} | stamped: {stampedMs}ms</status-bar>
436
+ \\ <!-- The empty state is markup, not model surgery: stampedMs starts
437
+ \\ at -1 (never stamped), and the view says what Stamp will do
438
+ \\ instead of printing "-1ms". -->
439
+ \\ <if test="{stampedMs < 0}">
440
+ \\ <status-bar>total: {total} | press Stamp for a timestamp</status-bar>
441
+ \\ </if>
442
+ \\ <else>
443
+ \\ <status-bar>total: {total} | stamped: {stampedMs}ms</status-bar>
444
+ \\ </else>
437
445
  \\</column>
438
446
  \\
439
447
  ;
@@ -491,8 +499,8 @@ fn tsSlimReadme(allocator: std.mem.Allocator, names: TemplateNames) ![]const u8
491
499
  \\
492
500
  \\## Requirements
493
501
  \\
494
- \\Node.js 22+ on PATH (the TypeScript-to-native transpiler runs at build
495
- \\time; your shipped binary carries none of it).
502
+ \\Node.js 22.15+ (on the 23 line: 23.5+) on PATH (the TypeScript-to-native
503
+ \\transpiler runs at build time; your shipped binary carries none of it).
496
504
  \\
497
505
  );
498
506
  return out.toOwnedSlice(allocator);
@@ -1388,7 +1396,14 @@ fn buildZig(allocator: std.mem.Allocator, names: TemplateNames, framework_path:
1388
1396
  \\
1389
1397
  \\pub fn build(b: *std.Build) void {
1390
1398
  \\ const target = nativeSdkTarget(b);
1391
- \\ const optimize = b.standardOptimizeOption(.{});
1399
+ \\ // -Doptimize is registered by hand (not the std helper) so the
1400
+ \\ // graph can tell "unset" from "explicit": run/dev default to
1401
+ \\ // Debug for the edit loop, while `zig build package` wraps its own
1402
+ \\ // release-shaped exe — the same split `native dev`/`native build`
1403
+ \\ // apply. An explicit -Doptimize (or --release) pins both roles.
1404
+ \\ const optimize_request = b.option(std.builtin.OptimizeMode, "optimize", "Prioritize performance, safety, or binary size");
1405
+ \\ const optimize = optimizeMode(b, optimize_request, .Debug);
1406
+ \\ const package_optimize = optimizeMode(b, optimize_request, .ReleaseFast);
1392
1407
  \\ const platform_option = b.option(PlatformOption, "platform", "Desktop backend: auto, null, macos, linux, windows") orelse .auto;
1393
1408
  \\ const trace_option = b.option(TraceOption, "trace", "Trace output: off, events, runtime, all") orelse .events;
1394
1409
  \\ const debug_overlay = b.option(bool, "debug-overlay", "Enable debug overlay output") orelse false;
@@ -1400,7 +1415,7 @@ fn buildZig(allocator: std.mem.Allocator, names: TemplateNames, framework_path:
1400
1415
  \\ const cef_auto_install_override = b.option(bool, "cef-auto-install", "Override app.zon CEF auto-install setting");
1401
1416
  \\ const package_target = b.option(PackageTarget, "package-target", "Package target: macos, windows, linux") orelse .macos;
1402
1417
  \\ const native_sdk_path = b.option([]const u8, "native-sdk-path", "Path to the Native SDK framework checkout") orelse default_native_sdk_path;
1403
- \\ const optimize_name = @tagName(optimize);
1418
+ \\ const package_optimize_name = @tagName(package_optimize);
1404
1419
  \\ const selected_platform: PlatformOption = switch (platform_option) {
1405
1420
  \\ .auto => if (target.result.os.tag == .macos) .macos else if (target.result.os.tag == .linux) .linux else if (target.result.os.tag == .windows) .windows else .@"null",
1406
1421
  \\ else => platform_option,
@@ -1452,6 +1467,14 @@ fn buildZig(allocator: std.mem.Allocator, names: TemplateNames, framework_path:
1452
1467
  \\ .name = app_exe_name,
1453
1468
  \\ .root_module = app_mod,
1454
1469
  \\ });
1470
+ \\ // Windows subsystem posture (mirrors the Native SDK build graph):
1471
+ \\ // release-shaped exes are GUI-subsystem so the app never flashes a
1472
+ \\ // console behind its window; Debug keeps the console for dev logs.
1473
+ \\ // Redirected logging still works on GUI exes - only console
1474
+ \\ // AUTO-allocation is subsystem-gated.
1475
+ \\ if (target.result.os.tag == .windows and optimize != .Debug) {
1476
+ \\ exe.subsystem = .windows;
1477
+ \\ }
1455
1478
  \\ linkPlatform(b, target, app_mod, exe, selected_platform, web_engine, web_layer, native_sdk_path, cef_dir, cef_auto_install);
1456
1479
  \\ b.installArtifact(exe);
1457
1480
  \\
@@ -1479,6 +1502,33 @@ fn buildZig(allocator: std.mem.Allocator, names: TemplateNames, framework_path:
1479
1502
  \\ const dev_step = b.step("dev", "Run the frontend dev server and native shell");
1480
1503
  \\ dev_step.dependOn(&dev.step);
1481
1504
  \\
1505
+ \\ // `zig build package` wraps its own exe: release-shaped by default
1506
+ \\ // (ReleaseFast, GUI subsystem on Windows) so the packaged artifact
1507
+ \\ // is never a Debug console binary just because the dev loop
1508
+ \\ // defaults to Debug. When -Doptimize/--release pinned one mode for
1509
+ \\ // everything, the roles agree and the dev exe is reused as-is.
1510
+ \\ const package_exe = if (package_optimize == optimize) exe else pkg: {
1511
+ \\ const package_sdk_mod = nativeSdkModule(b, target, package_optimize, native_sdk_path);
1512
+ \\ const package_runner_mod = localModule(b, target, package_optimize, "src/runner.zig");
1513
+ \\ package_runner_mod.addImport("native_sdk", package_sdk_mod);
1514
+ \\ package_runner_mod.addImport("build_options", options_mod);
1515
+ \\ package_runner_mod.addImport("app_manifest_zon", b.createModule(.{ .root_source_file = b.path("app.zon") }));
1516
+ \\ const package_app_mod = localModule(b, target, package_optimize, "src/main.zig");
1517
+ \\ package_app_mod.addImport("native_sdk", package_sdk_mod);
1518
+ \\ package_app_mod.addImport("runner", package_runner_mod);
1519
+ \\ const built = b.addExecutable(.{
1520
+ \\ .name = app_exe_name,
1521
+ \\ .root_module = package_app_mod,
1522
+ \\ });
1523
+ \\ // Same subsystem posture as the dev exe above, keyed on this
1524
+ \\ // exe's own mode: release-shaped Windows exes are GUI-subsystem.
1525
+ \\ if (target.result.os.tag == .windows and package_optimize != .Debug) {
1526
+ \\ built.subsystem = .windows;
1527
+ \\ }
1528
+ \\ linkPlatform(b, target, package_app_mod, built, selected_platform, web_engine, web_layer, native_sdk_path, cef_dir, cef_auto_install);
1529
+ \\ break :pkg built;
1530
+ \\ };
1531
+ \\
1482
1532
  \\ const package = b.addSystemCommand(&.{
1483
1533
  \\ "native",
1484
1534
  \\ "package",
@@ -1492,9 +1542,9 @@ fn buildZig(allocator: std.mem.Allocator, names: TemplateNames, framework_path:
1492
1542
  try out.appendSlice(allocator,
1493
1543
  \\,
1494
1544
  \\ "--optimize",
1495
- \\ optimize_name,
1545
+ \\ package_optimize_name,
1496
1546
  \\ "--output",
1497
- \\ b.fmt("zig-out/package/{s}-0.1.0-{s}-{s}{s}", .{ app_exe_name, @tagName(package_target), optimize_name, packageSuffix(package_target) }),
1547
+ \\ b.fmt("zig-out/package/{s}-0.1.0-{s}-{s}{s}", .{ app_exe_name, @tagName(package_target), package_optimize_name, packageSuffix(package_target) }),
1498
1548
  \\ "--binary",
1499
1549
  \\ });
1500
1550
  \\ // The CLI resolves SDK-owned package inputs (the vendored WebView2
@@ -1502,7 +1552,7 @@ fn buildZig(allocator: std.mem.Allocator, names: TemplateNames, framework_path:
1502
1552
  \\ // belong to a different checkout than the one this build compiled
1503
1553
  \\ // against, so hand the same root over explicitly.
1504
1554
  \\ package.setEnvironmentVariable("NATIVE_SDK_PATH", b.pathFromRoot(native_sdk_path));
1505
- \\ package.addFileArg(exe.getEmittedBin());
1555
+ \\ package.addFileArg(package_exe.getEmittedBin());
1506
1556
  \\ package.addArgs(&.{ "--web-engine", @tagName(web_engine), "--cef-dir", cef_dir });
1507
1557
  \\ // Forward the RESOLVED web-layer decision, never the raw inputs:
1508
1558
  \\ // this graph already decided web vs native-only for the exe it is
@@ -1512,7 +1562,7 @@ fn buildZig(allocator: std.mem.Allocator, names: TemplateNames, framework_path:
1512
1562
  \\ // exe/package agreement structural.
1513
1563
  \\ package.addArgs(&.{ "--web-layer", if (web_layer) "include" else "exclude" });
1514
1564
  \\ if (cef_auto_install) package.addArg("--cef-auto-install");
1515
- \\ package.step.dependOn(&exe.step);
1565
+ \\ package.step.dependOn(&package_exe.step);
1516
1566
  \\ package.step.dependOn(&frontend_build.step);
1517
1567
  \\ const package_step = b.step("package", "Create a local package artifact");
1518
1568
  \\ package_step.dependOn(&package.step);
@@ -1522,6 +1572,21 @@ fn buildZig(allocator: std.mem.Allocator, names: TemplateNames, framework_path:
1522
1572
  \\ test_step.dependOn(&b.addRunArtifact(tests).step);
1523
1573
  \\}
1524
1574
  \\
1575
+ \\// Resolve the optimize mode for one exe role (mirrors the Native SDK
1576
+ \\// build graph): an explicit -Doptimize wins for every role, --release
1577
+ \\// resolves through zig's release_mode, and only when neither was
1578
+ \\// passed does the role keep its own default — Debug for the dev loop,
1579
+ \\// ReleaseFast for the exe `zig build package` wraps.
1580
+ \\fn optimizeMode(b: *std.Build, requested: ?std.builtin.OptimizeMode, default_mode: std.builtin.OptimizeMode) std.builtin.OptimizeMode {
1581
+ \\ if (requested) |mode| return mode;
1582
+ \\ return switch (b.release_mode) {
1583
+ \\ .off => default_mode,
1584
+ \\ .any, .fast => .ReleaseFast,
1585
+ \\ .safe => .ReleaseSafe,
1586
+ \\ .small => .ReleaseSmall,
1587
+ \\ };
1588
+ \\}
1589
+ \\
1525
1590
  \\fn nativeSdkTarget(b: *std.Build) std.Build.ResolvedTarget {
1526
1591
  \\ const target = b.standardTargetOptions(.{});
1527
1592
  \\ if (target.result.os.tag != .macos) return target;
@@ -3549,6 +3614,29 @@ test "writeDefaultApp emits Vite project files" {
3549
3614
  // stub define and webkitgtk-6.0 is not linked when native-only.
3550
3615
  try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "\"-DNATIVE_SDK_ALLOW_WEBKITGTK_STUB\"") != null);
3551
3616
  try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "if (!web_layer) return;") != null);
3617
+ // Release-shaped Windows exes must be GUI-subsystem (same posture as
3618
+ // the SDK build graph) so packaged scaffold apps never flash a console.
3619
+ try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "if (target.result.os.tag == .windows and optimize != .Debug) {") != null);
3620
+ try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "exe.subsystem = .windows;") != null);
3621
+ // The package step wraps its own release-shaped exe: Debug stays the
3622
+ // run/dev default, but `zig build package` must never ship a Debug
3623
+ // (console-subsystem) binary unless the user pinned the mode
3624
+ // explicitly. The --optimize arg and the artifact name follow the
3625
+ // package exe's actual mode, and the exe carries its own
3626
+ // subsystem-posture check keyed on that mode.
3627
+ try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "const optimize_request = b.option(std.builtin.OptimizeMode, \"optimize\"") != null);
3628
+ try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "const optimize = optimizeMode(b, optimize_request, .Debug);") != null);
3629
+ try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "const package_optimize = optimizeMode(b, optimize_request, .ReleaseFast);") != null);
3630
+ try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "const package_exe = if (package_optimize == optimize) exe else pkg: {") != null);
3631
+ try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "if (target.result.os.tag == .windows and package_optimize != .Debug) {") != null);
3632
+ try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "built.subsystem = .windows;") != null);
3633
+ try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "package.addFileArg(package_exe.getEmittedBin());") != null);
3634
+ try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "package.step.dependOn(&package_exe.step);") != null);
3635
+ try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "package_optimize_name,") != null);
3636
+ // No stale wiring: the package step must not reference the dev exe
3637
+ // or the old single-mode name.
3638
+ try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "package.addFileArg(exe.getEmittedBin());") == null);
3639
+ try std.testing.expect(std.mem.indexOf(u8, build_zig_text, "standardOptimizeOption") == null);
3552
3640
  try std.testing.expect(std.mem.indexOf(u8, main_zig_text, "frontend/dist") != null);
3553
3641
  try std.testing.expect(std.mem.indexOf(u8, main_zig_text, "127.0.0.1:5173") != null);
3554
3642
  try std.testing.expect(std.mem.indexOf(u8, runner_zig_text, "@import(\"app_manifest_zon\")") != null);