diff --git a/src/fsx.zig b/src/fsx.zig index d3e90c4..0a42955 100644 --- a/src/fsx.zig +++ b/src/fsx.zig @@ -171,11 +171,13 @@ pub fn normPath(gpa: Allocator, path: []const u8) Allocator.Error![]u8 { /// Return a normalized absolute path like Python's `os.path.abspath`, /// joining relative paths to the current working directory without -/// following symbolic links. The caller owns the returned slice. -pub fn absPath(gpa: Allocator, path: []const u8) Allocator.Error![]u8 { +/// following symbolic links. The caller owns the returned slice. A +/// relative path is `error.NotFound` when that directory has been +/// removed, as `os.getcwd` reports. +pub fn absPath(gpa: Allocator, path: []const u8) (Error || Allocator.Error)![]u8 { if (std.mem.startsWith(u8, path, "/")) return normPath(gpa, path); var buf: [sys.path_max]u8 = undefined; - const cwd_path = realpath(cwd, ".", &buf) catch return normPath(gpa, path); + const cwd_path = try getcwd(&buf); const joined = try std.mem.concat(gpa, u8, &.{ cwd_path, "/", path }); defer gpa.free(joined); return normPath(gpa, joined); @@ -212,6 +214,31 @@ test "normPath and absPath match posixpath.normpath and os.path.abspath" { try std.testing.expectEqualStrings(expected, abs_rel); } +test "absPath reports a removed working directory" { + const gpa = std.testing.allocator; + var here_buf: [sys.path_max]u8 = undefined; + const here = try realpath(cwd, ".", &here_buf); + var hz: [sys.path_max:0]u8 = undefined; + const here_z = try sys.z(&hz, here); + var gb: [sys.path_max]u8 = undefined; + const gone = try std.fmt.bufPrint(&gb, "{s}/.zig-cache/zig-common-abspath-gone-{d}", .{ here, linux.getpid() }); + var gz: [sys.path_max:0]u8 = undefined; + const gone_z = try sys.z(&gz, gone); + try std.testing.expectEqual(@as(usize, 0), linux.mkdirat(cwd, gone_z.ptr, 0o700)); + try std.testing.expectEqual(@as(usize, 0), linux.chdir(gone_z.ptr)); + defer _ = linux.chdir(here_z.ptr); + try sys.unlinkat(cwd, gone, linux.AT.REMOVEDIR); + + // os.path.abspath raises ENOENT from getcwd. A relative path with a + // "." segment still needs that directory. + try std.testing.expectError(error.NotFound, absPath(gpa, "sub/./file")); + try std.testing.expectEqual(linux.E.NOENT, sys.last_errno); + + const abs = try absPath(gpa, "/tmp/./x/../y"); + defer gpa.free(abs); + try std.testing.expectEqualStrings("/tmp/y", abs); +} + /// Open an existing regular file for reading. `follow` decides whether a /// final symbolic link is followed. A FIFO or device is refused without /// blocking on it. diff --git a/src/proc.zig b/src/proc.zig index bbe684b..4be267b 100644 --- a/src/proc.zig +++ b/src/proc.zig @@ -283,7 +283,11 @@ pub fn resolveExecutable(gpa: Allocator, name: []const u8, path_env: ?[]const u8 const abs = if (std.fs.path.isAbsolute(name)) try gpa.dupe(u8, name) else - try fsx.absPath(gpa, name); + fsx.absPath(gpa, name) catch |err| switch (err) { + error.OutOfMemory => return error.OutOfMemory, + // No absolute path exists when the working directory is gone. + else => return null, + }; if (!isExecutableFile(abs)) { gpa.free(abs); return null;