Skip to content

Commit

Permalink
CLI: improved local cache directory logic
Browse files Browse the repository at this point in the history
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
  • Loading branch information
andrewrk committed Dec 10, 2020
1 parent 26399b5 commit f7d6006
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1656,21 +1656,18 @@ fn buildOutputType(
if (arg_mode == .run) {
break :l global_cache_directory;
}
const cache_dir_path = blk: {
if (root_pkg) |pkg| {
if (pkg.root_src_directory.path) |p| {
break :blk try fs.path.join(arena, &[_][]const u8{ p, "zig-cache" });
}
}
break :blk "zig-cache";
};
const cache_parent_dir = if (root_pkg) |pkg| pkg.root_src_directory.handle else fs.cwd();
const dir = try cache_parent_dir.makeOpenPath("zig-cache", .{});
cleanup_local_cache_dir = dir;
break :l .{
.handle = dir,
.path = cache_dir_path,
};
if (root_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,
};
}
// Otherwise we really don't have a reasonable place to put the local cache directory,
// so we utilize the global one.
break :l global_cache_directory;
};

if (build_options.have_llvm and emit_asm != .no) {
Expand Down

0 comments on commit f7d6006

Please sign in to comment.