diff options
| author | pluick <38442746+pluick@users.noreply.github.com> | 2023-01-05 03:37:00 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-05 01:37:00 -0800 |
| commit | 2d617c482ca24563a27125d626ac51e194d49eff (patch) | |
| tree | ebc0dd086752d85b3337aa0bfcfdfd0c1c20055c /lib/std | |
| parent | f83834993e2628e347da71a11ffb07c804fc46c5 (diff) | |
| download | zig-2d617c482ca24563a27125d626ac51e194d49eff.tar.gz zig-2d617c482ca24563a27125d626ac51e194d49eff.zip | |
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.
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/fs/path.zig | 29 |
1 files changed, 12 insertions, 17 deletions
diff --git a/lib/std/fs/path.zig b/lib/std/fs/path.zig index 3d6ed27538..ebb2ec82d8 100644 --- a/lib/std/fs/path.zig +++ b/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 |
