Skip to content

Commit d3bb0af

Browse files
committed
cli: correctly error for missing output directories
1 parent 12a51ee commit d3bb0af

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/main.zig

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,16 @@ const Emit = union(enum) {
712712
.{ @tagName(reason), path },
713713
),
714714
}
715-
} else .{ .yes_path = path },
715+
} else e: {
716+
// If there's a dirname, check that dir exists. This will give a more descriptive error than `Compilation` otherwise would.
717+
if (fs.path.dirname(path)) |dir_path| {
718+
var dir = fs.cwd().openDir(dir_path, .{}) catch |err| {
719+
fatal("unable to open output directory '{s}': {s}", .{ dir_path, @errorName(err) });
720+
};
721+
dir.close();
722+
}
723+
break :e .{ .yes_path = path };
724+
},
716725
};
717726
}
718727
};
@@ -3220,7 +3229,16 @@ fn buildOutputType(
32203229
.yes => |path| if (output_to_cache != null) {
32213230
assert(output_to_cache == .listen); // there was an explicit bin path
32223231
fatal("--listen incompatible with explicit output path '{s}'", .{path});
3223-
} else .{ .yes_path = path },
3232+
} else emit: {
3233+
// If there's a dirname, check that dir exists. This will give a more descriptive error than `Compilation` otherwise would.
3234+
if (fs.path.dirname(path)) |dir_path| {
3235+
var dir = fs.cwd().openDir(dir_path, .{}) catch |err| {
3236+
fatal("unable to open output directory '{s}': {s}", .{ dir_path, @errorName(err) });
3237+
};
3238+
dir.close();
3239+
}
3240+
break :emit .{ .yes_path = path };
3241+
},
32243242
.yes_a_out => emit: {
32253243
assert(output_to_cache == null);
32263244
break :emit .{ .yes_path = switch (target.ofmt) {

0 commit comments

Comments
 (0)