bash: preserve an existing ENV value

Our bash shell integration code uses ENV (in POSIX mode) to bootstrap
our shell integration script. This had the side effect of overwriting an
existing ENV value.

This change preserves ENV by storing it temporarily in GHOSTTY_BASH_ENV.

Note that this doesn't enable --posix mode support for automatic shell
integration. (--posix does work; we just skip shell integration when
that flag is specified.) We can reconsider implementing full --posix
support separately.
This commit is contained in:
Jon Parise
2025-07-10 15:12:54 -04:00
parent cc646ecf46
commit 01233a48d1
2 changed files with 27 additions and 0 deletions

View File

@ -26,6 +26,12 @@ if [ -n "$GHOSTTY_BASH_INJECT" ]; then
builtin declare __ghostty_bash_flags="$GHOSTTY_BASH_INJECT"
builtin unset ENV GHOSTTY_BASH_INJECT
# Restore an existing ENV that was replaced by the shell integration code.
if [[ -n "$GHOSTTY_BASH_ENV" ]]; then
builtin export ENV=$GHOSTTY_BASH_ENV
builtin unset GHOSTTY_BASH_ENV
fi
# Restore bash's default 'posix' behavior. Also reset 'inherit_errexit',
# which doesn't happen as part of the 'posix' reset.
builtin set +o posix

View File

@ -340,6 +340,11 @@ fn setupBash(
}
}
// Preserve an existing ENV value. We're about to overwrite it.
if (env.get("ENV")) |v| {
try env.put("GHOSTTY_BASH_ENV", v);
}
// Set our new ENV to point to our integration script.
var path_buf: [std.fs.max_path_bytes]u8 = undefined;
const integ_dir = try std.fmt.bufPrint(
@ -502,6 +507,22 @@ test "bash: HISTFILE" {
}
}
test "bash: ENV" {
const testing = std.testing;
var arena = ArenaAllocator.init(testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
var env = EnvMap.init(alloc);
defer env.deinit();
try env.put("ENV", "env.sh");
_ = try setupBash(alloc, .{ .shell = "bash" }, ".", &env);
try testing.expectEqualStrings("./shell-integration/bash/ghostty.bash", env.get("ENV").?);
try testing.expectEqualStrings("env.sh", env.get("GHOSTTY_BASH_ENV").?);
}
test "bash: additional arguments" {
const testing = std.testing;
var arena = ArenaAllocator.init(testing.allocator);