Skip to content

Fix 243417: sanitize env command output for bash integration #243673

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

Closed
wants to merge 4 commits into from

Conversation

ryenus
Copy link
Contributor

@ryenus ryenus commented Mar 16, 2025

In the current bash shell integration implementation, it only considers the lines of simple key/value pairs while processing the output of the env command. However, when there're exported shell functions, which are typically printed over multiple lines, there could be lines with arbitrary shell script that might lead to unexpected result, causing errors like:

bash: vsc_aa_env: bad array subscript
bash: currentEnvMap["$key"]: bad array subscript

Fortunately with env -0, it becomes possible to join the lines of an exported function into one line with a custom line separator, such as '\r', or any other unused ascii control characters. With that we could also exclude the exported functions from code completion suggestions:

image

Fixes #243417

@Tyriar Tyriar requested a review from anthonykim1 March 17, 2025 12:33
@Tyriar Tyriar added this to the March 2025 milestone Mar 17, 2025
@Tyriar
Copy link
Member

Tyriar commented Mar 17, 2025

@anthonykim1 could you review+test this one?

ryenus added 4 commits March 18, 2025 11:38
Currently the shellintegration-bash.sh assumes the env output includes
only lines of simple key=value pairs. As a result, when an exported
function is displayed over multiple lines, such assumption no longer
holds. Hence we "sanitize" env output by excluding exported functions.
So that we only need to run the env command once.
- avoid redundunt assosicate array support checks
- avoid unbound variable access issues
- avoid leaking function local variables
- prefer to check array length for emptiness
@ryenus ryenus changed the title Fix 243417: sanitize env command output for shell integration Fix 243417: sanitize env command output for bash integration Mar 18, 2025
@anthonykim1 anthonykim1 modified the milestones: March 2025, April 2025 Mar 23, 2025
@anthonykim1
Copy link
Contributor

anthonykim1 commented Mar 31, 2025

Hi @ryenus Thanks much for putting up this PR.

I have to admit, we did suffer through some pain in the bash shell integration script where we had bunch of debris from user's env show up in the terminal.

I did make some changes recently to resolve this and separate the envs with null delimiter here
Relevant PR here

Would you be able to check on the latest vscode-insiders version how this would behave on your setup?
I'm wondering if the check I've merged is smart enough.

I feel like it may still be worth taking the suggestion from BASH_FUNC, but still worth checking first.
Thanks,

@ryenus
Copy link
Contributor Author

ryenus commented Apr 1, 2025

I feel like it may still be worth taking the suggestion from BASH_FUNC, but still worth checking first. Thanks,

@anthonykim1, good that you brought this up, please consider these:

  1. The exported bash functions are excluded by bash completion
  2. How to use the suggested $BASH_FUNC_<function_name>%% variables? For what?

With that I'd suggest to NOT include the BASH_FUNC entries, and the code would performs better with less entries to process.

Meanwhile please also checkout the fixes in minor shell integration code style fix (cc42b1d). While env -0 might have fixed the unbound variable errors, we'd still need to avoid leaking function local variables that might unexpected overwrite existing shell variables with the same name(s).

@lszomoru lszomoru modified the milestones: April 2025, May 2025 May 5, 2025
@Tyriar Tyriar modified the milestones: May 2025, June 2025 Jun 4, 2025
@anthonykim1 anthonykim1 modified the milestones: June 2025, July 2025 Jun 9, 2025
@Tyriar Tyriar modified the milestones: June 2025, July 2025 Jul 1, 2025
@Tyriar Tyriar removed their assignment Jul 1, 2025
@anthonykim1
Copy link
Contributor

Thanks @ryenus
Do you mind revisiting this after resolving merge conflicts? There were bunch of changes in this script over the past months.

@ryenus
Copy link
Contributor Author

ryenus commented Jul 8, 2025

Do you mind revisiting this after resolving merge conflicts? There were bunch of changes in this script over the past months.

@anthonykim1, may I ask what would you expect as of now?

  1. I can see that you had already taken the idea of using env -0, as you mentioned earlier:

    I did make some changes recently to resolve this and separate the envs with null delimiter here
    Relevant PR Fix prompt glitch on zsh, bash when environment reporting is set to true #243804

  2. meanwhile the original issue no longer exists since you now use a fixed set of env vars as with:
    const scopedDownShellEnvs = ['PATH', 'VIRTUAL_ENV', 'HOME', 'SHELL', 'PWD'];

@anthonykim1
Copy link
Contributor

anthonykim1 commented Jul 8, 2025

@ryenus Sounds good, just wanted to make sure I didn't miss anything before I close this out.
btw env -0 was really useful.

# Filtered env output without exported functions
 __vsc_env() {
 	command env -0 | tr '\0\n' '\n\r' | sort | grep -Ev '^BASH_FUNC'
 }

May come in handy in the future when we make the env setting configurable as opposed to being the "hardcoded" list right now.

I'd /cc you then :)

@anthonykim1 anthonykim1 closed this Jul 8, 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.

[Shell Integration Error] bash: vsc_aa_env: bad array subscript
4 participants