std.process: currentDir -> currentPath

In Zig standard library, Dir means an open directory handle. path
represents a file system identifier string. This function is better
named after "current path" than "current dir". "get" and "working" are
superfluous.
This commit is contained in:
Andrew Kelley
2026-01-29 18:47:58 -08:00
parent 0a37ad2ec4
commit b1d1806fef
18 changed files with 41 additions and 41 deletions
+1 -1
View File
@@ -83,7 +83,7 @@ pub fn main(init: process.Init.Minimal) !void {
.io = io,
.gpa = gpa,
.manifest_dir = try local_cache_directory.handle.createDirPathOpen(io, "h", .{}),
.cwd = try process.currentDirAlloc(io, single_threaded_arena.allocator()),
.cwd = try process.currentPathAlloc(io, single_threaded_arena.allocator()),
},
.zig_exe = zig_exe,
.environ_map = try init.environ.createMap(arena),
+4 -4
View File
@@ -1315,7 +1315,7 @@ test "cache file and then recall it" {
var tmp = testing.tmpDir(.{});
defer tmp.cleanup();
const cwd = try std.process.currentDirAlloc(io, testing.allocator);
const cwd = try std.process.currentPathAlloc(io, testing.allocator);
defer testing.allocator.free(cwd);
const temp_file = "test.txt";
@@ -1383,7 +1383,7 @@ test "check that changing a file makes cache fail" {
var tmp = testing.tmpDir(.{});
defer tmp.cleanup();
const cwd = try std.process.currentDirAlloc(io, testing.allocator);
const cwd = try std.process.currentPathAlloc(io, testing.allocator);
defer testing.allocator.free(cwd);
const temp_file = "cache_hash_change_file_test.txt";
@@ -1459,7 +1459,7 @@ test "no file inputs" {
var tmp = testing.tmpDir(.{});
defer tmp.cleanup();
const cwd = try std.process.currentDirAlloc(io, testing.allocator);
const cwd = try std.process.currentPathAlloc(io, testing.allocator);
defer testing.allocator.free(cwd);
const temp_manifest_dir = "no_file_inputs_manifest_dir";
@@ -1509,7 +1509,7 @@ test "Manifest with files added after initial hash work" {
var tmp = testing.tmpDir(.{});
defer tmp.cleanup();
const cwd = try std.process.currentDirAlloc(io, testing.allocator);
const cwd = try std.process.currentPathAlloc(io, testing.allocator);
defer testing.allocator.free(cwd);
const temp_file1 = "cache_hash_post_file_test1.txt";
+1 -1
View File
@@ -519,7 +519,7 @@ test Options {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const cwd = try std.process.currentDirAlloc(io, std.testing.allocator);
const cwd = try std.process.currentPathAlloc(io, std.testing.allocator);
defer std.testing.allocator.free(cwd);
var graph: std.Build.Graph = .{
+1 -1
View File
@@ -665,7 +665,7 @@ pub const VTable = struct {
lockStderr: *const fn (?*anyopaque, ?Terminal.Mode) Cancelable!LockedStderr,
tryLockStderr: *const fn (?*anyopaque, ?Terminal.Mode) Cancelable!?LockedStderr,
unlockStderr: *const fn (?*anyopaque) void,
processCurrentDir: *const fn (?*anyopaque, buffer: []u8) std.process.CurrentDirError!usize,
processCurrentPath: *const fn (?*anyopaque, buffer: []u8) std.process.CurrentPathError!usize,
processSetCurrentDir: *const fn (?*anyopaque, Dir) std.process.SetCurrentDirError!void,
processReplace: *const fn (?*anyopaque, std.process.ReplaceOptions) std.process.ReplaceError,
processReplacePath: *const fn (?*anyopaque, Dir, std.process.ReplaceOptions) std.process.ReplaceError,
+4 -4
View File
@@ -1643,7 +1643,7 @@ pub fn io(t: *Threaded) Io {
.lockStderr = lockStderr,
.tryLockStderr = tryLockStderr,
.unlockStderr = unlockStderr,
.processCurrentDir = processCurrentDir,
.processCurrentPath = processCurrentPath,
.processSetCurrentDir = processSetCurrentDir,
.processReplace = processReplace,
.processReplacePath = processReplacePath,
@@ -1802,7 +1802,7 @@ pub fn ioBasic(t: *Threaded) Io {
.lockStderr = lockStderr,
.tryLockStderr = tryLockStderr,
.unlockStderr = unlockStderr,
.processCurrentDir = processCurrentDir,
.processCurrentPath = processCurrentPath,
.processSetCurrentDir = processSetCurrentDir,
.processReplace = processReplace,
.processReplacePath = processReplacePath,
@@ -12606,7 +12606,7 @@ fn unlockStderr(userdata: ?*anyopaque) void {
process.stderr_thread_mutex.unlock();
}
fn processCurrentDir(userdata: ?*anyopaque, buffer: []u8) process.CurrentDirError!usize {
fn processCurrentPath(userdata: ?*anyopaque, buffer: []u8) process.CurrentPathError!usize {
const t: *Threaded = @ptrCast(@alignCast(userdata));
_ = t;
if (is_windows) {
@@ -12638,7 +12638,7 @@ fn processCurrentDir(userdata: ?*anyopaque, buffer: []u8) process.CurrentDirErro
};
switch (err) {
.SUCCESS => return std.mem.findScalar(u8, buffer, 0).?,
.NOENT => return error.CurrentWorkingDirectoryUnlinked,
.NOENT => return error.CurrentDirUnlinked,
.RANGE => return error.NameTooLong,
.FAULT => |e| return errnoBug(e),
.INVAL => |e| return errnoBug(e),
+1 -1
View File
@@ -1787,7 +1787,7 @@ test "open file with exclusive nonblocking lock twice (absolute paths)" {
const gpa = testing.allocator;
const cwd = try std.process.currentDirAlloc(io, gpa);
const cwd = try std.process.currentPathAlloc(io, gpa);
defer gpa.free(cwd);
const filename = try Dir.path.resolve(gpa, &.{ cwd, sub_path });
+11 -11
View File
@@ -63,22 +63,22 @@ pub const Init = struct {
};
};
pub const CurrentDirError = error{
pub const CurrentPathError = error{
NameTooLong,
/// Not possible on Windows. Always returned on WASI.
CurrentWorkingDirectoryUnlinked,
CurrentDirUnlinked,
} || Io.UnexpectedError;
/// On Windows, the result is encoded as [WTF-8](https://wtf-8.codeberg.page/).
/// On other platforms, the result is an opaque sequence of bytes with no
/// particular encoding.
pub fn currentDir(io: Io, buffer: []u8) CurrentDirError!usize {
return io.vtable.processCurrentDir(io.userdata, buffer);
pub fn currentPath(io: Io, buffer: []u8) CurrentPathError!usize {
return io.vtable.processCurrentPath(io.userdata, buffer);
}
pub const CurrentDirAllocError = Allocator.Error || error{
pub const CurrentPathAllocError = Allocator.Error || error{
/// Not possible on Windows. Always returned on WASI.
CurrentWorkingDirectoryUnlinked,
CurrentDirUnlinked,
} || Io.UnexpectedError;
/// On Windows, the result is encoded as [WTF-8](https://wtf-8.codeberg.page/).
@@ -86,17 +86,17 @@ pub const CurrentDirAllocError = Allocator.Error || error{
/// particular encoding.
///
/// Caller owns returned memory.
pub fn currentDirAlloc(io: Io, allocator: Allocator) CurrentDirAllocError![:0]u8 {
pub fn currentPathAlloc(io: Io, allocator: Allocator) CurrentPathAllocError![:0]u8 {
var buffer: [max_path_bytes]u8 = undefined;
const n = currentDir(io, &buffer) catch |err| switch (err) {
const n = currentPath(io, &buffer) catch |err| switch (err) {
error.NameTooLong => unreachable,
else => |e| return e,
};
return allocator.dupeZ(u8, buffer[0..n]);
}
test currentDirAlloc {
const cwd = try currentDirAlloc(testing.io, testing.allocator);
test currentPathAlloc {
const cwd = try currentPathAlloc(testing.io, testing.allocator);
testing.allocator.free(cwd);
}
@@ -453,7 +453,7 @@ pub fn spawnPath(io: Io, dir: Io.Dir, options: SpawnOptions) SpawnError!Child {
return io.vtable.processSpawnPath(io.userdata, dir, options);
}
pub const RunError = CurrentDirError || posix.ReadError || SpawnError || posix.PollError || error{
pub const RunError = CurrentPathError || posix.ReadError || SpawnError || posix.PollError || error{
StdoutStreamTooLong,
StderrStreamTooLong,
};
+4 -4
View File
@@ -51,23 +51,23 @@ pub fn findZigLibDir(gpa: Allocator, io: Io) !Cache.Directory {
return findZigLibDirFromSelfExe(gpa, io, cwd_path, self_exe_path);
}
/// Like `std.process.currentDirAlloc`, but also resolves the path with `Dir.path.resolve`. This
/// Like `std.process.currentPathAlloc`, but also resolves the path with `Dir.path.resolve`. This
/// means the path has no repeated separators, no "." or ".." components, and no trailing separator.
/// On WASI, "" is returned instead of ".".
pub fn getResolvedCwd(io: Io, gpa: Allocator) error{
OutOfMemory,
CurrentWorkingDirectoryUnlinked,
CurrentDirUnlinked,
Unexpected,
}![]u8 {
if (builtin.target.os.tag == .wasi) {
if (std.debug.runtime_safety) {
const cwd = try std.process.currentDirAlloc(io, gpa);
const cwd = try std.process.currentPathAlloc(io, gpa);
defer gpa.free(cwd);
assert(mem.eql(u8, cwd, "."));
}
return "";
}
const cwd = try std.process.currentDirAlloc(io, gpa);
const cwd = try std.process.currentPathAlloc(io, gpa);
defer gpa.free(cwd);
const resolved = try Dir.path.resolve(gpa, &.{cwd});
assert(Dir.path.isAbsolute(resolved));
+1 -1
View File
@@ -16,7 +16,7 @@ pub fn main(init: std.process.Init.Minimal) !void {
defer threaded.deinit();
const io = threaded.io();
const process_cwd_path = try std.process.currentDirAlloc(io, gpa);
const process_cwd_path = try std.process.currentPathAlloc(io, gpa);
defer gpa.free(process_cwd_path);
var environ_map = try init.environ.createMap(gpa);
+5 -5
View File
@@ -28,13 +28,13 @@ pub fn main(init: std.process.Init) !void {
// get current working directory and expect it to match given path
fn expect_cwd(io: Io, expected_cwd: []const u8) !void {
var cwd_buf: [path_max]u8 = undefined;
const actual_cwd = cwd_buf[0..try std.process.currentDir(io, &cwd_buf)];
const actual_cwd = cwd_buf[0..try std.process.currentPath(io, &cwd_buf)];
try std.testing.expectEqualStrings(actual_cwd, expected_cwd);
}
fn test_chdir_self(io: Io) !void {
var old_cwd_buf: [path_max]u8 = undefined;
const old_cwd = old_cwd_buf[0..try std.process.currentDir(io, &old_cwd_buf)];
const old_cwd = old_cwd_buf[0..try std.process.currentPath(io, &old_cwd_buf)];
// Try changing to the current directory
try std.Io.Threaded.chdir(old_cwd);
@@ -43,7 +43,7 @@ fn test_chdir_self(io: Io) !void {
fn test_chdir_absolute(io: Io) !void {
var old_cwd_buf: [path_max]u8 = undefined;
const old_cwd = old_cwd_buf[0..try std.process.currentDir(io, &old_cwd_buf)];
const old_cwd = old_cwd_buf[0..try std.process.currentPath(io, &old_cwd_buf)];
const parent = std.fs.path.dirname(old_cwd) orelse unreachable; // old_cwd should be absolute
@@ -62,7 +62,7 @@ fn test_chdir_relative(gpa: Allocator, io: Io, tmp_dir: Io.Dir) !void {
// Capture base working directory path, to build expected full path
var base_cwd_buf: [path_max]u8 = undefined;
const base_cwd = base_cwd_buf[0..try std.process.currentDir(io, &base_cwd_buf)];
const base_cwd = base_cwd_buf[0..try std.process.currentPath(io, &base_cwd_buf)];
const expected_path = try std.fs.path.resolve(gpa, &.{ base_cwd, subdir_path });
defer gpa.free(expected_path);
@@ -71,7 +71,7 @@ fn test_chdir_relative(gpa: Allocator, io: Io, tmp_dir: Io.Dir) !void {
try std.Io.Threaded.chdir(subdir_path);
var new_cwd_buf: [path_max]u8 = undefined;
const new_cwd = new_cwd_buf[0..try std.process.currentDir(io, &new_cwd_buf)];
const new_cwd = new_cwd_buf[0..try std.process.currentPath(io, &new_cwd_buf)];
// On Windows, fs.path.resolve returns an uppercase drive letter, but the drive letter returned by getcwd may be lowercase
const resolved_cwd = try std.fs.path.resolve(gpa, &.{new_cwd});
@@ -9,7 +9,7 @@ pub fn main(init: std.process.Init) !void {
const exe_path = it.next() orelse unreachable;
const symlink_path = it.next() orelse unreachable;
const cwd = try std.process.currentDirAlloc(io, init.arena.allocator());
const cwd = try std.process.currentPathAlloc(io, init.arena.allocator());
// If `exe_path` is relative to our cwd, we need to convert it to be relative to the dirname of `symlink_path`.
const exe_rel_path = try std.fs.path.relative(gpa, cwd, init.environ_map, std.fs.path.dirname(symlink_path) orelse ".", exe_path);
+1 -1
View File
@@ -4,7 +4,7 @@ pub fn main(init: std.process.Init) !void {
const arena = init.arena.allocator();
const args = try init.minimal.args.toSlice(arena);
const io = init.io;
const cwd_path = try std.process.currentDirAlloc(io, arena);
const cwd_path = try std.process.currentPathAlloc(io, arena);
if (args.len < 3) return error.MissingArgs;
+1 -1
View File
@@ -10,7 +10,7 @@ pub fn main(init: std.process.Init) !void {
const exe_path = args[1];
const cwd_path = try std.process.currentDirAlloc(io, arena);
const cwd_path = try std.process.currentPathAlloc(io, arena);
const parsed_cwd_path = std.fs.path.parsePathWindows(u8, cwd_path);
if (parsed_cwd_path.kind == .drive_absolute and !std.ascii.isAlphabetic(cwd_path[0])) {
+1 -1
View File
@@ -8,7 +8,7 @@ const utf16Literal = std.unicode.utf8ToUtf16LeStringLiteral;
pub fn main(init: std.process.Init) !void {
const gpa = init.gpa;
const io = init.io;
const process_cwd_path = try std.process.currentDirAlloc(io, init.arena.allocator());
const process_cwd_path = try std.process.currentPathAlloc(io, init.arena.allocator());
var initial_process_cwd = try Io.Dir.cwd().openDir(io, ".", .{});
defer initial_process_cwd.close(io);
+1 -1
View File
@@ -33,7 +33,7 @@ pub fn main(init: std.process.Init) !void {
const arena = init.arena.allocator();
const io = init.io;
const environ_map = init.environ_map;
const cwd_path = try std.process.currentDirAlloc(io, arena);
const cwd_path = try std.process.currentPathAlloc(io, arena);
try environ_map.put("CLICOLOR_FORCE", "1");
+1 -1
View File
@@ -32,7 +32,7 @@ pub fn main(init: std.process.Init) !void {
const arena = init.arena.allocator();
const io = init.io;
const environ_map = init.environ_map;
const cwd_path = try std.process.currentDirAlloc(io, arena);
const cwd_path = try std.process.currentPathAlloc(io, arena);
var opt_zig_exe: ?[]const u8 = null;
var opt_input_file_name: ?[]const u8 = null;
+1 -1
View File
@@ -145,7 +145,7 @@ pub fn main(init: std.process.Init) !void {
const arena = init.arena.allocator();
const io = init.io;
const args = try init.minimal.args.toSlice(arena);
const cwd_path = try std.process.currentDirAlloc(io, arena);
const cwd_path = try std.process.currentPathAlloc(io, arena);
const environ_map = init.environ_map;
var search_paths = std.array_list.Managed([]const u8).init(arena);
+1 -1
View File
@@ -146,7 +146,7 @@ pub fn main(init: std.process.Init) !void {
const io = init.io;
const args = try init.minimal.args.toSlice(arena);
const environ_map = init.environ_map;
const cwd = try std.process.currentDirAlloc(io, arena);
const cwd = try std.process.currentPathAlloc(io, arena);
var search_paths = std.array_list.Managed([]const u8).init(arena);
var opt_out_dir: ?[]const u8 = null;