From c9c7521f4f6763dac8c5910ffcf2451c4c2f60d8 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Mon, 27 Dec 2021 04:27:15 -0800 Subject: Fix edgecase in `bun run` where it would choose a directory instead of a file --- src/which.zig | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/which.zig') diff --git a/src/which.zig b/src/which.zig index 0b8e5f1f8..e6ec2c74d 100644 --- a/src/which.zig +++ b/src/which.zig @@ -6,8 +6,11 @@ fn isValid(buf: *[std.fs.MAX_PATH_BYTES]u8, segment: []const u8, bin: []const u8 std.mem.copy(u8, buf[segment.len + 1 ..], bin); buf[segment.len + 1 + bin.len ..][0] = 0; const filepath = buf[0 .. segment.len + 1 + bin.len :0]; - - std.os.accessZ(filepath, std.os.X_OK) catch return null; + var stat = std.mem.zeroes(std.os.Stat); + // we cannot use access() here even though all we want to do now here is check it is executable + // directories can be considered executable + if (std.c.stat(filepath, &stat) != 0) return null; + if (stat.mode & std.os.S_IXUSR == 0 or stat.mode & std.os.S_IFREG == 0) return null; return @intCast(u16, filepath.len); } -- cgit v1.2.3