Skip to content

Commit 0ac4097

Browse files
committed
handle when zig path link exists as a non-symlink entry
1 parent cacd43c commit 0ac4097

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

test.zig

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,41 @@ pub fn main() !u8 {
126126
dumpExecResult(result);
127127
try testing.expect(std.mem.eql(u8, result.stdout, "0.7.0\n"));
128128
}
129+
130+
// verify we print a nice error message if we can't update the symlink
131+
// because it's a directory
132+
{
133+
const zig_exe_link = comptime "scratch" ++ sep ++ "bin" ++ sep ++ "zig" ++ builtin.target.exeFileExt();
134+
135+
if (std.fs.cwd().access(zig_exe_link, .{})) {
136+
try std.fs.cwd().deleteFile(zig_exe_link);
137+
} else |err| switch (err) {
138+
error.FileNotFound => {},
139+
else => |e| return e,
140+
}
141+
try std.fs.cwd().makeDir(zig_exe_link);
142+
143+
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{ "default", "0.7.0" });
144+
defer {
145+
allocator.free(result.stdout);
146+
allocator.free(result.stderr);
147+
}
148+
dumpExecResult(result);
149+
switch (result.term) {
150+
.Exited => |code| try testing.expectEqual(@as(u8, 1), code),
151+
else => |term| std.debug.panic("unexpected exit {}", .{term}),
152+
}
153+
if (builtin.os.tag == .windows) {
154+
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "unable to create the exe link, the path '"));
155+
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "' is a directory"));
156+
} else {
157+
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "unable to update/overwrite the 'zig' PATH symlink, the file '"));
158+
try testing.expect(std.mem.containsAtLeast(u8, result.stderr, 1, "' already exists and is not a symlink"));
159+
}
160+
161+
try std.fs.cwd().deleteDir(zig_exe_link);
162+
}
163+
129164
{
130165
const result = try runCaptureOuts(allocator, zigup_args ++ &[_][]const u8{ "fetch", "0.7.0" });
131166
defer {

zigup.zig

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,13 @@ pub fn loggyUpdateSymlink(target_path: []const u8, sym_link_path: []const u8, fl
463463
try std.os.unlink(sym_link_path);
464464
} else |e| switch (e) {
465465
error.FileNotFound => {},
466+
error.NotLink => {
467+
std.debug.print(
468+
"unable to update/overwrite the 'zig' PATH symlink, the file '{s}' already exists and is not a symlink\n",
469+
.{ sym_link_path},
470+
);
471+
std.os.exit(1);
472+
},
466473
else => return e,
467474
}
468475
try loggySymlinkAbsolute(target_path, sym_link_path, flags);
@@ -865,7 +872,16 @@ fn createExeLink(link_target: []const u8, path_link: []const u8) !void {
865872
std.debug.print("Error: path_link (size {}) is too large (max {})\n", .{ path_link.len, std.fs.MAX_PATH_BYTES });
866873
return error.AlreadyReported;
867874
}
868-
const file = try std.fs.cwd().createFile(path_link, .{});
875+
const file = std.fs.cwd().createFile(path_link, .{}) catch |err| switch (err) {
876+
error.IsDir => {
877+
std.debug.print(
878+
"unable to create the exe link, the path '{s}' is a directory\n",
879+
.{ path_link},
880+
);
881+
std.os.exit(1);
882+
},
883+
else => |e| return e,
884+
};
869885
defer file.close();
870886
try file.writer().writeAll(win32exelink.content[0..win32exelink.exe_offset]);
871887
try file.writer().writeAll(link_target);

0 commit comments

Comments
 (0)