macos: NSProcessInfo-based arg iterator

Fixes #2432

On macOS, processes with an NSApplicationMain entrypoint do not have
access to libc argc/argv. Instead, we must use NSProcessInfo. This
commit introduces an args iterator that uses NSProcessInfo, giving us
access to the args.

This also fixes an issue where we were not properly skipping argv0 when
iterating over the args. This happened to be fine because we happened to
ignore invalid args but it introduces a config error.
This commit is contained in:
Mitchell Hashimoto
2024-10-14 10:59:46 -07:00
parent 0cf12cd88b
commit 115e14f19f
3 changed files with 144 additions and 4 deletions

View File

@@ -2290,8 +2290,12 @@ pub fn loadCliArgs(self: *Config, alloc_gpa: Allocator) !void {
switch (builtin.os.tag) {
.windows => {},
// Fast-path if we are non-Windows and no args, do nothing.
else => if (std.os.argv.len <= 1) return,
// Fast-path if we are Linux and have no args.
.linux => if (std.os.argv.len <= 1) return,
// Everything else we have to at least try because it may
// not use std.os.argv.
else => {},
}
// On Linux, we have a special case where if the executing
@@ -2360,9 +2364,13 @@ pub fn loadCliArgs(self: *Config, alloc_gpa: Allocator) !void {
counter[i] = @field(self, field).list.items.len;
}
// Parse the config from the CLI args
var iter = try std.process.argsWithAllocator(alloc_gpa);
// Initialize our CLI iterator. The first argument is always assumed
// to be the program name so we skip over that.
var iter = try internal_os.args.iterator(alloc_gpa);
defer iter.deinit();
if (iter.next()) |argv0| log.debug("skipping argv0 value={s}", .{argv0});
// Parse the config from the CLI args
try self.loadIter(alloc_gpa, &iter);
// If we are not loading the default files, then we need to