Commit 2d617c482c

pluick <38442746+pluick@users.noreply.github.com>
2023-01-05 10:37:00
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
Changed files (1)
lib
std
lib/std/fs/path.zig
@@ -1046,11 +1046,13 @@ pub fn relative(allocator: Allocator, from: []const u8, to: []const u8) ![]u8 {
 }
 
 pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) ![]u8 {
-    const resolved_from = try resolveWindows(allocator, &[_][]const u8{from});
+    const cwd = try process.getCwdAlloc(allocator);
+    defer allocator.free(cwd);
+    const resolved_from = try resolveWindows(allocator, &[_][]const u8{ cwd, from });
     defer allocator.free(resolved_from);
 
     var clean_up_resolved_to = true;
-    const resolved_to = try resolveWindows(allocator, &[_][]const u8{to});
+    const resolved_to = try resolveWindows(allocator, &[_][]const u8{ cwd, to });
     defer if (clean_up_resolved_to) allocator.free(resolved_to);
 
     const parsed_from = windowsParsePath(resolved_from);
@@ -1096,12 +1098,8 @@ pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) !
 
         var result_index: usize = 0;
         while (result_index < up_index_end) {
-            result[result_index] = '.';
-            result_index += 1;
-            result[result_index] = '.';
-            result_index += 1;
-            result[result_index] = '\\';
-            result_index += 1;
+            result[result_index..][0..3].* = "..\\".*;
+            result_index += 3;
         }
         // shave off the trailing slash
         result_index -= 1;
@@ -1121,10 +1119,11 @@ pub fn relativeWindows(allocator: Allocator, from: []const u8, to: []const u8) !
 }
 
 pub fn relativePosix(allocator: Allocator, from: []const u8, to: []const u8) ![]u8 {
-    const resolved_from = try resolvePosix(allocator, &[_][]const u8{from});
+    const cwd = try process.getCwdAlloc(allocator);
+    defer allocator.free(cwd);
+    const resolved_from = try resolvePosix(allocator, &[_][]const u8{ cwd, from });
     defer allocator.free(resolved_from);
-
-    const resolved_to = try resolvePosix(allocator, &[_][]const u8{to});
+    const resolved_to = try resolvePosix(allocator, &[_][]const u8{ cwd, to });
     defer allocator.free(resolved_to);
 
     var from_it = mem.tokenize(u8, resolved_from, "/");
@@ -1146,12 +1145,8 @@ pub fn relativePosix(allocator: Allocator, from: []const u8, to: []const u8) ![]
 
         var result_index: usize = 0;
         while (result_index < up_index_end) {
-            result[result_index] = '.';
-            result_index += 1;
-            result[result_index] = '.';
-            result_index += 1;
-            result[result_index] = '/';
-            result_index += 1;
+            result[result_index..][0..3].* = "../".*;
+            result_index += 3;
         }
         if (to_rest.len == 0) {
             // shave off the trailing slash