zig fetch: enhanced error reporting

* Package: use std.tar diagnostics to give detailed error messages
* std.tar: add diagnostic for unsupported file type
This commit is contained in:
Andrew Kelley 2023-10-02 17:00:45 -07:00
parent ef9966c985
commit 21181181bf
4 changed files with 148 additions and 55 deletions

View File

@ -29,6 +29,10 @@ pub const Options = struct {
file_name: []const u8, file_name: []const u8,
link_name: []const u8, link_name: []const u8,
}, },
unsupported_file_type: struct {
file_name: []const u8,
file_type: Header.FileType,
},
}; };
pub fn deinit(d: *Diagnostics) void { pub fn deinit(d: *Diagnostics) void {
@ -38,6 +42,9 @@ pub const Options = struct {
d.allocator.free(info.file_name); d.allocator.free(info.file_name);
d.allocator.free(info.link_name); d.allocator.free(info.link_name);
}, },
.unsupported_file_type => |info| {
d.allocator.free(info.file_name);
},
} }
} }
d.errors.deinit(d.allocator); d.errors.deinit(d.allocator);
@ -50,6 +57,7 @@ pub const Header = struct {
bytes: *const [512]u8, bytes: *const [512]u8,
pub const FileType = enum(u8) { pub const FileType = enum(u8) {
normal_alias = 0,
normal = '0', normal = '0',
hard_link = '1', hard_link = '1',
symbolic_link = '2', symbolic_link = '2',
@ -105,8 +113,9 @@ pub const Header = struct {
} }
pub fn fileType(header: Header) FileType { pub fn fileType(header: Header) FileType {
const result = @as(FileType, @enumFromInt(header.bytes[156])); const result: FileType = @enumFromInt(header.bytes[156]);
return if (result == @as(FileType, @enumFromInt(0))) .normal else result; if (result == .normal_alias) return .normal;
return result;
} }
fn str(header: Header, start: usize, end: usize) []const u8 { fn str(header: Header, start: usize, end: usize) []const u8 {
@ -268,18 +277,21 @@ pub fn pipeToFileSystem(dir: std.fs.Dir, reader: anytype, options: Options) !voi
const link_name = header.linkName(); const link_name = header.linkName();
dir.symLink(link_name, file_name, .{}) catch |err| { dir.symLink(link_name, file_name, .{}) catch |err| {
if (options.diagnostics) |d| { const d = options.diagnostics orelse return error.UnableToCreateSymLink;
try d.errors.append(d.allocator, .{ .unable_to_create_sym_link = .{ try d.errors.append(d.allocator, .{ .unable_to_create_sym_link = .{
.code = err, .code = err,
.file_name = try d.allocator.dupe(u8, file_name), .file_name = try d.allocator.dupe(u8, file_name),
.link_name = try d.allocator.dupe(u8, link_name), .link_name = try d.allocator.dupe(u8, link_name),
} }); } });
} else {
return error.UnableToCreateSymLink;
}
}; };
}, },
else => return error.TarUnsupportedFileType, else => |file_type| {
const d = options.diagnostics orelse return error.TarUnsupportedFileType;
try d.errors.append(d.allocator, .{ .unsupported_file_type = .{
.file_name = try d.allocator.dupe(u8, unstripped_file_name),
.file_type = file_type,
} });
},
} }
} }
} }

View File

@ -202,7 +202,7 @@ fn renderErrorMessageToWriter(
try counting_stderr.writeAll(": "); try counting_stderr.writeAll(": ");
// This is the length of the part before the error message: // This is the length of the part before the error message:
// e.g. "file.zig:4:5: error: " // e.g. "file.zig:4:5: error: "
const prefix_len = @as(usize, @intCast(counting_stderr.context.bytes_written)); const prefix_len: usize = @intCast(counting_stderr.context.bytes_written);
try ttyconf.setColor(stderr, .reset); try ttyconf.setColor(stderr, .reset);
try ttyconf.setColor(stderr, .bold); try ttyconf.setColor(stderr, .bold);
if (err_msg.count == 1) { if (err_msg.count == 1) {
@ -356,7 +356,7 @@ pub const Wip = struct {
} }
const compile_log_str_index = if (compile_log_text.len == 0) 0 else str: { const compile_log_str_index = if (compile_log_text.len == 0) 0 else str: {
const str = @as(u32, @intCast(wip.string_bytes.items.len)); const str: u32 = @intCast(wip.string_bytes.items.len);
try wip.string_bytes.ensureUnusedCapacity(gpa, compile_log_text.len + 1); try wip.string_bytes.ensureUnusedCapacity(gpa, compile_log_text.len + 1);
wip.string_bytes.appendSliceAssumeCapacity(compile_log_text); wip.string_bytes.appendSliceAssumeCapacity(compile_log_text);
wip.string_bytes.appendAssumeCapacity(0); wip.string_bytes.appendAssumeCapacity(0);
@ -364,8 +364,8 @@ pub const Wip = struct {
}; };
wip.setExtra(0, ErrorMessageList{ wip.setExtra(0, ErrorMessageList{
.len = @as(u32, @intCast(wip.root_list.items.len)), .len = @intCast(wip.root_list.items.len),
.start = @as(u32, @intCast(wip.extra.items.len)), .start = @intCast(wip.extra.items.len),
.compile_log_text = compile_log_str_index, .compile_log_text = compile_log_str_index,
}); });
try wip.extra.appendSlice(gpa, @as([]const u32, @ptrCast(wip.root_list.items))); try wip.extra.appendSlice(gpa, @as([]const u32, @ptrCast(wip.root_list.items)));
@ -385,7 +385,7 @@ pub const Wip = struct {
pub fn addString(wip: *Wip, s: []const u8) !u32 { pub fn addString(wip: *Wip, s: []const u8) !u32 {
const gpa = wip.gpa; const gpa = wip.gpa;
const index = @as(u32, @intCast(wip.string_bytes.items.len)); const index: u32 = @intCast(wip.string_bytes.items.len);
try wip.string_bytes.ensureUnusedCapacity(gpa, s.len + 1); try wip.string_bytes.ensureUnusedCapacity(gpa, s.len + 1);
wip.string_bytes.appendSliceAssumeCapacity(s); wip.string_bytes.appendSliceAssumeCapacity(s);
wip.string_bytes.appendAssumeCapacity(0); wip.string_bytes.appendAssumeCapacity(0);
@ -394,7 +394,7 @@ pub const Wip = struct {
pub fn printString(wip: *Wip, comptime fmt: []const u8, args: anytype) !u32 { pub fn printString(wip: *Wip, comptime fmt: []const u8, args: anytype) !u32 {
const gpa = wip.gpa; const gpa = wip.gpa;
const index = @as(u32, @intCast(wip.string_bytes.items.len)); const index: u32 = @intCast(wip.string_bytes.items.len);
try wip.string_bytes.writer(gpa).print(fmt, args); try wip.string_bytes.writer(gpa).print(fmt, args);
try wip.string_bytes.append(gpa, 0); try wip.string_bytes.append(gpa, 0);
return index; return index;
@ -406,15 +406,15 @@ pub const Wip = struct {
} }
pub fn addErrorMessage(wip: *Wip, em: ErrorMessage) !MessageIndex { pub fn addErrorMessage(wip: *Wip, em: ErrorMessage) !MessageIndex {
return @as(MessageIndex, @enumFromInt(try addExtra(wip, em))); return @enumFromInt(try addExtra(wip, em));
} }
pub fn addErrorMessageAssumeCapacity(wip: *Wip, em: ErrorMessage) MessageIndex { pub fn addErrorMessageAssumeCapacity(wip: *Wip, em: ErrorMessage) MessageIndex {
return @as(MessageIndex, @enumFromInt(addExtraAssumeCapacity(wip, em))); return @enumFromInt(addExtraAssumeCapacity(wip, em));
} }
pub fn addSourceLocation(wip: *Wip, sl: SourceLocation) !SourceLocationIndex { pub fn addSourceLocation(wip: *Wip, sl: SourceLocation) !SourceLocationIndex {
return @as(SourceLocationIndex, @enumFromInt(try addExtra(wip, sl))); return @enumFromInt(try addExtra(wip, sl));
} }
pub fn addReferenceTrace(wip: *Wip, rt: ReferenceTrace) !void { pub fn addReferenceTrace(wip: *Wip, rt: ReferenceTrace) !void {
@ -430,7 +430,7 @@ pub const Wip = struct {
const other_list = other.getMessages(); const other_list = other.getMessages();
// The ensureUnusedCapacity call above guarantees this. // The ensureUnusedCapacity call above guarantees this.
const notes_start = wip.reserveNotes(@as(u32, @intCast(other_list.len))) catch unreachable; const notes_start = wip.reserveNotes(@intCast(other_list.len)) catch unreachable;
for (notes_start.., other_list) |note, message| { for (notes_start.., other_list) |note, message| {
wip.extra.items[note] = @intFromEnum(wip.addOtherMessage(other, message) catch unreachable); wip.extra.items[note] = @intFromEnum(wip.addOtherMessage(other, message) catch unreachable);
} }
@ -455,7 +455,7 @@ pub const Wip = struct {
try wip.extra.ensureUnusedCapacity(wip.gpa, notes_len + try wip.extra.ensureUnusedCapacity(wip.gpa, notes_len +
notes_len * @typeInfo(ErrorBundle.ErrorMessage).Struct.fields.len); notes_len * @typeInfo(ErrorBundle.ErrorMessage).Struct.fields.len);
wip.extra.items.len += notes_len; wip.extra.items.len += notes_len;
return @as(u32, @intCast(wip.extra.items.len - notes_len)); return @intCast(wip.extra.items.len - notes_len);
} }
fn addOtherMessage(wip: *Wip, other: ErrorBundle, msg_index: MessageIndex) !MessageIndex { fn addOtherMessage(wip: *Wip, other: ErrorBundle, msg_index: MessageIndex) !MessageIndex {
@ -510,7 +510,7 @@ pub const Wip = struct {
fn addExtraAssumeCapacity(wip: *Wip, extra: anytype) u32 { fn addExtraAssumeCapacity(wip: *Wip, extra: anytype) u32 {
const fields = @typeInfo(@TypeOf(extra)).Struct.fields; const fields = @typeInfo(@TypeOf(extra)).Struct.fields;
const result = @as(u32, @intCast(wip.extra.items.len)); const result: u32 = @intCast(wip.extra.items.len);
wip.extra.items.len += fields.len; wip.extra.items.len += fields.len;
setExtra(wip, result, extra); setExtra(wip, result, extra);
return result; return result;

View File

@ -285,7 +285,8 @@ pub fn fetchAndAddDependencies(
if (manifest.errors.len > 0) { if (manifest.errors.len > 0) {
const file_path = try directory.join(arena, &.{Manifest.basename}); const file_path = try directory.join(arena, &.{Manifest.basename});
for (manifest.errors) |msg| { for (manifest.errors) |msg| {
try Report.addErrorMessage(ast, file_path, error_bundle, 0, msg); const str = try error_bundle.addString(msg.msg);
try Report.addErrorMessage(&ast, file_path, error_bundle, 0, str, msg.tok, msg.off);
} }
return error.PackageFetchFailed; return error.PackageFetchFailed;
} }
@ -465,20 +466,31 @@ pub const Report = struct {
comptime fmt_string: []const u8, comptime fmt_string: []const u8,
fmt_args: anytype, fmt_args: anytype,
) error{ PackageFetchFailed, OutOfMemory } { ) error{ PackageFetchFailed, OutOfMemory } {
const ast = report.ast orelse main.fatal(fmt_string, fmt_args); const msg = try report.error_bundle.printString(fmt_string, fmt_args);
return failMsg(report, tok, msg);
}
fn failMsg(
report: Report,
tok: std.zig.Ast.TokenIndex,
msg: u32,
) error{ PackageFetchFailed, OutOfMemory } {
const gpa = report.error_bundle.gpa; const gpa = report.error_bundle.gpa;
const file_path = try report.directory.join(gpa, &.{Manifest.basename}); const file_path = try report.directory.join(gpa, &.{Manifest.basename});
defer gpa.free(file_path); defer gpa.free(file_path);
const msg = try std.fmt.allocPrint(gpa, fmt_string, fmt_args); const eb = report.error_bundle;
defer gpa.free(msg);
try addErrorMessage(ast.*, file_path, report.error_bundle, 0, .{ if (report.ast) |ast| {
.tok = tok, try addErrorMessage(ast, file_path, eb, 0, msg, tok, 0);
.off = 0, } else {
.msg = msg, try eb.addRootErrorMessage(.{
}); .msg = msg,
.src_loc = .none,
.notes_len = 0,
});
}
return error.PackageFetchFailed; return error.PackageFetchFailed;
} }
@ -488,31 +500,42 @@ pub const Report = struct {
notes_len: u32, notes_len: u32,
msg: Manifest.ErrorMessage, msg: Manifest.ErrorMessage,
) error{OutOfMemory}!void { ) error{OutOfMemory}!void {
const ast = report.ast orelse main.fatal("{s}", .{msg.msg}); const eb = report.error_bundle;
const gpa = report.error_bundle.gpa; const msg_str = try eb.addString(msg.msg);
const file_path = try report.directory.join(gpa, &.{Manifest.basename}); if (report.ast) |ast| {
defer gpa.free(file_path); const gpa = eb.gpa;
return addErrorMessage(ast.*, file_path, report.error_bundle, notes_len, msg); const file_path = try report.directory.join(gpa, &.{Manifest.basename});
defer gpa.free(file_path);
return addErrorMessage(ast, file_path, eb, notes_len, msg_str, msg.tok, msg.off);
} else {
return eb.addRootErrorMessage(.{
.msg = msg_str,
.src_loc = .none,
.notes_len = notes_len,
});
}
} }
fn addErrorMessage( fn addErrorMessage(
ast: std.zig.Ast, ast: *const std.zig.Ast,
file_path: []const u8, file_path: []const u8,
eb: *std.zig.ErrorBundle.Wip, eb: *std.zig.ErrorBundle.Wip,
notes_len: u32, notes_len: u32,
msg: Manifest.ErrorMessage, msg_str: u32,
msg_tok: std.zig.Ast.TokenIndex,
msg_off: u32,
) error{OutOfMemory}!void { ) error{OutOfMemory}!void {
const token_starts = ast.tokens.items(.start); const token_starts = ast.tokens.items(.start);
const start_loc = ast.tokenLocation(0, msg.tok); const start_loc = ast.tokenLocation(0, msg_tok);
try eb.addRootErrorMessage(.{ try eb.addRootErrorMessage(.{
.msg = try eb.addString(msg.msg), .msg = msg_str,
.src_loc = try eb.addSourceLocation(.{ .src_loc = try eb.addSourceLocation(.{
.src_path = try eb.addString(file_path), .src_path = try eb.addString(file_path),
.span_start = token_starts[msg.tok], .span_start = token_starts[msg_tok],
.span_end = @as(u32, @intCast(token_starts[msg.tok] + ast.tokenSlice(msg.tok).len)), .span_end = @as(u32, @intCast(token_starts[msg_tok] + ast.tokenSlice(msg_tok).len)),
.span_main = token_starts[msg.tok] + msg.off, .span_main = token_starts[msg_tok] + msg_off,
.line = @as(u32, @intCast(start_loc.line)), .line = @intCast(start_loc.line),
.column = @as(u32, @intCast(start_loc.column)), .column = @as(u32, @intCast(start_loc.column)),
.source_line = try eb.addString(ast.source[start_loc.line_start..start_loc.line_end]), .source_line = try eb.addString(ast.source[start_loc.line_start..start_loc.line_end]),
}), }),
@ -752,9 +775,9 @@ pub const ReadableResource = struct {
}; };
switch (try rr.getFileType(dep_location_tok, report)) { switch (try rr.getFileType(dep_location_tok, report)) {
.tar => try unpackTarball(prog_reader.reader(), tmp_directory.handle), .tar => try unpackTarball(allocator, prog_reader.reader(), tmp_directory.handle, dep_location_tok, report),
.@"tar.gz" => try unpackTarballCompressed(allocator, prog_reader, tmp_directory.handle, std.compress.gzip), .@"tar.gz" => try unpackTarballCompressed(allocator, prog_reader, tmp_directory.handle, dep_location_tok, report, std.compress.gzip),
.@"tar.xz" => try unpackTarballCompressed(allocator, prog_reader, tmp_directory.handle, std.compress.xz), .@"tar.xz" => try unpackTarballCompressed(allocator, prog_reader, tmp_directory.handle, dep_location_tok, report, std.compress.xz),
.git_pack => try unpackGitPack(allocator, &prog_reader, git.parseOid(rr.path) catch unreachable, tmp_directory.handle), .git_pack => try unpackGitPack(allocator, &prog_reader, git.parseOid(rr.path) catch unreachable, tmp_directory.handle),
} }
} else { } else {
@ -1128,6 +1151,8 @@ fn unpackTarballCompressed(
gpa: Allocator, gpa: Allocator,
reader: anytype, reader: anytype,
out_dir: fs.Dir, out_dir: fs.Dir,
dep_location_tok: std.zig.Ast.TokenIndex,
report: Report,
comptime Compression: type, comptime Compression: type,
) !void { ) !void {
var br = std.io.bufferedReaderSize(std.crypto.tls.max_ciphertext_record_len, reader); var br = std.io.bufferedReaderSize(std.crypto.tls.max_ciphertext_record_len, reader);
@ -1135,11 +1160,21 @@ fn unpackTarballCompressed(
var decompress = try Compression.decompress(gpa, br.reader()); var decompress = try Compression.decompress(gpa, br.reader());
defer decompress.deinit(); defer decompress.deinit();
return unpackTarball(decompress.reader(), out_dir); return unpackTarball(gpa, decompress.reader(), out_dir, dep_location_tok, report);
} }
fn unpackTarball(reader: anytype, out_dir: fs.Dir) !void { fn unpackTarball(
gpa: Allocator,
reader: anytype,
out_dir: fs.Dir,
dep_location_tok: std.zig.Ast.TokenIndex,
report: Report,
) !void {
var diagnostics: std.tar.Options.Diagnostics = .{ .allocator = gpa };
defer diagnostics.deinit();
try std.tar.pipeToFileSystem(out_dir, reader, .{ try std.tar.pipeToFileSystem(out_dir, reader, .{
.diagnostics = &diagnostics,
.strip_components = 1, .strip_components = 1,
// TODO: we would like to set this to executable_bit_only, but two // TODO: we would like to set this to executable_bit_only, but two
// things need to happen before that: // things need to happen before that:
@ -1148,6 +1183,36 @@ fn unpackTarball(reader: anytype, out_dir: fs.Dir) !void {
// bit on Windows from the ACLs (see the isExecutable function). // bit on Windows from the ACLs (see the isExecutable function).
.mode_mode = .ignore, .mode_mode = .ignore,
}); });
if (diagnostics.errors.items.len > 0) {
const notes_len: u32 = @intCast(diagnostics.errors.items.len);
try report.addErrorWithNotes(notes_len, .{
.tok = dep_location_tok,
.off = 0,
.msg = "unable to unpack tarball",
});
const eb = report.error_bundle;
const notes_start = try eb.reserveNotes(notes_len);
for (diagnostics.errors.items, notes_start..) |item, note_i| {
switch (item) {
.unable_to_create_sym_link => |info| {
eb.extra.items[note_i] = @intFromEnum(try eb.addErrorMessage(.{
.msg = try eb.printString("unable to create symlink from '{s}' to '{s}': {s}", .{
info.file_name, info.link_name, @errorName(info.code),
}),
}));
},
.unsupported_file_type => |info| {
eb.extra.items[note_i] = @intFromEnum(try eb.addErrorMessage(.{
.msg = try eb.printString("file '{s}' has unsupported type '{c}'", .{
info.file_name, @intFromEnum(info.file_type),
}),
}));
},
}
}
return error.InvalidTarball;
}
} }
fn unpackGitPack( fn unpackGitPack(

View File

@ -6610,6 +6610,7 @@ fn cmdFetch(
arena: Allocator, arena: Allocator,
args: []const []const u8, args: []const []const u8,
) !void { ) !void {
const color: Color = .auto;
var opt_url: ?[]const u8 = null; var opt_url: ?[]const u8 = null;
var override_global_cache_dir: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_GLOBAL_CACHE_DIR"); var override_global_cache_dir: ?[]const u8 = try optionalStringEnvVar(arena, "ZIG_GLOBAL_CACHE_DIR");
@ -6651,10 +6652,17 @@ fn cmdFetch(
const root_prog_node = progress.start("Fetch", 0); const root_prog_node = progress.start("Fetch", 0);
defer root_prog_node.end(); defer root_prog_node.end();
var wip_errors: std.zig.ErrorBundle.Wip = undefined;
try wip_errors.init(gpa);
defer wip_errors.deinit();
var report: Package.Report = .{ var report: Package.Report = .{
.ast = null, .ast = null,
.directory = undefined, .directory = .{
.error_bundle = undefined, .handle = fs.cwd(),
.path = null,
},
.error_bundle = &wip_errors,
}; };
var global_cache_directory: Compilation.Directory = l: { var global_cache_directory: Compilation.Directory = l: {
@ -6697,14 +6705,22 @@ fn cmdFetch(
}; };
defer readable_resource.deinit(gpa); defer readable_resource.deinit(gpa);
var package_location = try readable_resource.unpack( var package_location = readable_resource.unpack(
gpa, gpa,
&thread_pool, &thread_pool,
global_cache_directory, global_cache_directory,
0, 0,
report, report,
root_prog_node, root_prog_node,
); ) catch |err| {
if (wip_errors.root_list.items.len > 0) {
var errors = try wip_errors.toOwnedBundle("");
defer errors.deinit(gpa);
errors.renderToStdErr(renderOptions(color));
process.exit(1);
}
fatal("unable to unpack '{s}': {s}", .{ url, @errorName(err) });
};
defer package_location.deinit(gpa); defer package_location.deinit(gpa);
const hex_digest = Package.Manifest.hexDigest(package_location.hash); const hex_digest = Package.Manifest.hexDigest(package_location.hash);