简体   繁体   中英

Cannot override fish default environment variable with vscode launches

When using VSCode and fish as the default user shell, I cannot overwrite the environment variables set by fish in VSCode launch configs.

Example:

in $XDG_CONFIG_HOME/fish/config.fish :

set -x FOO BAR

In.vscode/launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "My launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "/bin/sh",
            "args": ["-c", "echo $FOO"],
            "environment": [
                {
                    "name": "FOO",
                    "value": "OVERWRITTEN"
                }
            ],
            "cwd": "${workspaceFolder}"
        }
    ]
}

I expected this to print OVERWRITTEN , but I got BAR .

It seems VSCode is setting the environment variable, then running the user shell to run my program. Why do I have this behavior with fish but not bash for example? Is there a way to avoid using the user shell? What would be a good practice?

So far, the best fix I could get was to wrap my fish.config in a if status is-interactive .

The environment portion causes VSCode to launch the program (in this case, fish) with an environment variable set. The program may then overwrite that variable, which is what the set command does.

This will also occur with bash or any shell. The most likely reason you are not seeing this with bash is that bash only sources ~/.bashrc for interactive shells ( docs on this ). If you were to set $BASH_ENV to a file, bash would execute that and potentially overwrite environment variables in the same way.

To avoid overwriting the variable in fish, you can conditionalize it on whether you are interactive or not:

status is-interactive && set -x FOO BAR

alternatively you can detect if it's already set:

set -qx FOO || set -x FOO BAR

This second approach implements a "default" value for the variable, allowing the environment to take precedence.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM