@native-sdk/cli 0.4.4 → 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 (83) hide show
  1. package/build/app.zig +187 -6
  2. package/build/ts_run.mjs +63 -0
  3. package/build.zig +213 -1
  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/native-ui/SKILL.md +14 -0
  28. package/skill-data/ts-core/SKILL.md +493 -0
  29. package/skill-data/zig/SKILL.md +10 -0
  30. package/src/app_runner/root.zig +24 -0
  31. package/src/app_runner/ts_core_main.zig +231 -0
  32. package/src/primitives/app_manifest/comptime_scene.zig +341 -0
  33. package/src/primitives/app_manifest/root.zig +5 -0
  34. package/src/primitives/canvas/commands.zig +14 -7
  35. package/src/primitives/canvas/lazy_tls.zig +73 -0
  36. package/src/primitives/canvas/render_generic_resources.zig +22 -15
  37. package/src/primitives/canvas/root.zig +6 -0
  38. package/src/primitives/canvas/tests.zig +1 -0
  39. package/src/primitives/canvas/text_atlas.zig +40 -32
  40. package/src/primitives/canvas/text_layout_cache.zig +29 -22
  41. package/src/primitives/canvas/text_measure_cache.zig +56 -41
  42. package/src/primitives/canvas/text_spans.zig +39 -26
  43. package/src/primitives/canvas/tokens.zig +54 -0
  44. package/src/primitives/canvas/ui.zig +125 -0
  45. package/src/primitives/canvas/ui_markup.zig +22 -3
  46. package/src/primitives/canvas/ui_markup_compiled.zig +97 -7
  47. package/src/primitives/canvas/ui_markup_compiled_tests.zig +275 -0
  48. package/src/primitives/canvas/ui_markup_contract.zig +68 -14
  49. package/src/primitives/canvas/ui_markup_contract_tests.zig +257 -2
  50. package/src/primitives/canvas/ui_markup_reflect.zig +214 -0
  51. package/src/primitives/canvas/ui_markup_view.zig +105 -1
  52. package/src/primitives/canvas/ui_markup_view_tests.zig +336 -0
  53. package/src/primitives/canvas/ui_schema.zig +8 -0
  54. package/src/primitives/canvas/ui_schema_tests.zig +5 -4
  55. package/src/primitives/canvas/widget_invalidation.zig +14 -7
  56. package/src/primitives/canvas/widget_render.zig +25 -15
  57. package/src/primitives/canvas/widget_render_controls.zig +15 -9
  58. package/src/primitives/canvas/widget_runtime_tests.zig +44 -0
  59. package/src/root.zig +7 -0
  60. package/src/runtime/canvas_frame.zig +57 -36
  61. package/src/runtime/canvas_images.zig +9 -3
  62. package/src/runtime/canvas_widget_control_tests.zig +5 -6
  63. package/src/runtime/canvas_widget_runtime.zig +7 -4
  64. package/src/runtime/effects.zig +490 -2
  65. package/src/runtime/effects_host_tests.zig +594 -0
  66. package/src/runtime/root.zig +18 -0
  67. package/src/runtime/session_replay.zig +7 -1
  68. package/src/runtime/tests.zig +2 -0
  69. package/src/runtime/ts_core_host.zig +1505 -0
  70. package/src/runtime/ts_core_host_tests.zig +1408 -0
  71. package/src/runtime/ts_ui_app.zig +469 -0
  72. package/src/runtime/ui_app.zig +58 -0
  73. package/src/runtime/view_canvas.zig +24 -17
  74. package/src/runtime/view_widget_tree.zig +2 -2
  75. package/src/tooling/codesign.zig +212 -105
  76. package/src/tooling/doctor.zig +78 -0
  77. package/src/tooling/manifest.zig +47 -0
  78. package/src/tooling/package.zig +124 -12
  79. package/src/tooling/raw_manifest.zig +15 -0
  80. package/src/tooling/root.zig +1 -0
  81. package/src/tooling/templates.zig +683 -28
  82. package/src/tooling/ts_core.zig +486 -0
  83. package/src/tooling/verbs.zig +17 -0
@@ -0,0 +1,486 @@
1
+ //! TypeScript-core plumbing for the `native` CLI: tree detection (which
2
+ //! core does this app carry?), the transpiler-checker pass `native check`
3
+ //! runs over src/core.ts, and the node dev-harness `native dev --core`
4
+ //! launches. The build graph re-derives the same detection in
5
+ //! build/app.zig; the CLI checks first so a both-cores tree fails with one
6
+ //! clean teaching message instead of a build panic.
7
+ //!
8
+ //! Multi-file cores: src/core.ts stays the detection root AND the entry
9
+ //! module, but a core may split into modules under src/ (relative imports
10
+ //! with real .ts filenames) plus SDK library modules
11
+ //! ("@native-sdk/core/text"). The transpiler walks that import graph
12
+ //! itself, so `native check` reports diagnostics with each module's own
13
+ //! path, and `native dev --core` runs the same graph under node (relative
14
+ //! imports are real files; the resolver hook maps only the SDK names).
15
+
16
+ const std = @import("std");
17
+ const buildgraph = @import("buildgraph.zig");
18
+ const process_tree = @import("process_tree.zig");
19
+
20
+ pub const Error = error{
21
+ BothCores,
22
+ MissingTsCore,
23
+ MissingNode,
24
+ MissingTranspiler,
25
+ CoreCheckFailed,
26
+ DevHostFailed,
27
+ };
28
+
29
+ /// Which core the app tree carries: `src/core.ts` is a TypeScript core,
30
+ /// `src/main.zig` a Zig one. Both at once is a teaching error (the tree is
31
+ /// the truth; there is no language flag or config to consult). Other Zig
32
+ /// files under src/ never affect detection — and neither does anything
33
+ /// else in the tree: a package.json (the TS scaffold's editor surface, or
34
+ /// a stray one in a Zig app) is not a language marker.
35
+ pub const CoreTree = enum { zig, ts, both, neither };
36
+
37
+ pub fn detect(io: std.Io) CoreTree {
38
+ return detectAt(io, ".");
39
+ }
40
+
41
+ /// `detect` against an explicit app root instead of the process cwd.
42
+ pub fn detectAt(io: std.Io, app_root: []const u8) CoreTree {
43
+ var ts_buffer: [1024]u8 = undefined;
44
+ var zig_buffer: [1024]u8 = undefined;
45
+ const ts_path = std.fmt.bufPrint(&ts_buffer, "{s}/src/core.ts", .{app_root}) catch return .neither;
46
+ const zig_path = std.fmt.bufPrint(&zig_buffer, "{s}/src/main.zig", .{app_root}) catch return .neither;
47
+ const has_ts = buildgraph.fileExists(io, ts_path);
48
+ const has_zig = buildgraph.fileExists(io, zig_path);
49
+ if (has_ts and has_zig) return .both;
50
+ if (has_ts) return .ts;
51
+ if (has_zig) return .zig;
52
+ return .neither;
53
+ }
54
+
55
+ /// The one both-cores teaching message, shared by every verb that detects.
56
+ pub fn failBothCores() Error {
57
+ std.debug.print(
58
+ \\this app declares two cores: src/core.ts (TypeScript) and src/main.zig (Zig).
59
+ \\An app has exactly one core - the tree is the truth. Keep src/core.ts and delete
60
+ \\src/main.zig, or keep src/main.zig and delete src/core.ts. (Other Zig files
61
+ \\under src/ are fine either way.)
62
+ \\
63
+ , .{});
64
+ return error.BothCores;
65
+ }
66
+
67
+ fn nodeMissing() Error {
68
+ std.debug.print(
69
+ \\TypeScript app cores need node on PATH (the @native-sdk/core transpiler and the
70
+ \\core dev-harness run under it; the binary you ship carries no JS runtime).
71
+ \\Install Node.js 22+ - https://nodejs.org or `brew install node` - and re-run.
72
+ \\
73
+ , .{});
74
+ return error.MissingNode;
75
+ }
76
+
77
+ fn transpilerPath(allocator: std.mem.Allocator, io: std.Io, framework_root: []const u8, comptime sub: []const u8) ![]const u8 {
78
+ const path = try std.fs.path.join(allocator, &.{ framework_root, "packages", "core", sub });
79
+ errdefer allocator.free(path);
80
+ if (!buildgraph.fileExists(io, path)) {
81
+ std.debug.print("the Native SDK at {s} is missing packages/core/{s} - is the checkout (or npm install) complete?\n", .{ framework_root, sub });
82
+ return error.MissingTranspiler;
83
+ }
84
+ return path;
85
+ }
86
+
87
+ /// The layout-neutral runner for the transpiler tier's .ts modules
88
+ /// (build/ts_run.mjs): a pass-through on a repo checkout, and the type
89
+ /// stripper for the npm-installed layout, where the same modules sit
90
+ /// inside node_modules and node refuses its builtin stripping. Every
91
+ /// node invocation of a packages/core .ts module goes through it.
92
+ fn tsRunnerPath(allocator: std.mem.Allocator, io: std.Io, framework_root: []const u8) ![]const u8 {
93
+ const path = try std.fs.path.join(allocator, &.{ framework_root, "build", "ts_run.mjs" });
94
+ errdefer allocator.free(path);
95
+ if (!buildgraph.fileExists(io, path)) {
96
+ std.debug.print("the Native SDK at {s} is missing build/ts_run.mjs - is the checkout (or npm install) complete?\n", .{framework_root});
97
+ return error.MissingTranspiler;
98
+ }
99
+ return path;
100
+ }
101
+
102
+ fn requireInstalledTranspiler(allocator: std.mem.Allocator, io: std.Io, framework_root: []const u8) !void {
103
+ const marker = try std.fs.path.join(allocator, &.{ framework_root, "packages", "core", "node_modules", "@typescript", "typescript6" });
104
+ defer allocator.free(marker);
105
+ const stat = std.Io.Dir.cwd().statFile(io, marker, .{}) catch {
106
+ std.debug.print(
107
+ \\the SDK's @native-sdk/core transpiler is missing its installed dependency.
108
+ \\Fix with: cd {s}/packages/core && npm ci
109
+ \\
110
+ , .{framework_root});
111
+ return error.MissingTranspiler;
112
+ };
113
+ _ = stat;
114
+ }
115
+
116
+ /// `native check` over a TypeScript core: run the transpiler (checker +
117
+ /// emitter) on src/core.ts — and, through it, the core's whole import
118
+ /// graph under src/ — and surface its NS diagnostics verbatim — they
119
+ /// are the teaching layer, nothing wraps them (each diagnostic carries
120
+ /// the owning module's path). The emitted Zig lands in .native/check/ (a
121
+ /// scratch product, gitignored with the rest of .native/). Exit 0 =
122
+ /// typechecked, subset-clean, emitted.
123
+ pub fn checkCore(allocator: std.mem.Allocator, io: std.Io, base_env: *std.process.Environ.Map, framework_root: []const u8) !void {
124
+ const cli_path = try transpilerPath(allocator, io, framework_root, "src/cli.ts");
125
+ defer allocator.free(cli_path);
126
+ const runner_path = try tsRunnerPath(allocator, io, framework_root);
127
+ defer allocator.free(runner_path);
128
+ try requireInstalledTranspiler(allocator, io, framework_root);
129
+ try std.Io.Dir.cwd().createDirPath(io, ".native/check");
130
+
131
+ var child = std.process.spawn(io, .{
132
+ .argv = &.{ "node", runner_path, cli_path, "src/core.ts", "-o", ".native/check/core.zig" },
133
+ .stdin = .ignore,
134
+ .stdout = .inherit,
135
+ .stderr = .inherit,
136
+ .environ_map = base_env,
137
+ }) catch return nodeMissing();
138
+ const term = try child.wait(io);
139
+ switch (term) {
140
+ .exited => |code| if (code == 0) return,
141
+ else => {},
142
+ }
143
+ // The transpiler's own diagnostics are already on screen; name the
144
+ // failing pass without burying them.
145
+ std.debug.print("native check: src/core.ts failed the @native-sdk/core checker (diagnostics above)\n", .{});
146
+ return error.CoreCheckFailed;
147
+ }
148
+
149
+ pub const DevHostOptions = struct {
150
+ base_env: *std.process.Environ.Map,
151
+ /// NDJSON message script; null = interactive stdin.
152
+ script: ?[]const u8 = null,
153
+ /// Re-run the harness whenever any module of the core changes.
154
+ /// Requires a script (a re-run replays it against the edited core).
155
+ watch: bool = false,
156
+ };
157
+
158
+ /// `native dev --core`: the node dev-harness over src/core.ts — the
159
+ /// core-logic loop (dispatch messages, watch the model and effect
160
+ /// transcript), not a renderer. Watch mode rides `node --watch`.
161
+ pub fn runDevHost(allocator: std.mem.Allocator, io: std.Io, framework_root: []const u8, options: DevHostOptions) !void {
162
+ if (!buildgraph.fileExists(io, "src/core.ts")) {
163
+ std.debug.print("no src/core.ts here - `native dev --core` runs the TypeScript core-logic loop\n", .{});
164
+ return error.MissingTsCore;
165
+ }
166
+ const devhost_path = try transpilerPath(allocator, io, framework_root, "src/devhost.ts");
167
+ defer allocator.free(devhost_path);
168
+ const runner_path = try tsRunnerPath(allocator, io, framework_root);
169
+ defer allocator.free(runner_path);
170
+
171
+ var argv: std.ArrayList([]const u8) = .empty;
172
+ defer argv.deinit(allocator);
173
+ try argv.append(allocator, "node");
174
+ if (options.watch) {
175
+ if (options.script == null) {
176
+ std.debug.print("`native dev --core --watch` needs --script <msgs.ndjson>: each re-run replays the script against the edited core\n", .{});
177
+ return error.DevHostFailed;
178
+ }
179
+ try argv.append(allocator, "--watch");
180
+ }
181
+ try argv.append(allocator, runner_path);
182
+ try argv.append(allocator, devhost_path);
183
+ try argv.append(allocator, "src/core.ts");
184
+ if (options.script) |script| {
185
+ try argv.appendSlice(allocator, &.{ "--script", script });
186
+ }
187
+
188
+ std.debug.print("native dev --core: the core-logic loop under node (update/effects, virtual clock) - not a renderer; `native dev` runs the app\n", .{});
189
+ var child = std.process.spawn(io, .{
190
+ .argv = argv.items,
191
+ .stdin = .inherit,
192
+ .stdout = .inherit,
193
+ .stderr = .inherit,
194
+ .environ_map = options.base_env,
195
+ .pgid = if (process_tree.supported) process_tree.spawnPgid() else null,
196
+ }) catch return nodeMissing();
197
+ const group_pid: i32 = if (process_tree.supported) process_tree.groupId(&child) else 0;
198
+ if (group_pid > 0) process_tree.own(group_pid);
199
+ defer if (group_pid > 0) process_tree.releaseAndKill(group_pid);
200
+ const term = try child.wait(io);
201
+ switch (term) {
202
+ .exited => |code| if (code == 0) return,
203
+ else => {},
204
+ }
205
+ std.debug.print("native dev --core: the harness exited abnormally\n", .{});
206
+ return error.DevHostFailed;
207
+ }
208
+
209
+ // ------------------------------------------------------- editor package
210
+ //
211
+ // Stock editor TypeScript (VS Code et al.) resolves `@native-sdk/core`
212
+ // the only way editors know: node_modules. Until the package is published
213
+ // to npm, the CLI materializes `node_modules/@native-sdk/core` inside the
214
+ // app itself — a plain COPY (never a symlink; cache paths get GC'd) of
215
+ // exactly the files the published artifact will contain (package.json plus
216
+ // the `files: ["sdk"]` allowlist: sdk/core.ts, sdk/text.ts, sdk/events.ts,
217
+ // and the ambient bytes-text method surface core.ts references).
218
+ // The copy is
219
+ // EDITOR-AND-VERSIONING SURFACE ONLY: builds transpile against the SDK
220
+ // checkout's own sources and never read node_modules — delete it and
221
+ // `native build|dev|check|test` still work; the next check/dev/build puts
222
+ // it back. Once the real package is published, a user-run `npm install`
223
+ // overwrites the copy with identical content; the refresh below compares
224
+ // the one `version` field, sees it current, and never touches it again.
225
+
226
+ /// Where the editor copy lives inside an app.
227
+ pub const editor_package_dir = "node_modules/@native-sdk/core";
228
+
229
+ /// The published artifact's files, relative to packages/core (and to the
230
+ /// materialized copy): package.json plus its `files: ["sdk"]` allowlist.
231
+ /// packages/core/test/package_manifest.test.ts pins the manifest to this
232
+ /// shape, so copy and tarball cannot drift apart.
233
+ const editor_package_files = [_][]const u8{ "package.json", "sdk/core.ts", "sdk/text.ts", "sdk/events.ts", "sdk/bytes_text_methods.d.ts" };
234
+
235
+ /// Extract the top-level "version" of a package.json. A targeted scan, not
236
+ /// a JSON parse: both inputs are our own manifest and npm's byte-identical
237
+ /// install of it, where the first "version" key is the package version.
238
+ pub fn parsePackageVersion(manifest_json: []const u8) ?[]const u8 {
239
+ const key = "\"version\"";
240
+ const key_at = std.mem.indexOf(u8, manifest_json, key) orelse return null;
241
+ var rest = manifest_json[key_at + key.len ..];
242
+ rest = std.mem.trimStart(u8, rest, " \t\r\n");
243
+ if (rest.len == 0 or rest[0] != ':') return null;
244
+ rest = std.mem.trimStart(u8, rest[1..], " \t\r\n");
245
+ if (rest.len == 0 or rest[0] != '"') return null;
246
+ rest = rest[1..];
247
+ const end = std.mem.indexOfScalar(u8, rest, '"') orelse return null;
248
+ if (end == 0) return null;
249
+ return rest[0..end];
250
+ }
251
+
252
+ /// The version of the SDK's bundled @native-sdk/core package
253
+ /// (packages/core/package.json). Caller frees.
254
+ pub fn bundledSdkVersion(allocator: std.mem.Allocator, io: std.Io, framework_root: []const u8) ![]const u8 {
255
+ const path = try std.fs.path.join(allocator, &.{ framework_root, "packages", "core", "package.json" });
256
+ defer allocator.free(path);
257
+ const manifest = std.Io.Dir.cwd().readFileAlloc(io, path, allocator, .limited(64 * 1024)) catch
258
+ return error.MissingTranspiler;
259
+ defer allocator.free(manifest);
260
+ const version = parsePackageVersion(manifest) orelse return error.MissingTranspiler;
261
+ return allocator.dupe(u8, version);
262
+ }
263
+
264
+ /// How an app's editor copy compares to the SDK's bundled package.
265
+ pub const EditorPackageStatus = struct {
266
+ /// The SDK-bundled @native-sdk/core version (owned).
267
+ bundled: []const u8,
268
+ /// The app copy's version, or null when the copy is missing or
269
+ /// incomplete (owned when present).
270
+ installed: ?[]const u8,
271
+
272
+ pub fn fresh(self: EditorPackageStatus) bool {
273
+ const installed = self.installed orelse return false;
274
+ return std.mem.eql(u8, installed, self.bundled);
275
+ }
276
+
277
+ pub fn deinit(self: EditorPackageStatus, allocator: std.mem.Allocator) void {
278
+ allocator.free(self.bundled);
279
+ if (self.installed) |installed| allocator.free(installed);
280
+ }
281
+ };
282
+
283
+ /// Compare `<app_root>/node_modules/@native-sdk/core` against the SDK's
284
+ /// bundled package. Cheap by design (the refresh trigger runs on every
285
+ /// check/dev/build): two one-field manifest reads plus two stats.
286
+ pub fn editorPackageStatus(allocator: std.mem.Allocator, io: std.Io, framework_root: []const u8, app_root: []const u8) !EditorPackageStatus {
287
+ const bundled = try bundledSdkVersion(allocator, io, framework_root);
288
+ errdefer allocator.free(bundled);
289
+
290
+ const installed: ?[]const u8 = read: {
291
+ const manifest_path = try std.fs.path.join(allocator, &.{ app_root, editor_package_dir, "package.json" });
292
+ defer allocator.free(manifest_path);
293
+ const manifest = std.Io.Dir.cwd().readFileAlloc(io, manifest_path, allocator, .limited(64 * 1024)) catch break :read null;
294
+ defer allocator.free(manifest);
295
+ const version = parsePackageVersion(manifest) orelse break :read null;
296
+ // A manifest without its sdk/ sources is an incomplete copy (a
297
+ // half-deleted tree), not a fresh install at that version.
298
+ for (editor_package_files[1..]) |sub_path| {
299
+ const file_path = try std.fs.path.join(allocator, &.{ app_root, editor_package_dir, sub_path });
300
+ defer allocator.free(file_path);
301
+ if (!buildgraph.fileExists(io, file_path)) break :read null;
302
+ }
303
+ break :read try allocator.dupe(u8, version);
304
+ };
305
+ return .{ .bundled = bundled, .installed = installed };
306
+ }
307
+
308
+ pub const EnsureOutcome = enum { fresh, materialized };
309
+
310
+ /// Make `<app_root>/node_modules/@native-sdk/core` a current copy of the
311
+ /// SDK's bundled package: leave a version-matching copy alone (that is the
312
+ /// post-publish `npm install` handoff — npm's content is identical, so the
313
+ /// CLI recognizes the version and stops rewriting), (re)materialize
314
+ /// otherwise.
315
+ pub fn ensureEditorPackage(allocator: std.mem.Allocator, io: std.Io, framework_root: []const u8, app_root: []const u8) !EnsureOutcome {
316
+ const status = try editorPackageStatus(allocator, io, framework_root, app_root);
317
+ defer status.deinit(allocator);
318
+ if (status.fresh()) return .fresh;
319
+
320
+ for (editor_package_files) |sub_path| {
321
+ const source_path = try std.fs.path.join(allocator, &.{ framework_root, "packages", "core", sub_path });
322
+ defer allocator.free(source_path);
323
+ const data = std.Io.Dir.cwd().readFileAlloc(io, source_path, allocator, .limited(4 * 1024 * 1024)) catch
324
+ return error.MissingTranspiler;
325
+ defer allocator.free(data);
326
+ const dest_path = try std.fs.path.join(allocator, &.{ app_root, editor_package_dir, sub_path });
327
+ defer allocator.free(dest_path);
328
+ if (std.fs.path.dirname(dest_path)) |parent| {
329
+ try std.Io.Dir.cwd().createDirPath(io, parent);
330
+ }
331
+ try std.Io.Dir.cwd().writeFile(io, .{ .sub_path = dest_path, .data = data });
332
+ }
333
+ return .materialized;
334
+ }
335
+
336
+ /// The self-heal hook `native check|dev|build|test` run over a TS app (cwd
337
+ /// = the app root): refresh the editor copy when missing or stale, say so
338
+ /// in one line, and never fail the verb — the copy is editor surface, and
339
+ /// build truth must not depend on it in either direction.
340
+ pub fn selfHealEditorPackage(allocator: std.mem.Allocator, io: std.Io, framework_root: []const u8) void {
341
+ const outcome = ensureEditorPackage(allocator, io, framework_root, ".") catch return;
342
+ if (outcome == .materialized) {
343
+ std.debug.print("refreshed node_modules/@native-sdk/core (editor types for stock tsc; builds never read it)\n", .{});
344
+ }
345
+ }
346
+
347
+ test "core tree detection reads the tree, not config" {
348
+ const io = std.testing.io;
349
+ var cwd = std.Io.Dir.cwd();
350
+ const root = ".zig-cache/test-ts-core-detect";
351
+ cwd.deleteTree(io, root) catch {};
352
+ try cwd.createDirPath(io, root ++ "/src");
353
+
354
+ // Empty tree: neither.
355
+ {
356
+ var dir = try cwd.openDir(io, root, .{});
357
+ defer dir.close(io);
358
+ // detect() reads the process cwd; probe the predicate parts instead.
359
+ }
360
+ try std.testing.expect(!buildgraph.fileExists(io, root ++ "/src/core.ts"));
361
+ try cwd.writeFile(io, .{ .sub_path = root ++ "/src/core.ts", .data = "// ts" });
362
+ try std.testing.expect(buildgraph.fileExists(io, root ++ "/src/core.ts"));
363
+ try cwd.writeFile(io, .{ .sub_path = root ++ "/src/main.zig", .data = "// zig" });
364
+ try std.testing.expect(buildgraph.fileExists(io, root ++ "/src/main.zig"));
365
+ cwd.deleteTree(io, root) catch {};
366
+ }
367
+
368
+ test "a stray package.json is not a language marker: detection keys on the core files only" {
369
+ const io = std.testing.io;
370
+ var cwd = std.Io.Dir.cwd();
371
+ const root = ".zig-cache/test-ts-core-detect-residue";
372
+ cwd.deleteTree(io, root) catch {};
373
+ try cwd.createDirPath(io, root ++ "/src");
374
+ defer cwd.deleteTree(io, root) catch {};
375
+
376
+ // A Zig app that grew a package.json (frontend tooling, a stray npm
377
+ // init) is still a Zig app.
378
+ try cwd.writeFile(io, .{ .sub_path = root ++ "/src/main.zig", .data = "// zig" });
379
+ try cwd.writeFile(io, .{ .sub_path = root ++ "/package.json", .data = "{ \"name\": \"stray\" }" });
380
+ try std.testing.expectEqual(CoreTree.zig, detectAt(io, root));
381
+
382
+ // And a TS app whose editor surface (package.json, node_modules) was
383
+ // deleted is still a TS app.
384
+ try cwd.deleteFile(io, root ++ "/src/main.zig");
385
+ try cwd.deleteFile(io, root ++ "/package.json");
386
+ try cwd.writeFile(io, .{ .sub_path = root ++ "/src/core.ts", .data = "// ts" });
387
+ try std.testing.expectEqual(CoreTree.ts, detectAt(io, root));
388
+ }
389
+
390
+ test "package.json version extraction" {
391
+ try std.testing.expectEqualStrings("0.0.1", parsePackageVersion(
392
+ \\{
393
+ \\ "name": "@native-sdk/core",
394
+ \\ "private": true,
395
+ \\ "version": "0.0.1"
396
+ \\}
397
+ ).?);
398
+ try std.testing.expectEqualStrings("1.2.3", parsePackageVersion("{\"version\":\"1.2.3\"}").?);
399
+ try std.testing.expect(parsePackageVersion("{}") == null);
400
+ try std.testing.expect(parsePackageVersion("{\"version\": 3}") == null);
401
+ try std.testing.expect(parsePackageVersion("{\"version\": \"\"}") == null);
402
+ }
403
+
404
+ // The materialize -> self-heal -> npm-install handoff lifecycle, against a
405
+ // fake SDK checkout so the pinned versions are the test's own.
406
+ test "editor package: materialize, heal, and the npm-install handoff" {
407
+ const allocator = std.testing.allocator;
408
+ const io = std.testing.io;
409
+ var cwd = std.Io.Dir.cwd();
410
+ const root = ".zig-cache/test-ts-editor-package";
411
+ const sdk = root ++ "/sdk-checkout";
412
+ const app = root ++ "/app";
413
+ cwd.deleteTree(io, root) catch {};
414
+ defer cwd.deleteTree(io, root) catch {};
415
+
416
+ try cwd.createDirPath(io, sdk ++ "/packages/core/sdk");
417
+ try cwd.createDirPath(io, app ++ "/src");
418
+ try cwd.writeFile(io, .{ .sub_path = sdk ++ "/packages/core/package.json", .data = "{ \"name\": \"@native-sdk/core\", \"version\": \"0.0.9\" }" });
419
+ try cwd.writeFile(io, .{ .sub_path = sdk ++ "/packages/core/sdk/core.ts", .data = "// core module" });
420
+ try cwd.writeFile(io, .{ .sub_path = sdk ++ "/packages/core/sdk/text.ts", .data = "// text module" });
421
+ try cwd.writeFile(io, .{ .sub_path = sdk ++ "/packages/core/sdk/events.ts", .data = "// events module" });
422
+ try cwd.writeFile(io, .{ .sub_path = sdk ++ "/packages/core/sdk/bytes_text_methods.d.ts", .data = "// ambient text methods" });
423
+
424
+ const bundled = try bundledSdkVersion(allocator, io, sdk);
425
+ defer allocator.free(bundled);
426
+ try std.testing.expectEqualStrings("0.0.9", bundled);
427
+
428
+ // Missing copy: materialized, and the full artifact lands.
429
+ try std.testing.expectEqual(EnsureOutcome.materialized, try ensureEditorPackage(allocator, io, sdk, app));
430
+ try std.testing.expect(buildgraph.fileExists(io, app ++ "/node_modules/@native-sdk/core/package.json"));
431
+ try std.testing.expect(buildgraph.fileExists(io, app ++ "/node_modules/@native-sdk/core/sdk/core.ts"));
432
+ try std.testing.expect(buildgraph.fileExists(io, app ++ "/node_modules/@native-sdk/core/sdk/text.ts"));
433
+ try std.testing.expect(buildgraph.fileExists(io, app ++ "/node_modules/@native-sdk/core/sdk/events.ts"));
434
+ try std.testing.expect(buildgraph.fileExists(io, app ++ "/node_modules/@native-sdk/core/sdk/bytes_text_methods.d.ts"));
435
+
436
+ // Current copy: untouched (this is also the post-publish handoff — a
437
+ // real `npm install` writes identical content at the same version, so
438
+ // user-owned node_modules stop being rewritten). Prove "untouched"
439
+ // through a sentinel edit the refresh must NOT undo.
440
+ try cwd.writeFile(io, .{ .sub_path = app ++ "/node_modules/@native-sdk/core/sdk/core.ts", .data = "// same version, npm-owned bytes" });
441
+ try std.testing.expectEqual(EnsureOutcome.fresh, try ensureEditorPackage(allocator, io, sdk, app));
442
+ const untouched = try cwd.readFileAlloc(io, app ++ "/node_modules/@native-sdk/core/sdk/core.ts", allocator, .limited(1024));
443
+ defer allocator.free(untouched);
444
+ try std.testing.expectEqualStrings("// same version, npm-owned bytes", untouched);
445
+
446
+ // Version skew (SDK moved on): refreshed to the bundled content.
447
+ try cwd.writeFile(io, .{ .sub_path = sdk ++ "/packages/core/package.json", .data = "{ \"name\": \"@native-sdk/core\", \"version\": \"0.1.0\" }" });
448
+ try std.testing.expectEqual(EnsureOutcome.materialized, try ensureEditorPackage(allocator, io, sdk, app));
449
+ const refreshed = try cwd.readFileAlloc(io, app ++ "/node_modules/@native-sdk/core/sdk/core.ts", allocator, .limited(1024));
450
+ defer allocator.free(refreshed);
451
+ try std.testing.expectEqualStrings("// core module", refreshed);
452
+ {
453
+ const status = try editorPackageStatus(allocator, io, sdk, app);
454
+ defer status.deinit(allocator);
455
+ try std.testing.expect(status.fresh());
456
+ }
457
+
458
+ // A half-deleted copy (manifest present, sdk/ gone) heals too.
459
+ try cwd.deleteFile(io, app ++ "/node_modules/@native-sdk/core/sdk/text.ts");
460
+ try std.testing.expectEqual(EnsureOutcome.materialized, try ensureEditorPackage(allocator, io, sdk, app));
461
+ try std.testing.expect(buildgraph.fileExists(io, app ++ "/node_modules/@native-sdk/core/sdk/text.ts"));
462
+ }
463
+
464
+ // The copy the CLI materializes IS the future published artifact: the
465
+ // bundled manifest must carry the exports/files shape editors resolve
466
+ // through (pinned in full by packages/core/test/package_manifest.test.ts;
467
+ // this cross-check keeps the Zig side honest about the file list it
468
+ // copies). Runs against the real repo checkout.
469
+ test "the bundled @native-sdk/core manifest matches the materialized file list" {
470
+ const allocator = std.testing.allocator;
471
+ const io = std.testing.io;
472
+ const manifest = std.Io.Dir.cwd().readFileAlloc(io, "packages/core/package.json", allocator, .limited(64 * 1024)) catch return error.SkipZigTest;
473
+ defer allocator.free(manifest);
474
+ try std.testing.expect(parsePackageVersion(manifest) != null);
475
+ try std.testing.expect(std.mem.indexOf(u8, manifest, "\"files\"") != null);
476
+ try std.testing.expect(std.mem.indexOf(u8, manifest, "\"./sdk/core.ts\"") != null);
477
+ try std.testing.expect(std.mem.indexOf(u8, manifest, "\"./text\"") != null);
478
+ try std.testing.expect(std.mem.indexOf(u8, manifest, "\"./sdk/text.ts\"") != null);
479
+ try std.testing.expect(std.mem.indexOf(u8, manifest, "\"./events\"") != null);
480
+ try std.testing.expect(std.mem.indexOf(u8, manifest, "\"./sdk/events.ts\"") != null);
481
+ for (editor_package_files[1..]) |sub_path| {
482
+ var path_buffer: [128]u8 = undefined;
483
+ const path = try std.fmt.bufPrint(&path_buffer, "packages/core/{s}", .{sub_path});
484
+ try std.testing.expect(buildgraph.fileExists(io, path));
485
+ }
486
+ }
@@ -11,6 +11,7 @@
11
11
 
12
12
  const std = @import("std");
13
13
  const buildgraph = @import("buildgraph.zig");
14
+ const ts_core = @import("ts_core.zig");
14
15
  const toolchain = @import("toolchain.zig");
15
16
  const manifest_tool = @import("manifest.zig");
16
17
  const dev_tool = @import("dev.zig");
@@ -50,6 +51,22 @@ pub fn run(allocator: std.mem.Allocator, io: std.Io, verb: Verb, options: Option
50
51
  }
51
52
  const metadata = try manifest_tool.readMetadata(allocator, io, "app.zon");
52
53
 
54
+ // Tree detection happens here too (the build graph re-derives it), so
55
+ // a both-cores tree fails with one clean teaching message before any
56
+ // zig invocation.
57
+ const core_tree = ts_core.detect(io);
58
+ if (core_tree == .both) return ts_core.failBothCores();
59
+ if (core_tree == .ts) {
60
+ // Keep the app's editor surface fresh (node_modules/@native-sdk/core,
61
+ // the pre-publish copy stock tsc resolves): best-effort by design —
62
+ // build truth never depends on it, so an unresolvable framework here
63
+ // is not this verb's failure (the graph below reports its own).
64
+ if (buildgraph.resolveFrameworkRoot(allocator, io, options.base_env) catch null) |framework_root| {
65
+ defer allocator.free(framework_root);
66
+ ts_core.selfHealEditorPackage(allocator, io, framework_root);
67
+ }
68
+ }
69
+
53
70
  const zig_exe = try toolchain.resolveZig(allocator, io, options.base_env, .{ .assume_yes = options.assume_yes });
54
71
  defer allocator.free(zig_exe);
55
72