From 8cab0762d86d405f21877045e67bd1858c75e258 Mon Sep 17 00:00:00 2001 From: Jan Philipp Hafer Date: Sat, 28 May 2022 05:06:32 +0200 Subject: [PATCH] place zig-cache directory next to build.zig * search upwards, if no build.zig is found * if this fails or any error occurs, fallback to global zig-cache Closes #11672 --- src/main.zig | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/main.zig b/src/main.zig index 4e24b3d789..7bf08fb759 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2825,13 +2825,28 @@ fn buildOutputType( break :l global_cache_directory; } if (main_pkg) |pkg| { - const cache_dir_path = try pkg.root_src_directory.join(arena, &[_][]const u8{"zig-cache"}); - const dir = try pkg.root_src_directory.handle.makeOpenPath("zig-cache", .{}); - cleanup_local_cache_dir = dir; - break :l .{ - .handle = dir, - .path = cache_dir_path, - }; + // search upwards from cwd until we find directory with build.zig + const cwd_path = try process.getCwdAlloc(arena); + const build_zig = "build.zig"; + const zig_cache = "zig-cache"; + var dirname: []const u8 = cwd_path; + while (true) { + const joined_path = try fs.path.join(arena, &[_][]const u8{ dirname, build_zig }); + if (fs.cwd().access(joined_path, .{})) |_| { + const cache_dir_path = try fs.path.join(arena, &[_][]const u8{ dirname, zig_cache }); + const dir = try pkg.root_src_directory.handle.makeOpenPath(cache_dir_path, .{}); + cleanup_local_cache_dir = dir; + break :l .{ .handle = dir, .path = cache_dir_path }; + } else |err| switch (err) { + error.FileNotFound => { + dirname = fs.path.dirname(dirname) orelse { + break :l global_cache_directory; + }; + continue; + }, + else => break :l global_cache_directory, + } + } } // Otherwise we really don't have a reasonable place to put the local cache directory, // so we utilize the global one.