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

Not able to override env variables using terminal.integrated.env.osx #98490

Closed
jahan01 opened this issue May 25, 2020 · 11 comments
Closed

Not able to override env variables using terminal.integrated.env.osx #98490

jahan01 opened this issue May 25, 2020 · 11 comments
Assignees
Labels
*as-designed Described behavior is as designed

Comments

@jahan01
Copy link

jahan01 commented May 25, 2020

  • VSCode Version: 1.45.1
  • OS Version: Mac 10.15.4

Steps to Reproduce:

  1. Set a env variable in your shell script ( .zshenv or .bash_profile depending on your default shell):
echo "sourcing path"
echo "Before: $JAVA_HOME"
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.jdk/Contents/Home
echo "After: $JAVA_HOME"
  1. Set your workspace settings.json as
 "terminal.integrated.env.osx": {
        "JAVA_HOME": "/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home",
   }
  1. Launch a terminal and you will see the shell startup scripts overrides the values set by the vscode.
sourcing path
Before: /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
After: /Library/Java/JavaVirtualMachines/sapmachine-jdk-11.0.6.0.1.jdk/Contents/Home

Does this issue occur when all extensions are disabled?: Yes

@RA80533
Copy link

RA80533 commented May 25, 2020

@jahan01, do you happen to have one of the terminal.integrated.shell.* options set in your settings.json file? For example, on macOS, a common configuration might be something similar to the following:

"terminal.integrated.shell.osx": "/bin/bash"

Opening an integrated terminal in VS Code sources your .bash_profile if Bash is set as your shell just as Bash would normally (in fact, I believe VS Code sources your .bash_profile anyway when it starts to set up your development environment, although I could be wrong).

I have two possible fixes for you to try:

  1. Add the following to your settings.json:

    "terminal.integrated.inheritEnv": false
  2. If you're at liberty to, move the offending variable export to a .bashrc file.

@jahan01
Copy link
Author

jahan01 commented May 25, 2020

do you happen to have one of the terminal.integrated.shell.* options set in your settings.json file?

No, by default its empty, so it uses my system default shell ZSH. I was trying with /bin/bash to see if it work, but no luck.

"terminal.integrated.inheritEnv": false

already set and tried with both values

2. If you're at liberty to, move the offending variable export to a .bashrc file.

No, I wanted to override the variable only for this specific project, I dont want to change system wide settings. Hence I am thinking to pass --rcfile <my_file> in terminal.integrated.shellArgs.osx so that this project alone has its own rcfile. This very ugly to have project specific rcfiles just to handle few env vars, but it should work for now, I guess.

Though I have a workaround, I would appreciate if it can be fixed in vscode.

@RA80533
Copy link

RA80533 commented May 26, 2020

I understand your use case now. Yeah, it doesn't quite make sense for your setting not to be taken into effect.

I would take a look at this comment and try to make sure VS Code is allowed to modify your terminal. You can make sure it's allowed by running Terminal: Allow Workspace Shell Configuration in the Command Palette.

If that isn't able to fix your problem, I would look into using a launch.json file for your project and setting the environment like so:

{
  "configurations": [
    {
      "type": "java",
      "env": {
        "JAVA_HOME": "/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home"
      }
    }
  ]
}

@rucciva
Copy link

rucciva commented May 27, 2020

I'm experiencing this too. Using configuration in User settings works though. I can't also find Terminal: Allow Workspace Shell Configuration in the Command Palette.

Screen Shot 2020-05-27 at 18 20 25

@jahan01
Copy link
Author

jahan01 commented May 27, 2020

@RA80533:

You can make sure it's allowed by running Terminal: Allow Workspace Shell Configuration in the Command Palette.

I also dont see that command in command palate, but I got a prompt which asked me to allow workspace to set shell configs, to which I clicked yes.

I am repeating here, it doesnt work only when the same variable is also set by start scripts. The other ones, which are not set in startup scripts are working just fine.

So in my case if have below settings, only JAVA_HOME is overwritten. MY_CUSTOM_VAR is set correctly.

"terminal.integrated.env.osx": {
        "JAVA_HOME": "/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home",
        "MY_CUSTOM_VAR": "my_value"
 }

If that isn't able to fix your problem, I would look into using a launch.json file for your project and setting the environment like so:

This does work only if you want to execute a code but here I want to run built tools like maven package which needs to have correct JAVA_HOME set. I cannot use launch.json for these things.

I have a workaround myself which I have specified in my previous comment. But I dont think every vscode user can use that, as it requires people to be familiar with concepts of start scripts and rcfile. So having this fixed in vscode will be useful for every user.

@joint-song
Copy link

I also got this error on my mac. But when I launch vscode using code from a terminal, all envs would inherited from parent shell well.

@Tyriar
Copy link
Member

Tyriar commented Jun 8, 2020

Terminal: Allow Workspace Shell Configuration

This changed to Terminal: Manage Workspace Shell Permissions.

I am repeating here, it doesnt work only when the same variable is also set by start scripts. The other ones, which are not set in startup scripts are working just fine.

This is working 100% as designed, terminal.integrated.env.* sets the initial environment of the shell, we cannot reliably set it any other way and imo we should not try to. You should tweak your start up scripts somehow to accomplish what you're after.

@Tyriar Tyriar added the *as-designed Described behavior is as designed label Jun 8, 2020
@jahan01
Copy link
Author

jahan01 commented Jun 8, 2020

@Tyriar: i have project specific env variable which needs to override global one. turns out it is a common use case with the people I talk to. Is it possible to support something on the lines of how vscode-python set the virtual env paths? it sends texts after terminal creation.

I was trying to suggest if this feature is available in the core not every extension like vscode-maven and people like me dont need solve this problem independently ?

@Tyriar
Copy link
Member

Tyriar commented Jun 8, 2020

@jahan01 the way the Python extension does it is bad as it could fail and you would never know and spams the screen on launch, I'm trying to get them to adopt a new API to do away with sending a command after the terminal has launched microsoft/vscode-python#11039

I recommend switching your startup scripts to include a condition around JAVA_HOME getting set or something like that, this could be checking TERM_PROGRAM, or based on whether JAVA_HOME is already set or some other variable.

@jahan01
Copy link
Author

jahan01 commented Jun 8, 2020

@Tyriar thanks for your suggestion

@github-actions github-actions bot locked and limited conversation to collaborators Jul 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
*as-designed Described behavior is as designed
Projects
None yet
Development

No branches or pull requests

6 participants
@jahan01 @Tyriar @rucciva @joint-song @RA80533 and others