diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2022-11-25 17:12:50 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2022-11-28 01:23:39 -0500 |
| commit | 3ae4931dc1a3b1b338d2fd3a49a5a79b445bebf6 (patch) | |
| tree | 8c6f031412192a2024f17506a8f9d10e0fd13343 /src/introspect.zig | |
| parent | 7411be3c9e6d169108456f03b3cbb9b476ee7498 (diff) | |
| download | zig-3ae4931dc1a3b1b338d2fd3a49a5a79b445bebf6.tar.gz zig-3ae4931dc1a3b1b338d2fd3a49a5a79b445bebf6.zip | |
CLI: more careful resolution of paths
In general, we prefer compiler code to use relative paths based on open
directory handles because this is the most portable. However, sometimes
absolute paths are used, and sometimes relative paths are used that go
up a directory.
The recent improvements in 81d2135ca6ebd71b8c121a19957c8fbf7f87125b
regressed the use case when an absolute path is used for the zig lib
directory mixed with a relative path used for the root source file. This
could happen when, for example, running the standard library tests, like
this:
stage3/bin/zig test ../lib/std/std.zig
This happened because the zig lib dir was inferred to be an absolute
directory based on the zig executable directory, while the root source
file was detected as a relative path. There was no common prefix and so
it was not determined that the std.zig file was inside the lib
directory.
This commit adds a function for resolving paths that preserves relative
path names while allowing absolute paths, and converting relative
upwards paths (e.g. "../foo") to absolute paths. This restores the
previous functionality while remaining compatible with systems such as
WASI that cannot deal with absolute paths.
Diffstat (limited to 'src/introspect.zig')
| -rw-r--r-- | src/introspect.zig | 54 |
1 files changed, 52 insertions, 2 deletions
diff --git a/src/introspect.zig b/src/introspect.zig index 74f0d45c80..27925ab667 100644 --- a/src/introspect.zig +++ b/src/introspect.zig @@ -82,7 +82,12 @@ pub fn findZigLibDir(gpa: mem.Allocator) !Compilation.Directory { pub fn findZigLibDirFromSelfExe( allocator: mem.Allocator, self_exe_path: []const u8, -) error{ OutOfMemory, FileNotFound }!Compilation.Directory { +) error{ + OutOfMemory, + FileNotFound, + CurrentWorkingDirectoryUnlinked, + Unexpected, +}!Compilation.Directory { const cwd = fs.cwd(); var cur_path: []const u8 = self_exe_path; while (fs.path.dirname(cur_path)) |dirname| : (cur_path = dirname) { @@ -90,9 +95,11 @@ pub fn findZigLibDirFromSelfExe( defer base_dir.close(); const sub_directory = testZigInstallPrefix(base_dir) orelse continue; + const p = try fs.path.join(allocator, &[_][]const u8{ dirname, sub_directory.path.? }); + defer allocator.free(p); return Compilation.Directory{ .handle = sub_directory.handle, - .path = try fs.path.join(allocator, &[_][]const u8{ dirname, sub_directory.path.? }), + .path = try resolvePath(allocator, p), }; } return error.FileNotFound; @@ -130,3 +137,46 @@ pub fn resolveGlobalCacheDir(allocator: mem.Allocator) ![]u8 { return fs.getAppDataDir(allocator, appname); } } + +/// Similar to std.fs.path.resolve, with a few important differences: +/// * If the input is an absolute path, check it against the cwd and try to +/// convert it to a relative path. +/// * If the resulting path would start with a relative up-dir ("../"), instead +/// return an absolute path based on the cwd. +/// * When targeting WASI, fail with an error message if an absolute path is +/// used. +pub fn resolvePath( + ally: mem.Allocator, + p: []const u8, +) error{ + OutOfMemory, + CurrentWorkingDirectoryUnlinked, + Unexpected, +}![]u8 { + if (fs.path.isAbsolute(p)) { + const cwd_path = try std.process.getCwdAlloc(ally); + defer ally.free(cwd_path); + const relative = try fs.path.relative(ally, cwd_path, p); + if (isUpDir(relative)) { + ally.free(relative); + return ally.dupe(u8, p); + } else { + return relative; + } + } else { + const resolved = try fs.path.resolve(ally, &.{p}); + if (isUpDir(resolved)) { + ally.free(resolved); + const cwd_path = try std.process.getCwdAlloc(ally); + defer ally.free(cwd_path); + return fs.path.resolve(ally, &.{ cwd_path, p }); + } else { + return resolved; + } + } +} + +/// TODO move this to std.fs.path +pub fn isUpDir(p: []const u8) bool { + return mem.startsWith(u8, p, "..") and (p.len == 2 or p[2] == fs.path.sep); +} |
