Skip to content

shellIntegration-bash.sh: exactly preserve DEBUG trap expression#165581

Merged
Tyriar merged 5 commits intomicrosoft:mainfrom
rwe:bash-parse-trap
Dec 15, 2022
Merged

shellIntegration-bash.sh: exactly preserve DEBUG trap expression#165581
Tyriar merged 5 commits intomicrosoft:mainfrom
rwe:bash-parse-trap

Conversation

@rwe
Copy link
Contributor

@rwe rwe commented Nov 5, 2022

The __vsc_dbg_trap management was a bit hacky. It incorrectly coalesced whitespace within commands (and even within their arguments), and failed to parse many other kinds of expressions. For example, trap DEBUG $'echo foo\necho \'bar baz qux\'' would be mis-parsed for two reasons: one is that the expression has a $'…' string (and so the "find-and-replace" approach would fail); the other is that all newlines etc. would be replaced with spaces due to exploded strings.

It also unnecessarily relied on external tr and cut utilities, not that that matters much.

This replaces it with an implementation that preserves any expression exactly, and includes some helpful comments about how it does so. This was mostly an exercise in "how would one do this better…" than any specific issue.

fixes #150877

@rwe rwe force-pushed the bash-parse-trap branch from af1ca71 to 6bf4332 Compare November 6, 2022 21:55
@rwe rwe force-pushed the bash-parse-trap branch 2 times, most recently from c35e96e to bc6b43b Compare November 15, 2022 23:16
rwe added 4 commits November 23, 2022 11:25
The use of an unquoted `$__vsc_dbg_trap` as an argument to `eval` was
incorrect, losing whitespace within the expression stored in that
variable.

For illustration of what that means:

```console
$ foo=$'echo "Hello   \n     world!"'
$ eval "$foo"
Hello
     world!
$ eval $foo
Hello world!
```
The previous method of reading the currently-set DEBUG trap was
brittle and incorrect.

It would fail on trap expressions whose strings required more advanced
quoting, e.g. those with embedded single quotes or newlines. It required
unnecessary spawning of external `cut` and `tr` commands, and included
special-casing on '[[', presumably because those tend to be associated
with newlines.

This updates the technique to use bash itself to parse terms of `trap -p
DEBUG` into an array, exactly preserving the original trap expression
string. It includes commentary and a small comment diagram explaining
the process.
@meganrogge
Copy link
Collaborator

might fix #150877

@meganrogge meganrogge added this to the January 2023 milestone Dec 14, 2022
Copy link
Collaborator

@meganrogge meganrogge left a comment

Choose a reason for hiding this comment

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

Works well, thank you 👍🏼

@meganrogge meganrogge requested a review from Tyriar December 14, 2022 15:17
Copy link
Member

@Tyriar Tyriar left a comment

Choose a reason for hiding this comment

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

This is great, thank you! Exactly the type of contributions we're hoping for in shell integration 🙂

@Tyriar Tyriar merged commit cfbd5c3 into microsoft:main Dec 15, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Jan 30, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bash debug trap doesn't work with shell integration for multi-word functions

5 participants