authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-10 16:17:02-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-10 17:00:46-07:00
log21236c015140cbb1061e6161faa8011915d15de3
tree7d4439167a93fdee99bf87f6e170381877a8545d
parentea70a983ba54efe1d73b85d393596dcb98e19c02

CLI: improved local cache directory logic

Previously, when choosing the local cache directory, if there was no root source file, an explicitly chosen path, or other clues, zig would choose cwd + zig-cache/ as the local cache directory. This can be problematic if Zig is invoked with the CWD set to a read-only directory, or a directory unrelated to the actual source files being compiled. In the real world, we see this when using `zig cc` with CGo, which for some reason changes the current working directory to the read-only go standard library path before running the C compiler. This commit conservatively chooses to use the global cache directory as the local cache directory when there is no other reasonable choice, and no longer will rely on the cwd path to choose a local cache directory. As a reminder, the --cache-dir CLI flag and ZIG_LOCAL_CACHE_DIR environment variable are available for overriding the decision. For the zig build system, it will always choose the directory that build.zig is + zig-cache/. Closes #7342

1 files changed, 12 insertions(+), 15 deletions(-)

src/main.zig+12-15
...@@ -1624,21 +1624,18 @@ fn buildOutputType(...@@ -1624,21 +1624,18 @@ fn buildOutputType(
1624 if (arg_mode == .run) {1624 if (arg_mode == .run) {
1625 break :l global_cache_directory;1625 break :l global_cache_directory;
1626 }1626 }
1627 const cache_dir_path = blk: {1627 if (root_pkg) |pkg| {
1628 if (root_pkg) |pkg| {1628 const cache_dir_path = try pkg.root_src_directory.join(arena, &[_][]const u8{"zig-cache"});
1629 if (pkg.root_src_directory.path) |p| {1629 const dir = try pkg.root_src_directory.handle.makeOpenPath("zig-cache", .{});
1630 break :blk try fs.path.join(arena, &[_][]const u8{ p, "zig-cache" });1630 cleanup_local_cache_dir = dir;
1631 }1631 break :l .{
1632 }1632 .handle = dir,
1633 break :blk "zig-cache";1633 .path = cache_dir_path,
1634 };1634 };
1635 const cache_parent_dir = if (root_pkg) |pkg| pkg.root_src_directory.handle else fs.cwd();1635 }
1636 const dir = try cache_parent_dir.makeOpenPath("zig-cache", .{});1636 // Otherwise we really don't have a reasonable place to put the local cache directory,
1637 cleanup_local_cache_dir = dir;1637 // so we utilize the global one.
1638 break :l .{1638 break :l global_cache_directory;
1639 .handle = dir,
1640 .path = cache_dir_path,
1641 };
1642 };1639 };
16431640
1644 if (build_options.have_llvm and emit_asm != .no) {1641 if (build_options.have_llvm and emit_asm != .no) {