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(...@@ -1811,22 +1811,72 @@ fn buildOutputType(
1811 };1811 };
18121812
1813 const default_h_basename = try std.fmt.allocPrint(arena, "{s}.h", .{root_name});1813 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 };
1815 defer emit_h_resolved.deinit();1825 defer emit_h_resolved.deinit();
18161826
1817 const default_asm_basename = try std.fmt.allocPrint(arena, "{s}.s", .{root_name});1827 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 };
1819 defer emit_asm_resolved.deinit();1839 defer emit_asm_resolved.deinit();
18201840
1821 const default_llvm_ir_basename = try std.fmt.allocPrint(arena, "{s}.ll", .{root_name});1841 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 };
1823 defer emit_llvm_ir_resolved.deinit();1853 defer emit_llvm_ir_resolved.deinit();
18241854
1825 const default_analysis_basename = try std.fmt.allocPrint(arena, "{s}-analysis.json", .{root_name});1855 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 };
1827 defer emit_analysis_resolved.deinit();1867 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 };
1830 defer emit_docs_resolved.deinit();1880 defer emit_docs_resolved.deinit();
18311881
1832 const root_pkg: ?*Package = if (root_src_file) |src_path| blk: {1882 const root_pkg: ?*Package = if (root_src_file) |src_path| blk: {
...@@ -1849,9 +1899,11 @@ fn buildOutputType(...@@ -1849,9 +1899,11 @@ fn buildOutputType(
1849 const self_exe_path = try fs.selfExePathAlloc(arena);1899 const self_exe_path = try fs.selfExePathAlloc(arena);
1850 var zig_lib_directory: Compilation.Directory = if (override_lib_dir) |lib_dir| .{1900 var zig_lib_directory: Compilation.Directory = if (override_lib_dir) |lib_dir| .{
1851 .path = lib_dir,1901 .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 },
1853 } else introspect.findZigLibDirFromSelfExe(arena, self_exe_path) catch |err| {1905 } 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)});
1855 };1907 };
1856 defer zig_lib_directory.handle.close();1908 defer zig_lib_directory.handle.close();
18571909
...@@ -2496,7 +2548,9 @@ fn cmdTranslateC(comp: *Compilation, arena: *Allocator, enable_cache: bool) !voi...@@ -2496,7 +2548,9 @@ fn cmdTranslateC(comp: *Compilation, arena: *Allocator, enable_cache: bool) !voi
2496 return cleanExit();2548 return cleanExit();
2497 } else {2549 } else {
2498 const out_zig_path = try fs.path.join(arena, &[_][]const u8{ "o", &digest, translated_zig_basename });2550 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 };
2500 defer zig_file.close();2554 defer zig_file.close();
2501 try io.getStdOut().writeFileAll(zig_file, .{});2555 try io.getStdOut().writeFileAll(zig_file, .{});
2502 return cleanExit();2556 return cleanExit();
...@@ -2606,7 +2660,9 @@ pub fn cmdInit(...@@ -2606,7 +2660,9 @@ pub fn cmdInit(
2606 .Lib => "std" ++ s ++ "special" ++ s ++ "init-lib",2660 .Lib => "std" ++ s ++ "special" ++ s ++ "init-lib",
2607 .Exe => "std" ++ s ++ "special" ++ s ++ "init-exe",2661 .Exe => "std" ++ s ++ "special" ++ s ++ "init-exe",
2608 };2662 };
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 };
2610 defer template_dir.close();2666 defer template_dir.close();
26112667
2612 const cwd_path = try process.getCwdAlloc(arena);2668 const cwd_path = try process.getCwdAlloc(arena);
...@@ -2721,9 +2777,11 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v...@@ -2721,9 +2777,11 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
27212777
2722 var zig_lib_directory: Compilation.Directory = if (override_lib_dir) |lib_dir| .{2778 var zig_lib_directory: Compilation.Directory = if (override_lib_dir) |lib_dir| .{
2723 .path = lib_dir,2779 .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 },
2725 } else introspect.findZigLibDirFromSelfExe(arena, self_exe_path) catch |err| {2783 } 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) });
2727 };2785 };
2728 defer zig_lib_directory.handle.close();2786 defer zig_lib_directory.handle.close();
27292787
...@@ -2733,7 +2791,9 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v...@@ -2733,7 +2791,9 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
2733 var root_pkg: Package = .{2791 var root_pkg: Package = .{
2734 .root_src_directory = .{2792 .root_src_directory = .{
2735 .path = special_dir_path,2793 .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 },
2737 },2797 },
2738 .root_src_path = "build_runner.zig",2798 .root_src_path = "build_runner.zig",
2739 };2799 };
...@@ -2747,7 +2807,9 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v...@@ -2747,7 +2807,9 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
2747 const build_directory: Compilation.Directory = blk: {2807 const build_directory: Compilation.Directory = blk: {
2748 if (build_file) |bf| {2808 if (build_file) |bf| {
2749 if (fs.path.dirname(bf)) |dirname| {2809 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 };
2751 cleanup_build_dir = dir;2813 cleanup_build_dir = dir;
2752 break :blk .{ .path = dirname, .handle = dir };2814 break :blk .{ .path = dirname, .handle = dir };
2753 }2815 }
...@@ -2759,7 +2821,9 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v...@@ -2759,7 +2821,9 @@ pub fn cmdBuild(gpa: *Allocator, arena: *Allocator, args: []const []const u8) !v
2759 while (true) {2821 while (true) {
2760 const joined_path = try fs.path.join(arena, &[_][]const u8{ dirname, build_zig_basename });2822 const joined_path = try fs.path.join(arena, &[_][]const u8{ dirname, build_zig_basename });
2761 if (fs.cwd().access(joined_path, .{})) |_| {2823 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 };
2763 break :blk .{ .path = dirname, .handle = dir };2827 break :blk .{ .path = dirname, .handle = dir };
2764 } else |err| switch (err) {2828 } else |err| switch (err) {
2765 error.FileNotFound => {2829 error.FileNotFound => {
...@@ -3805,7 +3869,9 @@ pub fn cmdAstCheck(...@@ -3805,7 +3869,9 @@ pub fn cmdAstCheck(
3805 .root_decl = null,3869 .root_decl = null,
3806 };3870 };
3807 if (zig_source_file) |file_name| {3871 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 };
3809 defer f.close();3875 defer f.close();
38103876
3811 const stat = try f.stat();3877 const stat = try f.stat();
...@@ -3925,7 +3991,9 @@ pub fn cmdChangelist(...@@ -3925,7 +3991,9 @@ pub fn cmdChangelist(
3925 const old_source_file = args[0];3991 const old_source_file = args[0];
3926 const new_source_file = args[1];3992 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 };
3929 defer f.close();3997 defer f.close();
39303998
3931 const stat = try f.stat();3999 const stat = try f.stat();
...@@ -3981,7 +4049,9 @@ pub fn cmdChangelist(...@@ -3981,7 +4049,9 @@ pub fn cmdChangelist(
3981 process.exit(1);4049 process.exit(1);
3982 }4050 }
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 };
3985 defer new_f.close();4055 defer new_f.close();
39864056
3987 const new_stat = try new_f.stat();4057 const new_stat = try new_f.stat();