microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
163.52k stars 28.99k forks source link

Support command variables in a terminal profile's env property #173134

Open Apreche opened 1 year ago

Apreche commented 1 year ago

Behavior

I am trying to create a Python integrated terminal profile for VSCode. This integrated terminal should always use the currently selected Python Interpreter. There does not seem to be a way to do that.

Expected vs. Actual

I expect to be able to create an integrated terminal profile that uses the currently selected Python interpreter as part or all of its path.

When creating a custom terminal profile, setting the path to python works, but it does not use the correct Interpreter.

According to the VSCode documentation the path, args, and env properties of the integrated terminal configuration all support resolving variables.

https://code.visualstudio.com/docs/terminal/profiles#_configuring-profiles

Therefore, setting the terminal profile path to ${command:python.interpreterPath} should work. It does not work. The profile will not even show up in the list as a valid choice.

Steps to reproduce:

  1. Make a new Python project in VScode that uses a non-default Python interpreter such as in a virtualenv.
  2. Use the Python: Select Interpreter command to select the interpreter.
  3. Create a .vscode directory in the project.
  4. Create a settings.json file in the .vscode directory
  5. Add the following custom integrated terminal profiles to the settings.json.
{
    "terminal.integrated.profiles.linux": {
        "pythona": {
            "path": "python"
        },
        "pythonb": {
            "path": "${command:python.interpreterPath}"
        }
    }
}
  1. Attempt to open new integrated terminals using these profiles.
  2. The first profile will work, but it will use the wrong Python interpreter.
  3. The second profile will not work at all.

Diagnostic data

karrtikr commented 1 year ago

Thanks for the bug report! We investigate issues in order based on priority and severity, which includes the impact it has on your ability to use the extension to do productive work, and the number of people affected. If other users come forward and leave a comment demonstrating they are seeing/reproducing the problem then we will raise this issue's priority. Thanks for understanding and the patience!

karrtikr commented 1 year ago

${command:python.interpreterPath} is not resolved when reading settings, only in launch.json and tasks.json as far as I'm aware. Transferring this issue to VSCode.

karthiknadig commented 1 year ago

@meganrogge This issue is about resolving variables in settings.json. Currently these are resolved only for launch.json and tasks.json, this should be assigned to someone who own the settings in VS Code.

meganrogge commented 1 year ago

can you just provide the correct python interpreter path to the first profile?

we don't support resolving commands in terminal profiles, so not surprising that the second one fails

Apreche commented 1 year ago

can you just provide the correct python interpreter path to the first profile?

It is very handy to be able to make custom integrated terminals that launch directly into Python interpreters like IPython, or other project-specific Python programs. For example in a Django project someone might want to have an integrated terminal that uses python manage.py shell or python manage.py dbshell as its shell. That saves a lot of hassle compared to opening a bash or zsh integrated terminal and typing an additional command every single time.

The problem is that this will only work if the settings.json is aware of the path to the currently selected Python interpreter. If it is a solo developer that never changes interpreters, yes, they could hard-code the path in there. But that is rarely the case.

In my case, the settings.json with the definitions of integrated terminals is distributed with the project in the .vscode directory. The custom terminals are running some interactive shells that are project-specific. Therefore, every developer on the project would like to share those configurations by having them in the repo, and be universal amongst all developers.

However, each different developer might have a different Python interpreter path based on their dev environment. Even a single developer might have multiple Python interpreters that they switch between for testing or other purposes. VSCode has an "official" way of selecting the current Python interpreter. That selection is recognized in launch.json and also tasks.json. But there is no way I know of to access it from the definition of an integrated terminal.

Therefore, every time the interpreter changes, a developer has to change it in a shared settings.py file and be careful not to commit that change. This is far from ideal.

meganrogge commented 1 year ago

@karthiknadig the fix @Tyriar and I discussed for this would be for the python extension to contribute a terminal profile with the correct path.

Apreche commented 1 year ago

@karthiknadig the fix @Tyriar and I discussed for this would be for the python extension to contribute a terminal profile with the correct path.

That's actually a great idea.

Tyriar commented 1 year ago

@karrtikr I know we're tracking the env var collection changes, but are we tracking a new profile that uses the active interpreter somewhere?