Skip to content

Commit

Permalink
Fixes ziglang#12685.
Browse files Browse the repository at this point in the history
The problem was that paths in the cache are assumed to endwithout the path separator, but are added verbatim from the command line
in several places. The solution here is to make sure that all prefixes are without path separator by cutting it off when it is passed
to Cache.addPrefix. This way, the assumption is met.
  • Loading branch information
Felix "xq" Queißner committed Jan 5, 2023
1 parent b89158d commit bbd30b4
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/Cache.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,21 @@ const Compilation = @import("Compilation.zig");
const log = std.log.scoped(.cache);

pub fn addPrefix(cache: *Cache, directory: Compilation.Directory) void {
if (directory.path) |p| {
var patched_dir = directory;
if (patched_dir.path) |*path| {
if (std.mem.endsWith(u8, path.*, std.fs.path.sep_str)) {
// remove the path separator at the end. This might remove the "/" from an
// absolute path, and push "" into the cache, but this is acceptible,
// as both "/" and "" are semantically the same prefix of a unix path.
path.* = path.*[0 .. path.*.len - 1];
}
}

if (patched_dir.path) |p| {
log.debug("Cache.addPrefix {d} {s}", .{ cache.prefixes_len, p });
}
cache.prefixes_buffer[cache.prefixes_len] = directory;

cache.prefixes_buffer[cache.prefixes_len] = patched_dir;
cache.prefixes_len += 1;
}

Expand Down Expand Up @@ -73,7 +84,6 @@ fn findPrefixResolved(cache: *const Cache, resolved_path: []u8) !PrefixedPath {
while (i < prefixes_slice.len) : (i += 1) {
const p = prefixes_slice[i].path.?;
if (mem.startsWith(u8, resolved_path, p)) {
// +1 to skip over the path separator here
const sub_path = try gpa.dupe(u8, resolved_path[p.len + 1 ..]);
gpa.free(resolved_path);
return PrefixedPath{
Expand Down

0 comments on commit bbd30b4

Please sign in to comment.