shellIntegration-bash.sh: exactly preserve DEBUG trap expression#165581
Merged
Tyriar merged 5 commits intomicrosoft:mainfrom Dec 15, 2022
Merged
shellIntegration-bash.sh: exactly preserve DEBUG trap expression#165581Tyriar merged 5 commits intomicrosoft:mainfrom
Tyriar merged 5 commits intomicrosoft:mainfrom
Conversation
c35e96e to
bc6b43b
Compare
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.
Collaborator
|
might fix #150877 |
meganrogge
approved these changes
Dec 14, 2022
Collaborator
meganrogge
left a comment
There was a problem hiding this comment.
Works well, thank you 👍🏼
Tyriar
approved these changes
Dec 15, 2022
Member
Tyriar
left a comment
There was a problem hiding this comment.
This is great, thank you! Exactly the type of contributions we're hoping for in shell integration 🙂
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
__vsc_dbg_trapmanagement 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
trandcututilities, 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