Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use builtin sed #4991

Closed

Conversation

konosuke-sakai
Copy link

This commit fixes alias conflicts by explicitly using \sed. This ensures compatibility in environments where sed is aliased to a non-compatible tool, such as alias sed=sd (chmln/sd).

Fixes #4990

This commit fixes alias conflicts by explicitly using `\sed`. This
ensures compatibility in environments where sed is aliased to a
non-compatible tool, such as `alias sed=sd` (chmln/sd).
Copy link
Collaborator

@jparise jparise left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll give this a deeper look shortly, but I also want to be cognizant of diverging from the upstream project.

https://github.com/rcaloras/bash-preexec

@jcollie
Copy link
Member

jcollie commented Jan 12, 2025

I'll give this a deeper look shortly, but I also want to be cognizant of diverging from the upstream project.

https://github.com/rcaloras/bash-preexec

Seems as if we already diverge from upstream a bit. Perhaps we should upstream this change and then use build.zig.zon to pull in the file rather than vendoring it.

--- /home/jeff/tmp/bash-preexec/bash-preexec.sh 2025-01-12 11:50:03.904439951 -0600
+++ src/shell-integration/bash/bash-preexec.sh  2024-10-27 12:44:20.553235037 -0500
@@ -310,7 +310,13 @@
     fi

     # Adjust our HISTCONTROL Variable if needed.
-    __bp_adjust_histcontrol
+    #
+    # GHOSTTY: Don't modify HISTCONTROL. This hack is only needed to improve the
+    # accuracy of the command argument passed to the preexec functions, and we
+    # don't use that argument in our bash shell integration script (and nor does
+    # the __bp_original_debug_trap function above, which is the only other active
+    # preexec function).
+    #__bp_adjust_histcontrol

     # Issue #25. Setting debug trap for subshells causes sessions to exit for
     # backgrounded subshell commands (e.g. (pwd)& ). Believe this is a bug in Bash.

@mitchellh
Copy link
Contributor

I'll give this a deeper look shortly, but I also want to be cognizant of diverging from the upstream project.

https://github.com/rcaloras/bash-preexec

If it's actively maintained (looks like it), I think we should upstream these changes and try to remain mainlined.

Copy link
Collaborator

@jparise jparise left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few thoughts:

  • Is it common to alias sed to sd? That seems risky given that sd doesn't provide a sed-compatible command line interface (in a "you're knowingly voiding the warranty" kind of way).
  • This proposed change seems fine to me, although it does flip the assumption that someone who intentionally aliased sed to a sed-compatible tool won't get that benefit here. I doubt that matters in practice though.
  • I'd ultimately prefer to drop the dependency on sed entirely, which we should be able to do with bash's shell parameter expansion syntax, although I'd need to look more closely to see if what's needed would be supported by the full range of bash versions that bash-preexec targets.

For example, we previously used something like this in our own shell integration script to avoid calling sed for this use case:

cmd=$(builtin history 1)
cmd="${cmd#*[[:digit:]]*[[:space:]]}" # remove leading history number
cmd="${cmd#"${cmd%%[![:space:]]*}"}"  # remove remaining leading whitespace

I think that would make for a more attractive upstream change (assuming it's version-compatible).

But for the purposes of this change, I'm good with integrating it in the short term if we agree this is an issue that needs addressing while we look at the long-term options.

@mitchellh
Copy link
Contributor

This proposed change seems fine to me, although it does flip the assumption that someone who intentionally aliased sed to a sed-compatible tool won't get that benefit here. I doubt that matters in practice though.

I think in general for scripts like this, we want to use the tool we expect and DON'T want user aliases coming through. As long as we can get to a point where we're mostly doing POSIX-standard things then I think that's especially true as we expect POSIX behavior.

@jparise
Copy link
Collaborator

jparise commented Jan 12, 2025

I'll give this a deeper look shortly, but I also want to be cognizant of diverging from the upstream project.
rcaloras/bash-preexec

Seems as if we already diverge from upstream a bit. Perhaps we should upstream this change and then use build.zig.zon to pull in the file rather than vendoring it.

--- /home/jeff/tmp/bash-preexec/bash-preexec.sh 2025-01-12 11:50:03.904439951 -0600
+++ src/shell-integration/bash/bash-preexec.sh  2024-10-27 12:44:20.553235037 -0500
@@ -310,7 +310,13 @@
     fi

     # Adjust our HISTCONTROL Variable if needed.
-    __bp_adjust_histcontrol
+    #
+    # GHOSTTY: Don't modify HISTCONTROL. This hack is only needed to improve the
+    # accuracy of the command argument passed to the preexec functions, and we
+    # don't use that argument in our bash shell integration script (and nor does
+    # the __bp_original_debug_trap function above, which is the only other active
+    # preexec function).
+    #__bp_adjust_histcontrol

     # Issue #25. Setting debug trap for subshells causes sessions to exit for
     # backgrounded subshell commands (e.g. (pwd)& ). Believe this is a bug in Bash.

I think that would work if we make the __bp_adjust_histcontrol behavior runtime-configurable and the bash-preexec maintainers were cool with that approach.

@jparise
Copy link
Collaborator

jparise commented Jan 12, 2025

This proposed change seems fine to me, although it does flip the assumption that someone who intentionally aliased sed to a sed-compatible tool won't get that benefit here. I doubt that matters in practice though.

I think in general for scripts like this, we want to use the tool we expect and DON'T want user aliases coming through. As long as we can get to a point where we're mostly doing POSIX-standard things then I think that's especially true as we expect POSIX behavior.

Yes, I agree with that (for the same reason we want to use builtin ... consistently in our bash scripts).

My point was more about people who were expecting it to work that way before (in a compatible way), but I can't think of why that would be important in practice.

@jparise
Copy link
Collaborator

jparise commented Jan 12, 2025

I'd ultimately prefer to drop the dependency on sed entirely, which we should be able to do with bash's shell parameter expansion syntax, although I'd need to look more closely to see if what's needed would be supported by the full range of bash versions that bash-preexec targets.

Here's an upstream change that aims to remove one instance of sed: rcaloras/bash-preexec#167

@jparise
Copy link
Collaborator

jparise commented Jan 13, 2025

I have two pending upstream changes that should also address this issue:

Let's wait a few on this PR. If those get merged, we can pull them down instead.

@jparise
Copy link
Collaborator

jparise commented Jan 16, 2025

While those upstream pull requests are under consideration (which might take some time), I've moved forward with our own copies of those changes:

@jparise
Copy link
Collaborator

jparise commented Jan 16, 2025

I think we can close this now that #5141 and #5142 have landed.

@mitchellh
Copy link
Contributor

Was going to ask this. Agree

@mitchellh mitchellh closed this Jan 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants