termio/exec: only add ghostty path if it isn't already in path

This commit is contained in:
Mitchell Hashimoto
2024-01-31 18:57:42 -08:00
parent 577b12430c
commit c2e0cff1d2
2 changed files with 21 additions and 10 deletions

View File

@ -2,6 +2,12 @@ const std = @import("std");
const builtin = @import("builtin"); const builtin = @import("builtin");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
/// The separator used in environment variables such as PATH.
pub const PATH_SEP = switch (builtin.os.tag) {
.windows => ";",
else => ":",
};
/// Append a value to an environment variable such as PATH. /// Append a value to an environment variable such as PATH.
/// The returned value is always allocated so it must be freed. /// The returned value is always allocated so it must be freed.
pub fn appendEnv( pub fn appendEnv(
@ -13,14 +19,9 @@ pub fn appendEnv(
if (current.len == 0) return try alloc.dupe(u8, value); if (current.len == 0) return try alloc.dupe(u8, value);
// Otherwise we must prefix. // Otherwise we must prefix.
const sep = switch (builtin.os.tag) {
.windows => ";",
else => ":",
};
return try std.fmt.allocPrint(alloc, "{s}{s}{s}", .{ return try std.fmt.allocPrint(alloc, "{s}{s}{s}", .{
current, current,
sep, PATH_SEP,
value, value,
}); });
} }

View File

@ -962,10 +962,20 @@ const Subprocess = struct {
// Append if we have a path. We want to append so that ghostty is // Append if we have a path. We want to append so that ghostty is
// the last priority in the path. If we don't have a path set // the last priority in the path. If we don't have a path set
// then we just set it to the directory of the binary. // then we just set it to the directory of the binary.
try env.put("PATH", if (env.get("PATH")) |path| if (env.get("PATH")) |path| {
try internal_os.appendEnv(alloc, path, exe_dir) // Verify that our path doesn't already contain this entry
else var it = std.mem.tokenizeScalar(u8, path, internal_os.PATH_SEP[0]);
exe_dir); while (it.next()) |entry| {
if (std.mem.eql(u8, entry, exe_dir)) break :ghostty_path;
}
try env.put(
"PATH",
try internal_os.appendEnv(alloc, path, exe_dir),
);
} else {
try env.put("PATH", exe_dir);
}
} }
// Set environment variables used by some programs (such as neovim) to detect // Set environment variables used by some programs (such as neovim) to detect