@native-sdk/cli 0.4.2 → 0.4.3

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.
@@ -37,7 +37,7 @@ pub fn RuntimeViewCanvasWidgetText(comptime RuntimeView: type) type {
37
37
  if (canvasWidgetTextEditUnchanged(current_state, next_state)) return null;
38
38
 
39
39
  try self.rewriteCanvasWidgetTextStorage(index, next_state);
40
- self.scrollCanvasTextareaCaretIntoView(index);
40
+ self.scrollCanvasTextInputCaretIntoView(index);
41
41
  const semantics = try self.widgetLayoutTree().collectSemantics(&self.widget_semantics_nodes);
42
42
  self.widget_semantics_node_count = semantics.len;
43
43
  self.widget_revision += 1;
@@ -98,6 +98,10 @@ pub fn RuntimeViewCanvasWidgetText(comptime RuntimeView: type) type {
98
98
 
99
99
  self.widget_layout_nodes[index].widget.text_selection = next_selection;
100
100
  self.widget_layout_nodes[index].widget.text_composition = null;
101
+ // A pointer-placed caret is a caret change like any other: a
102
+ // drag past a scrolled single-line field's edge lands on an
103
+ // off-screen offset, and the field follows it.
104
+ if (canvasWidgetSingleLineTextKind(widget.kind)) self.scrollCanvasTextInputCaretIntoView(index);
101
105
  try self.refreshCanvasWidgetSemantics();
102
106
  self.widget_revision += 1;
103
107
  return self.canvasWidgetDirtyBounds(index, widget.frame);
@@ -266,7 +270,7 @@ pub fn RuntimeViewCanvasWidgetText(comptime RuntimeView: type) type {
266
270
  .selection = canvas.TextSelection.collapsed(text.len),
267
271
  .composition = null,
268
272
  });
269
- self.scrollCanvasTextareaCaretIntoView(index);
273
+ self.scrollCanvasTextInputCaretIntoView(index);
270
274
  try self.refreshCanvasWidgetSemantics();
271
275
  self.widget_revision += 1;
272
276
  return self.canvasWidgetDirtyBounds(index, self.widget_layout_nodes[index].frame);
@@ -17,14 +17,46 @@
17
17
 
18
18
  const std = @import("std");
19
19
  const templates = @import("templates.zig");
20
+ const junction = @import("junction.zig");
20
21
 
21
22
  pub const generated_dir = ".native/build";
22
23
 
24
+ /// Where the cross-volume junction lives, relative to the app dir: a
25
+ /// directory junction `.native/sdk` -> framework root lets the generated
26
+ /// build graph reference an SDK on another Windows volume through a
27
+ /// relative zon path (`../sdk` from the `.native/build` build root), since
28
+ /// build.zig.zon rejects absolute paths and no `..` chain crosses volumes.
29
+ pub const sdk_junction_dir = ".native/sdk";
30
+ const junction_dependency_path = "../sdk";
31
+
23
32
  pub const Error = error{
24
33
  MissingFramework,
25
34
  AlreadyEjected,
35
+ CrossVolumeFramework,
36
+ };
37
+
38
+ /// How the framework path dependency reaches the zon file. Pure decision
39
+ /// over the `std.fs.path.relative` result, so the cross-volume case is
40
+ /// testable with fabricated drive-letter paths on every host.
41
+ pub const DependencyRoute = union(enum) {
42
+ /// Same volume (or any non-Windows host): the computed relative path
43
+ /// goes into the zon verbatim, exactly as before.
44
+ relative: []const u8,
45
+ /// The framework sits on another Windows volume, where no relative
46
+ /// path exists: the zon gets `../sdk` and the caller must create or
47
+ /// refresh the `.native/sdk` junction.
48
+ junction,
26
49
  };
27
50
 
51
+ pub fn routeDependencyPath(dependency_path: []const u8) DependencyRoute {
52
+ if (junction.crossesVolumes(dependency_path)) return .junction;
53
+ return .{ .relative = if (dependency_path.len == 0) "." else dependency_path };
54
+ }
55
+
56
+ /// The two user-level ways out when the junction bridge is unavailable
57
+ /// (appended to every cross-volume teaching error).
58
+ pub const cross_volume_ways_out = junction.cross_volume_ways_out;
59
+
28
60
  /// Where the `native_sdk` framework checkout lives, for wiring the path
29
61
  /// dependency of a generated or ejected build graph. Resolution order:
30
62
  /// 1. NATIVE_SDK_PATH environment variable (explicit override; the npm
@@ -112,9 +144,40 @@ pub fn ensureGeneratedBuild(allocator: std.mem.Allocator, io: std.Io, app_dir: [
112
144
  const dependency_path = try std.fs.path.relative(allocator, cwd, null, gen_real, framework_real);
113
145
  defer allocator.free(dependency_path);
114
146
 
147
+ const zon_dependency_path: []const u8 = switch (routeDependencyPath(dependency_path)) {
148
+ .relative => |path| path,
149
+ .junction => bridge: {
150
+ // Cross-volume (drive letters or UNC shares): wire the zon
151
+ // through a `.native/sdk` junction instead of the absolute
152
+ // path Zig rejects. Refreshed on every generation, so an SDK
153
+ // that moved or was reinstalled elsewhere is retargeted here.
154
+ const native_dir_real = std.fs.path.dirname(gen_real).?;
155
+ const app_real = std.fs.path.dirname(native_dir_real) orelse native_dir_real;
156
+ const junction_path = try std.fs.path.join(allocator, &.{ native_dir_real, "sdk" });
157
+ defer allocator.free(junction_path);
158
+ junction.ensure(allocator, io, junction_path, framework_real) catch |err| switch (err) {
159
+ error.OutOfMemory, error.Canceled => |e| return e,
160
+ else => {
161
+ std.debug.print(
162
+ \\cannot wire this app to the Native SDK: the app ({s})
163
+ \\and the SDK ({s})
164
+ \\sit on different Windows volumes, and build.zig.zon path
165
+ \\dependencies must be relative — no relative path crosses
166
+ \\volumes. The CLI bridges that with a directory junction at
167
+ \\{s}, but creating it failed ({t}).
168
+ \\
169
+ , .{ app_real, framework_real, sdk_junction_dir, err });
170
+ std.debug.print(cross_volume_ways_out, .{});
171
+ return error.CrossVolumeFramework;
172
+ },
173
+ };
174
+ break :bridge junction_dependency_path;
175
+ },
176
+ };
177
+
115
178
  const build_zig = try renderBuildZig(allocator, options.app_name, .generated);
116
179
  defer allocator.free(build_zig);
117
- const build_zon = try renderBuildZon(allocator, options.app_name, if (dependency_path.len == 0) "." else dependency_path, .generated);
180
+ const build_zon = try renderBuildZon(allocator, options.app_name, zon_dependency_path, .generated);
118
181
  defer allocator.free(build_zon);
119
182
 
120
183
  var dir = try std.Io.Dir.cwd().openDir(io, gen_path, .{});
@@ -149,9 +212,30 @@ pub fn eject(allocator: std.mem.Allocator, io: std.Io, app_dir: []const u8, opti
149
212
  const dependency_path = try std.fs.path.relative(allocator, cwd, null, app_real, framework_real);
150
213
  defer allocator.free(dependency_path);
151
214
 
215
+ const zon_dependency_path: []const u8 = switch (routeDependencyPath(dependency_path)) {
216
+ .relative => |path| path,
217
+ // No junction bridge for eject: the ejected build belongs to the
218
+ // user and is driven by plain `zig build`, so the CLI would never
219
+ // refresh a `.native/sdk` junction again — it would silently rot
220
+ // the first time the SDK moves. Teach the constraint instead.
221
+ .junction => {
222
+ std.debug.print(
223
+ \\cannot eject: the app ({s})
224
+ \\and the Native SDK ({s})
225
+ \\sit on different Windows volumes, and the ejected
226
+ \\build.zig.zon needs a relative SDK path — no relative path
227
+ \\crosses volumes, and an ejected build is user-owned, so the
228
+ \\CLI cannot keep a junction bridge fresh for it.
229
+ \\
230
+ , .{ app_real, framework_real });
231
+ std.debug.print(cross_volume_ways_out, .{});
232
+ return error.CrossVolumeFramework;
233
+ },
234
+ };
235
+
152
236
  const build_zig = try renderBuildZig(allocator, options.app_name, .ejected);
153
237
  defer allocator.free(build_zig);
154
- const build_zon = try renderBuildZon(allocator, options.app_name, if (dependency_path.len == 0) "." else dependency_path, .ejected);
238
+ const build_zon = try renderBuildZon(allocator, options.app_name, zon_dependency_path, .ejected);
155
239
  defer allocator.free(build_zon);
156
240
 
157
241
  try dir.writeFile(io, .{ .sub_path = "build.zig", .data = build_zig });
@@ -283,6 +367,32 @@ test "generated build.zig.zon wires the framework path dependency" {
283
367
  try std.testing.expect(std.mem.indexOf(u8, text, ".paths = .{ \"build.zig\", \"build.zig.zon\" }") != null);
284
368
  }
285
369
 
370
+ test "dependency routing keeps relative paths and bridges cross-volume ones" {
371
+ // Same volume (and every non-Windows host): verbatim, zero change.
372
+ try std.testing.expectEqualStrings("../../../framework", routeDependencyPath("../../../framework").relative);
373
+ try std.testing.expectEqualStrings("..\\..\\sdk", routeDependencyPath("..\\..\\sdk").relative);
374
+ // Build root == framework root: relative() returns "", the zon gets ".".
375
+ try std.testing.expectEqualStrings(".", routeDependencyPath("").relative);
376
+ // Cross-volume: std.fs.path.relative degrades to the absolute target
377
+ // (drive letters or UNC shares), which must route through the junction.
378
+ try std.testing.expect(routeDependencyPath("C:\\Users\\alpha\\AppData\\Roaming\\npm\\node_modules\\@native-sdk\\cli") == .junction);
379
+ try std.testing.expect(routeDependencyPath("\\\\server\\share\\native-sdk") == .junction);
380
+ }
381
+
382
+ test "the junction route renders a relative zon path through .native/sdk" {
383
+ const text = try renderBuildZon(std.testing.allocator, "my-app", junction_dependency_path, .generated);
384
+ defer std.testing.allocator.free(text);
385
+ try std.testing.expect(std.mem.indexOf(u8, text, ".native_sdk = .{ .path = \"../sdk\" }") != null);
386
+ // Never an absolute path in the zon, junction route included.
387
+ try std.testing.expect(std.mem.indexOf(u8, text, ".path = \"C:") == null);
388
+ try std.testing.expect(std.mem.indexOf(u8, text, ".path = \"\\\\") == null);
389
+ }
390
+
391
+ test "the cross-volume teaching text names both user-level ways out" {
392
+ try std.testing.expect(std.mem.indexOf(u8, cross_volume_ways_out, "same volume") != null);
393
+ try std.testing.expect(std.mem.indexOf(u8, cross_volume_ways_out, "npm config set prefix") != null);
394
+ }
395
+
286
396
  /// Path equality where '/' in the expected value also matches the
287
397
  /// platform separator, so tests written with forward slashes hold on
288
398
  /// Windows (where std.fs.path.join emits backslashes).
@@ -0,0 +1,247 @@
1
+ //! Windows directory-junction shim for cross-volume SDK wiring.
2
+ //!
3
+ //! build.zig.zon path dependencies must be relative to the build root (Zig
4
+ //! rejects absolute paths), but on Windows a relative path only exists when
5
+ //! both ends share a volume: no chain of `..` segments leads from an app on
6
+ //! D:\ to an SDK under the npm global prefix on C:\, nor between two
7
+ //! different \\server\share UNC roots. `std.fs.path.relative` signals that
8
+ //! case by returning the canonicalized absolute target instead of a
9
+ //! relative path. The bridge is a directory junction inside the app's
10
+ //! `.native/` directory pointing at the framework root, so the zon
11
+ //! dependency can stay relative by routing through it. Junctions — mount
12
+ //! points, not file symlinks — resolve on the filesystem like plain
13
+ //! directories and, unlike symlinks, need neither Administrator rights nor
14
+ //! Developer Mode to create, which is why this shim sets a MOUNT_POINT
15
+ //! reparse point instead of calling the symlink API.
16
+
17
+ const std = @import("std");
18
+ const builtin = @import("builtin");
19
+
20
+ /// True when a path computed by `std.fs.path.relative` could not actually
21
+ /// be made relative — the Windows cross-volume case (two drive letters, or
22
+ /// two different UNC shares), where the function degrades to the absolute
23
+ /// target path. Checks both path flavors so the decision is testable with
24
+ /// fabricated `C:\`/`D:\` strings on every host.
25
+ pub fn crossesVolumes(dependency_path: []const u8) bool {
26
+ return std.fs.path.isAbsoluteWindows(dependency_path) or
27
+ std.fs.path.isAbsolutePosix(dependency_path);
28
+ }
29
+
30
+ /// The two user-level ways out when the cross-volume junction bridge is
31
+ /// unavailable or does not apply (appended to every cross-volume teaching
32
+ /// error, so no failure ever ends at "absolute paths are rejected" without
33
+ /// a way forward).
34
+ pub const cross_volume_ways_out =
35
+ \\Two ways out:
36
+ \\ - keep the app on the same volume as the Native SDK install, or
37
+ \\ - move the npm global prefix onto the app's volume
38
+ \\ (`npm config set prefix <dir on the app's volume>`, then
39
+ \\ reinstall @native-sdk/cli there).
40
+ \\
41
+ ;
42
+
43
+ pub const EnsureError = error{
44
+ /// Junctions are a Windows (NTFS reparse point) concept; on other
45
+ /// platforms a genuinely relative path always exists and this shim is
46
+ /// never needed.
47
+ UnsupportedPlatform,
48
+ /// The reparse point could not be created (exotic filesystem, policy).
49
+ /// Callers must turn this into a teaching error, never into an
50
+ /// absolute zon path.
51
+ JunctionFailed,
52
+ /// Something that is not a junction (a non-empty real directory)
53
+ /// occupies the junction path; refusing beats silently deleting a tree
54
+ /// the CLI does not own.
55
+ JunctionPathOccupied,
56
+ } || std.mem.Allocator.Error || std.Io.Cancelable;
57
+
58
+ /// Make `junction_path` a directory junction resolving to `target` (both
59
+ /// absolute, symlink-free paths — realpath output). Idempotent per
60
+ /// generation: a junction already resolving to `target` is left untouched
61
+ /// (so repeated `native dev|build|test` runs are no-ops), one resolving
62
+ /// elsewhere (the SDK moved or was upgraded in place) or dangling (its old
63
+ /// target was deleted) is removed and recreated.
64
+ pub fn ensure(allocator: std.mem.Allocator, io: std.Io, junction_path: []const u8, target: []const u8) EnsureError!void {
65
+ if (builtin.os.tag != .windows) return error.UnsupportedPlatform;
66
+
67
+ const cwd = std.Io.Dir.cwd();
68
+ if (cwd.realPathFileAlloc(io, junction_path, allocator)) |resolved| {
69
+ defer allocator.free(resolved);
70
+ // Windows paths are case-insensitive; compare that way so a mere
71
+ // case difference between two realpath spellings never causes a
72
+ // delete/recreate cycle on every generation.
73
+ if (std.ascii.eqlIgnoreCase(resolved, target)) return;
74
+ } else |err| switch (err) {
75
+ error.Canceled => |e| return e,
76
+ // Missing entirely, or a junction whose target no longer exists
77
+ // (realpath cannot resolve through it) — either way, fall through
78
+ // and clear the name so creation below starts fresh.
79
+ else => {},
80
+ }
81
+
82
+ remove(io, junction_path) catch |err| switch (err) {
83
+ error.JunctionNotFound => {},
84
+ else => |e| return e,
85
+ };
86
+
87
+ create(io, junction_path, target) catch |err| switch (err) {
88
+ error.Canceled => |e| return e,
89
+ else => {
90
+ // A half-made junction is a plain empty directory; clear it so
91
+ // the next attempt (or a user retry) starts from nothing.
92
+ cwd.deleteDir(io, junction_path) catch {};
93
+ return error.JunctionFailed;
94
+ },
95
+ };
96
+ }
97
+
98
+ /// Delete whatever occupies `junction_path` without ever touching a
99
+ /// junction's target: RemoveDirectory deletes the reparse point itself
100
+ /// (dangling or not) and also clears a plain empty directory, while a
101
+ /// non-empty real directory fails DirNotEmpty — surfaced as occupied
102
+ /// rather than destroying data the CLI does not own.
103
+ fn remove(io: std.Io, junction_path: []const u8) error{ JunctionNotFound, JunctionPathOccupied, Canceled }!void {
104
+ const cwd = std.Io.Dir.cwd();
105
+ cwd.deleteDir(io, junction_path) catch |err| switch (err) {
106
+ error.FileNotFound => return error.JunctionNotFound,
107
+ error.Canceled => |e| return e,
108
+ // A regular file squatting on the name: clear it the file way.
109
+ error.NotDir => cwd.deleteFile(io, junction_path) catch |file_err| switch (file_err) {
110
+ error.FileNotFound => return error.JunctionNotFound,
111
+ error.Canceled => |e| return e,
112
+ else => return error.JunctionPathOccupied,
113
+ },
114
+ else => return error.JunctionPathOccupied,
115
+ };
116
+ }
117
+
118
+ /// Create the junction: make the directory and set a MOUNT_POINT reparse
119
+ /// point on it in one NtCreateFile + FSCTL_SET_REPARSE_POINT pair, the same
120
+ /// operation `mklink /J` performs. The substitute name is the NT-namespaced
121
+ /// target (`\??\C:\...`), the print name its Win32 spelling for `dir`
122
+ /// output.
123
+ fn create(io: std.Io, junction_path: []const u8, target: []const u8) !void {
124
+ if (builtin.os.tag != .windows) return error.UnsupportedPlatform;
125
+ const w = std.os.windows;
126
+
127
+ const junction_nt = try std.Io.Threaded.sliceToPrefixedFileW(null, junction_path, .{ .allow_relative = false });
128
+ const target_nt = try std.Io.Threaded.sliceToPrefixedFileW(null, target, .{ .allow_relative = false });
129
+ const substitute = target_nt.span();
130
+
131
+ var print_buffer: [w.PATH_MAX_WIDE]u16 = undefined;
132
+ // An empty print name is valid reparse data; the junction still
133
+ // resolves, it just lists without a pretty target in Explorer/`dir`.
134
+ const print_name: []const u16 = w.ntToWin32Namespace(substitute, &print_buffer) catch &.{};
135
+
136
+ // REPARSE_DATA_BUFFER header for IO_REPARSE_TAG_MOUNT_POINT, followed
137
+ // by PathBuffer holding substitute + NUL + print + NUL (WTF-16).
138
+ const MountPointData = extern struct {
139
+ ReparseTag: w.IO_REPARSE_TAG,
140
+ ReparseDataLength: w.USHORT,
141
+ Reserved: w.USHORT,
142
+ SubstituteNameOffset: w.USHORT,
143
+ SubstituteNameLength: w.USHORT,
144
+ PrintNameOffset: w.USHORT,
145
+ PrintNameLength: w.USHORT,
146
+ };
147
+ var buffer: [w.MAXIMUM_REPARSE_DATA_BUFFER_SIZE]u8 = undefined;
148
+ const substitute_bytes = substitute.len * 2;
149
+ const print_bytes = print_name.len * 2;
150
+ const total_len = @sizeOf(MountPointData) + substitute_bytes + 2 + print_bytes + 2;
151
+ if (total_len > buffer.len) return error.NameTooLong;
152
+ const header: MountPointData = .{
153
+ .ReparseTag = .MOUNT_POINT,
154
+ // Everything after the 8-byte (tag + length + reserved) prefix.
155
+ .ReparseDataLength = @intCast(total_len - 8),
156
+ .Reserved = 0,
157
+ .SubstituteNameOffset = 0,
158
+ .SubstituteNameLength = @intCast(substitute_bytes),
159
+ .PrintNameOffset = @intCast(substitute_bytes + 2),
160
+ .PrintNameLength = @intCast(print_bytes),
161
+ };
162
+ @memcpy(buffer[0..@sizeOf(MountPointData)], std.mem.asBytes(&header));
163
+ var offset: usize = @sizeOf(MountPointData);
164
+ @memcpy(buffer[offset..][0..substitute_bytes], std.mem.sliceAsBytes(substitute));
165
+ offset += substitute_bytes;
166
+ buffer[offset] = 0;
167
+ buffer[offset + 1] = 0;
168
+ offset += 2;
169
+ @memcpy(buffer[offset..][0..print_bytes], std.mem.sliceAsBytes(print_name));
170
+ offset += print_bytes;
171
+ buffer[offset] = 0;
172
+ buffer[offset + 1] = 0;
173
+
174
+ // Create the directory and keep a synchronous write handle to it that
175
+ // does not follow reparse points — the handle FSCTL_SET_REPARSE_POINT
176
+ // stamps the mount point onto.
177
+ var handle: w.HANDLE = undefined;
178
+ const attr: w.OBJECT.ATTRIBUTES = .{
179
+ .RootDirectory = null,
180
+ .ObjectName = @constCast(&w.UNICODE_STRING.init(junction_nt.span())),
181
+ };
182
+ var iosb: w.IO_STATUS_BLOCK = undefined;
183
+ switch (w.ntdll.NtCreateFile(
184
+ &handle,
185
+ .{ .GENERIC = .{ .WRITE = true }, .STANDARD = .{ .SYNCHRONIZE = true } },
186
+ &attr,
187
+ &iosb,
188
+ null,
189
+ .{ .NORMAL = true },
190
+ .VALID_FLAGS,
191
+ .CREATE,
192
+ .{ .DIRECTORY_FILE = true, .IO = .SYNCHRONOUS_NONALERT, .OPEN_REPARSE_POINT = true },
193
+ null,
194
+ 0,
195
+ )) {
196
+ .SUCCESS => {},
197
+ .OBJECT_NAME_COLLISION => return error.PathAlreadyExists,
198
+ .ACCESS_DENIED => return error.AccessDenied,
199
+ else => return error.JunctionFailed,
200
+ }
201
+ defer w.CloseHandle(handle);
202
+
203
+ const result = try io.operate(.{ .device_io_control = .{
204
+ .file = .{ .handle = handle, .flags = .{ .nonblocking = false } },
205
+ .code = .SET_REPARSE_POINT,
206
+ .in = buffer[0..total_len],
207
+ } });
208
+ if (result.device_io_control.u.Status != .SUCCESS) return error.JunctionFailed;
209
+ }
210
+
211
+ test "cross-volume detection catches what std.fs.path.relative degrades to" {
212
+ // Same volume: a genuinely relative result, no junction needed.
213
+ try std.testing.expect(!crossesVolumes("../../.."));
214
+ try std.testing.expect(!crossesVolumes("..\\..\\sdk"));
215
+ try std.testing.expect(!crossesVolumes(""));
216
+ // Cross volume on Windows: relative() returns the absolute target.
217
+ try std.testing.expect(crossesVolumes("C:\\Users\\alpha\\AppData\\Roaming\\npm\\node_modules\\@native-sdk\\cli"));
218
+ try std.testing.expect(crossesVolumes("\\\\server\\share\\native-sdk"));
219
+ // POSIX absolute cannot happen from relative(), but stays covered.
220
+ try std.testing.expect(crossesVolumes("/opt/native-sdk"));
221
+ }
222
+
223
+ test "the reported cross-drive layout degrades to an absolute path and is detected" {
224
+ // The exact shape from the field report: app on D:\, npm-global SDK on
225
+ // C:\ — relativeWindows is pure, so this runs on every host.
226
+ const dependency_path = try std.fs.path.relativeWindows(
227
+ std.testing.allocator,
228
+ "D:\\Projects\\my_app",
229
+ null,
230
+ "D:\\Projects\\my_app\\.native\\build",
231
+ "C:\\Users\\alpha\\AppData\\Roaming\\npm\\node_modules\\@native-sdk\\cli",
232
+ );
233
+ defer std.testing.allocator.free(dependency_path);
234
+ try std.testing.expect(std.fs.path.isAbsoluteWindows(dependency_path));
235
+ try std.testing.expect(crossesVolumes(dependency_path));
236
+
237
+ // Control: same drive stays relative and is not routed.
238
+ const same_drive = try std.fs.path.relativeWindows(
239
+ std.testing.allocator,
240
+ "D:\\Projects\\my_app",
241
+ null,
242
+ "D:\\Projects\\my_app\\.native\\build",
243
+ "D:\\tools\\native-sdk",
244
+ );
245
+ defer std.testing.allocator.free(same_drive);
246
+ try std.testing.expect(!crossesVolumes(same_drive));
247
+ }
@@ -9,6 +9,7 @@ pub const dev = @import("dev.zig");
9
9
  pub const cef = @import("cef.zig");
10
10
  pub const web_engine = @import("web_engine.zig");
11
11
  pub const buildgraph = @import("buildgraph.zig");
12
+ pub const junction = @import("junction.zig");
12
13
  pub const embedlib = @import("embedlib.zig");
13
14
  pub const eject_components = @import("eject_components.zig");
14
15
  pub const toolchain = @import("toolchain.zig");
@@ -1,4 +1,5 @@
1
1
  const std = @import("std");
2
+ const junction = @import("junction.zig");
2
3
 
3
4
  /// The SDK's default app icon, rendered from vector source by
4
5
  /// `zig build generate-icon` (tools/generate_app_icon.zig). Embedded so
@@ -3183,13 +3184,71 @@ fn nativeDependencyPath(allocator: std.mem.Allocator, io: std.Io, destination: [
3183
3184
  defer allocator.free(framework_real);
3184
3185
 
3185
3186
  const relative = try std.fs.path.relative(allocator, cwd, null, destination_real, framework_real);
3187
+ errdefer allocator.free(relative);
3188
+ // On Windows, two different volumes (drive letters or UNC shares) have
3189
+ // no relative path between them, and std.fs.path.relative degrades to
3190
+ // the absolute target — which build.zig.zon rejects. The full scaffold's
3191
+ // build files are user-owned from day one, so the CLI cannot bridge with
3192
+ // a junction it would never refresh; teach the constraint instead.
3193
+ if (junction.crossesVolumes(relative)) {
3194
+ std.debug.print(
3195
+ \\cannot scaffold a full-shape app at {s}:
3196
+ \\the Native SDK ({s})
3197
+ \\sits on a different Windows volume, and the scaffolded
3198
+ \\build.zig.zon needs a relative SDK path — no relative path
3199
+ \\crosses volumes.
3200
+ \\
3201
+ , .{ destination_real, framework_real });
3202
+ std.debug.print(junction.cross_volume_ways_out, .{});
3203
+ return error.CrossVolumeFramework;
3204
+ }
3186
3205
  if (relative.len == 0) {
3206
+ // Dupe before freeing `relative`: if the dupe fails, the errdefer
3207
+ // above still owns `relative` and must free it exactly once.
3208
+ const dot = try allocator.dupe(u8, ".");
3187
3209
  allocator.free(relative);
3188
- return allocator.dupe(u8, ".");
3210
+ return dot;
3189
3211
  }
3190
3212
  return relative;
3191
3213
  }
3192
3214
 
3215
+ test "nativeDependencyPath falls back to `.` for a same-directory scaffold" {
3216
+ const io = std.testing.io;
3217
+ const destination = ".zig-cache/test-native-dependency-same-dir";
3218
+ try std.Io.Dir.cwd().createDirPath(io, destination);
3219
+ const destination_real = try std.Io.Dir.cwd().realPathFileAlloc(io, destination, std.testing.allocator);
3220
+ defer std.testing.allocator.free(destination_real);
3221
+
3222
+ const dependency = try nativeDependencyPath(std.testing.allocator, io, destination, destination_real);
3223
+ defer std.testing.allocator.free(dependency);
3224
+ try std.testing.expectEqualStrings(".", dependency);
3225
+ }
3226
+
3227
+ fn expectSameDirDependencyDot(allocator: std.mem.Allocator, io: std.Io, destination: []const u8, framework_path: []const u8) !void {
3228
+ const dependency = try nativeDependencyPath(allocator, io, destination, framework_path);
3229
+ defer allocator.free(dependency);
3230
+ try std.testing.expectEqualStrings(".", dependency);
3231
+ }
3232
+
3233
+ test "nativeDependencyPath survives every allocation-failure point of the `.` fallback" {
3234
+ // Walks every allocation-failure point of the `.` fallback and asserts
3235
+ // no leak and no swallowed OutOfMemory at each index. The fallback's
3236
+ // ownership rule — the errdefer releases the relative path exactly
3237
+ // once, so the fallible dupe must run before the manual free — keeps
3238
+ // this loop clean at the index where the dupe itself fails. (The slice
3239
+ // in this branch is empty, and freeing an empty slice is a no-op, so a
3240
+ // free-then-dupe ordering would not crash here today; the test pins
3241
+ // the ordering so the branch stays safe if it ever handles a non-empty
3242
+ // allocation.)
3243
+ const io = std.testing.io;
3244
+ const destination = ".zig-cache/test-native-dependency-oom";
3245
+ try std.Io.Dir.cwd().createDirPath(io, destination);
3246
+ const destination_real = try std.Io.Dir.cwd().realPathFileAlloc(io, destination, std.testing.allocator);
3247
+ defer std.testing.allocator.free(destination_real);
3248
+
3249
+ try std.testing.checkAllAllocationFailures(std.testing.allocator, expectSameDirDependencyDot, .{ io, destination, destination_real });
3250
+ }
3251
+
3193
3252
  fn appendZigString(out: *std.ArrayList(u8), allocator: std.mem.Allocator, value: []const u8) !void {
3194
3253
  try appendEscapedString(out, allocator, value);
3195
3254
  }