From 01233a48d14cb9ae191c085a386bccfbafb91243 Mon Sep 17 00:00:00 2001 From: Jon Parise Date: Thu, 10 Jul 2025 15:12:54 -0400 Subject: [PATCH] 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. --- src/shell-integration/bash/ghostty.bash | 6 ++++++ src/termio/shell_integration.zig | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/shell-integration/bash/ghostty.bash b/src/shell-integration/bash/ghostty.bash index aacf37c3a..ca5a012c6 100644 --- a/src/shell-integration/bash/ghostty.bash +++ b/src/shell-integration/bash/ghostty.bash @@ -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 diff --git a/src/termio/shell_integration.zig b/src/termio/shell_integration.zig index 469ff2859..438c2a0ea 100644 --- a/src/termio/shell_integration.zig +++ b/src/termio/shell_integration.zig @@ -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);