Skip to content

Commit 2d617c4

Browse files
authored
Fix cache-dir specified on the command line (#14076)
The resolvePosix and resolveWindows routines changed behaviour in an earlier commit so that the return value is not always an absolute path. That caused the relativePosix and relativeWindows to return a relative path that is not correct. The change in behaviour mentioned above would cause a local cache-dir to be created in the wrong directory when --cache-dir was specified for a build.
1 parent f838349 commit 2d617c4

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

lib/std/fs/path.zig

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,11 +1046,13 @@ pub fn relative(allocator: Allocator, from: []const u8, to: []const u8) ![]u8 {
10461046
}
10471047

10481048
pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) ![]u8 {
1049-
const resolved_from = try resolveWindows(allocator, &[_][]const u8{from});
1049+
const cwd = try process.getCwdAlloc(allocator);
1050+
defer allocator.free(cwd);
1051+
const resolved_from = try resolveWindows(allocator, &[_][]const u8{ cwd, from });
10501052
defer allocator.free(resolved_from);
10511053

10521054
var clean_up_resolved_to = true;
1053-
const resolved_to = try resolveWindows(allocator, &[_][]const u8{to});
1055+
const resolved_to = try resolveWindows(allocator, &[_][]const u8{ cwd, to });
10541056
defer if (clean_up_resolved_to) allocator.free(resolved_to);
10551057

10561058
const parsed_from = windowsParsePath(resolved_from);
@@ -1096,12 +1098,8 @@ pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) !
10961098

10971099
var result_index: usize = 0;
10981100
while (result_index < up_index_end) {
1099-
result[result_index] = '.';
1100-
result_index += 1;
1101-
result[result_index] = '.';
1102-
result_index += 1;
1103-
result[result_index] = '\\';
1104-
result_index += 1;
1101+
result[result_index..][0..3].* = "..\\".*;
1102+
result_index += 3;
11051103
}
11061104
// shave off the trailing slash
11071105
result_index -= 1;
@@ -1121,10 +1119,11 @@ pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) !
11211119
}
11221120

11231121
pub fn relativePosix(allocator: Allocator, from: []const u8, to: []const u8) ![]u8 {
1124-
const resolved_from = try resolvePosix(allocator, &[_][]const u8{from});
1122+
const cwd = try process.getCwdAlloc(allocator);
1123+
defer allocator.free(cwd);
1124+
const resolved_from = try resolvePosix(allocator, &[_][]const u8{ cwd, from });
11251125
defer allocator.free(resolved_from);
1126-
1127-
const resolved_to = try resolvePosix(allocator, &[_][]const u8{to});
1126+
const resolved_to = try resolvePosix(allocator, &[_][]const u8{ cwd, to });
11281127
defer allocator.free(resolved_to);
11291128

11301129
var from_it = mem.tokenize(u8, resolved_from, "/");
@@ -1146,12 +1145,8 @@ pub fn relativePosix(allocator: Allocator, from: []const u8, to: []const u8) ![]
11461145

11471146
var result_index: usize = 0;
11481147
while (result_index < up_index_end) {
1149-
result[result_index] = '.';
1150-
result_index += 1;
1151-
result[result_index] = '.';
1152-
result_index += 1;
1153-
result[result_index] = '/';
1154-
result_index += 1;
1148+
result[result_index..][0..3].* = "../".*;
1149+
result_index += 3;
11551150
}
11561151
if (to_rest.len == 0) {
11571152
// shave off the trailing slash

0 commit comments

Comments
 (0)