authorgravatar for crs2017@gmail.comChristopher Smyth <crs2017@gmail.com> 2021-07-01 15:24:18-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-07-01 22:24:18+03:00
log628f490c59449e38fcc9122968c385997b9e788b
treec55cb40207d11670f42a9929e710be116740ceb0
parentacf2e8fe6484a48cef76c20368ff06fe9d7b264e
signature Signed by PGP key 4AEE18F83AFDEB23

Add context to fatal OpenErrors

Now it also outputs what directory it attempted to open.

1 files changed, 87 insertions(+), 17 deletions(-)

src/main.zig+87-17
......@@ -1811,22 +1811,72 @@ fn buildOutputType(
18111811 };
18121812
18131813 const default_h_basename = try std.fmt.allocPrint(arena, "{s}.h", .{root_name});
1814 var emit_h_resolved = try emit_h.resolve(default_h_basename);
1814 var emit_h_resolved = emit_h.resolve(default_h_basename) catch |err| {
1815 switch (emit_h) {
1816 .yes => {
1817 fatal("unable to open directory from argument 'femit-h', '{s}': {s}", .{ emit_h.yes, @errorName(err) });
1818 },
1819 .yes_default_path => {
1820 fatal("unable to open directory from arguments 'name' or 'soname', '{s}': {s}", .{ default_h_basename, @errorName(err) });
1821 },
1822 .no => unreachable,
1823 }
1824 };
18151825 defer emit_h_resolved.deinit();
18161826
18171827 const default_asm_basename = try std.fmt.allocPrint(arena, "{s}.s", .{root_name});
1818 var emit_asm_resolved = try emit_asm.resolve(default_asm_basename);
1828 var emit_asm_resolved = emit_asm.resolve(default_asm_basename) catch |err| {
1829 switch (emit_asm) {
1830 .yes => {
1831 fatal("unable to open directory from argument 'femit-asm', '{s}': {s}", .{ emit_asm.yes, @errorName(err) });
1832 },
1833 .yes_default_path => {
1834 fatal("unable to open directory from arguments 'name' or 'soname', '{s}': {s}", .{ default_asm_basename, @errorName(err) });
1835 },
1836 .no => unreachable,
1837 }
1838 };
18191839 defer emit_asm_resolved.deinit();
18201840
18211841 const default_llvm_ir_basename = try std.fmt.allocPrint(arena, "{s}.ll", .{root_name});
1822 var emit_llvm_ir_resolved = try emit_llvm_ir.resolve(default_llvm_ir_basename);
1842 var emit_llvm_ir_resolved = emit_llvm_ir.resolve(default_llvm_ir_basename) catch |err| {
1843 switch (emit_llvm_ir) {
1844 .yes => {
1845 fatal("unable to open directory from argument 'femit-llvm-ir', '{s}': {s}", .{ emit_llvm_ir.yes, @errorName(err) });
1846 },
1847 .yes_default_path => {
1848 fatal("unable to open directory from arguments 'name' or 'soname', '{s}': {s}", .{ default_llvm_ir_basename, @errorName(err) });
1849 },
1850 .no => unreachable,
1851 }
1852 };
18231853 defer emit_llvm_ir_resolved.deinit();
18241854
18251855 const default_analysis_basename = try std.fmt.allocPrint(arena, "{s}-analysis.json", .{root_name});
1826 var emit_analysis_resolved = try emit_analysis.resolve(default_analysis_basename);
1856 var emit_analysis_resolved = emit_analysis.resolve(default_analysis_basename) catch |err| {
1857 switch (emit_analysis) {
1858 .yes => {
1859 fatal("unable to open directory from argument 'femit-analysis', '{s}': {s}", .{ emit_analysis.yes, @errorName(err) });
1860 },
1861 .yes_default_path => {
1862 fatal("unable to open directory from arguments 'name' or 'soname', '{s}': {s}", .{ default_analysis_basename, @errorName(err) });
1863 },
1864 .no => unreachable,
1865 }
1866 };
18271867 defer emit_analysis_resolved.deinit();
18281868
1829 var emit_docs_resolved = try emit_docs.resolve("docs");
1869 var emit_docs_resolved = emit_docs.resolve("docs") catch |err| {
1870 switch (emit_docs) {
1871 .yes => {
1872 fatal("unable to open directory from argument 'femit-docs', '{s}': {s}", .{ emit_h.yes, @errorName(err) });
1873 },
1874 .yes_default_path => {
1875 fatal("unable to open directory 'docs': {s}", .{@errorName(err)});
1876 },
1877 .no => unreachable,
1878 }
1879 };
18301880 defer emit_docs_resolved.deinit();
18311881
18321882 const root_pkg: ?*Package = if (root_src_file) |src_path| blk: {
......@@ -1849,9 +1899,11 @@ fn buildOutputType(
18491899 const self_exe_path = try fs.selfExePathAlloc(arena);
18501900 var zig_lib_directory: Compilation.Directory = if (override_lib_dir) |lib_dir| .{
18511901 .path = lib_dir,
1852 .handle = try fs.cwd().openDir(lib_dir, .{}),
1902 .handle = fs.cwd().openDir(lib_dir, .{}) catch |err| {
1903 fatal("unable to open zig lib directory from 'zig-lib-dir' argument or env, '{s}': {s}", .{ lib_dir, @errorName(err) });
1904 },
18531905 } else introspect.findZigLibDirFromSelfExe(arena, self_exe_path) catch |err| {
1854 fatal("unable to find zig installation directory: {s}", .{@errorName(err)});
1906 fatal("unable to find zig installation directory: {s}\n", .{@errorName(err)});
18551907 };
18561908 defer zig_lib_directory.handle.close();
18571909
......@@ -2496,7 +2548,9 @@ fn cmdTranslateC(comp: *Compilation, arena: *Allocator, enable_cache: bool) !voi
24962548 return cleanExit();
24972549 } else {
24982550 const out_zig_path = try fs.path.join(arena, &[_][]const u8{ "o", &digest, translated_zig_basename });
2499 const zig_file = try comp.local_cache_directory.handle.openFile(out_zig_path, .{});
2551 const zig_file = comp.local_cache_directory.handle.openFile(out_zig_path, .{}) catch |err| {
2552 fatal("unable to open cached translated zig file '{s}{s}{s}': {s}", .{ comp.local_cache_directory.path, fs.path.sep_str, out_zig_path, @errorName(err) });
2553 };
25002554 defer zig_file.close();
25012555 try io.getStdOut().writeFileAll(zig_file, .{});
25022556 return cleanExit();
......@@ -2606,7 +2660,9 @@ pub fn cmdInit(
26062660 .Lib => "std" ++ s ++ "special" ++ s ++ "init-lib",
26072661 .Exe => "std" ++ s ++ "special" ++ s ++ "init-exe",
26082662 };
2609 var template_dir = try zig_lib_directory.handle.openDir(template_sub_path, .{});
2663 var template_dir = zig_lib_directory.handle.openDir(template_sub_path, .{}) catch |err| {
2664 fatal("unable to open zig project template directory '{s}{s}{s}': {s}", .{ zig_lib_directory.path, s, template_sub_path, @errorName(err) });
2665 };
26102666 defer template_dir.close();
26112667
26122668 const cwd_path = try process.getCwdAlloc(arena);
......@@ -2721,9 +2777,11 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
27212777
27222778 var zig_lib_directory: Compilation.Directory = if (override_lib_dir) |lib_dir| .{
27232779 .path = lib_dir,
2724 .handle = try fs.cwd().openDir(lib_dir, .{}),
2780 .handle = fs.cwd().openDir(lib_dir, .{}) catch |err| {
2781 fatal("unable to open zig lib directory from 'zig-lib-dir' argument: '{s}': {s}", .{ lib_dir, @errorName(err) });
2782 },
27252783 } else introspect.findZigLibDirFromSelfExe(arena, self_exe_path) catch |err| {
2726 fatal("unable to find zig installation directory: {s}", .{@errorName(err)});
2784 fatal("unable to find zig installation directory '{s}': {s}", .{ self_exe_path, @errorName(err) });
27272785 };
27282786 defer zig_lib_directory.handle.close();
27292787
......@@ -2733,7 +2791,9 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
27332791 var root_pkg: Package = .{
27342792 .root_src_directory = .{
27352793 .path = special_dir_path,
2736 .handle = try zig_lib_directory.handle.openDir(std_special, .{}),
2794 .handle = zig_lib_directory.handle.openDir(std_special, .{}) catch |err| {
2795 fatal("unable to open directory '{s}{s}{s}': {s}", .{ override_lib_dir, fs.path.sep_str, std_special, @errorName(err) });
2796 },
27372797 },
27382798 .root_src_path = "build_runner.zig",
27392799 };
......@@ -2747,7 +2807,9 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
27472807 const build_directory: Compilation.Directory = blk: {
27482808 if (build_file) |bf| {
27492809 if (fs.path.dirname(bf)) |dirname| {
2750 const dir = try fs.cwd().openDir(dirname, .{});
2810 const dir = fs.cwd().openDir(dirname, .{}) catch |err| {
2811 fatal("unable to open directory to build file from argument 'build-file', '{s}': {s}", .{ dirname, @errorName(err) });
2812 };
27512813 cleanup_build_dir = dir;
27522814 break :blk .{ .path = dirname, .handle = dir };
27532815 }
......@@ -2759,7 +2821,9 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
27592821 while (true) {
27602822 const joined_path = try fs.path.join(arena, &[_][]const u8{ dirname, build_zig_basename });
27612823 if (fs.cwd().access(joined_path, .{})) |_| {
2762 const dir = try fs.cwd().openDir(dirname, .{});
2824 const dir = fs.cwd().openDir(dirname, .{}) catch |err| {
2825 fatal("unable to open directory while searching for build.zig file, '{s}': {s}", .{ dirname, @errorName(err) });
2826 };
27632827 break :blk .{ .path = dirname, .handle = dir };
27642828 } else |err| switch (err) {
27652829 error.FileNotFound => {
......@@ -3805,7 +3869,9 @@ pub fn cmdAstCheck(
38053869 .root_decl = null,
38063870 };
38073871 if (zig_source_file) |file_name| {
3808 var f = try fs.cwd().openFile(file_name, .{});
3872 var f = fs.cwd().openFile(file_name, .{}) catch |err| {
3873 fatal("unable to open file for ast-check '{s}': {s}", .{ file_name, @errorName(err) });
3874 };
38093875 defer f.close();
38103876
38113877 const stat = try f.stat();
......@@ -3925,7 +3991,9 @@ pub fn cmdChangelist(
39253991 const old_source_file = args[0];
39263992 const new_source_file = args[1];
39273993
3928 var f = try fs.cwd().openFile(old_source_file, .{});
3994 var f = fs.cwd().openFile(old_source_file, .{}) catch |err| {
3995 fatal("unable to open old source file for comparison '{s}': {s}", .{ old_source_file, @errorName(err) });
3996 };
39293997 defer f.close();
39303998
39313999 const stat = try f.stat();
......@@ -3981,7 +4049,9 @@ pub fn cmdChangelist(
39814049 process.exit(1);
39824050 }
39834051
3984 var new_f = try fs.cwd().openFile(new_source_file, .{});
4052 var new_f = fs.cwd().openFile(new_source_file, .{}) catch |err| {
4053 fatal("unable to open new source file for comparison '{s}': {s}", .{ new_source_file, @errorName(err) });
4054 };
39854055 defer new_f.close();
39864056
39874057 const new_stat = try new_f.stat();