joelspadin / vscode-remote-x11

Visual Studio Code extension that sets the DISPLAY environment variable in remote workspaces
39 stars 9 forks source link

How to set $DISPLAY for debug configurations started with `"console": "internalConsole"` #41

Open bersbersbers opened 3 years ago

bersbersbers commented 3 years ago

I am using Python, and the following launch configs demonstrate my problem:

    "launch": {
        "configurations": [
            {
                "name": "P1",
                "type": "python",
                "request": "launch",
                "program": "${file}",
            },
            {
                "name": "P2",
                "type": "python",
                "request": "launch",
                "program": "${file}",
                "console": "internalConsole",
            },
        ],
    },

The code:

"""Test pyplot via X11."""
import os

print("DISPLAY =", os.getenv("DISPLAY"))

With P1, I get

DISPLAY = localhost:10.0

With P2, however, I get

DISPLAY = None

I could add an "env" entry to the debug configuration having "console": "internalConsole", but how to get the correct value?

Adding

                "env": {
                    "DISPLAY": "${env:DISPLAY}",
                }

to P2 does not work as it's empty.

bersbersbers commented 3 years ago

I assume this is a bug in either vscode or vscode-python, but as it's pretty specific to DISPLAY in my case (the two envs are identical up to some 10 environment variables all related to terminal- and X11-related things), checking Remote X11 is where I started. So I was looking to see if I can find out in which way DISPLAY is set. I did not find it in the code, however. Would you mind pointing me at that part where you set it? Oh, I just now found it - no idea how I could miss the RemoteHandler before: https://github.com/joelspadin/vscode-remote-x11/blob/3295b2b351ac09103d4a6c612a7edf7b543a9093/extension/src/RemoteHandler.ts#L94-L97

joelspadin commented 3 years ago

Yeah. I'm using an extension API to add environment variables there. I think it only adds them to the integrated terminal though, not to VS Code's own environment.

If you're comfortable with building the extension yourself, what happens if you also add this to that function?

process.env['DISPLAY'] = display;

That should add it to VS Code's environment too, which will hopefully make ${env:DISPLAY} work.

bersbersbers commented 3 years ago

Yeah. I'm using an extension API to add environment variables there. I think it only adds them to the integrated terminal though, not to VS Code's own environment.

That seems to be the case, even if they don't say it explicitly:

Gets the extension's environment variable collection for this workspace, enabling changes to be applied to terminal environment variables.

https://code.visualstudio.com/api/references/vscode-api#ExtensionContext

If you're comfortable with building the extension yourself, what happens if you also add this to that function?

process.env['DISPLAY'] = display;

That should add it to VS Code's environment too, which will hopefully make ${env:DISPLAY} work.

This seems like a good workaround. I did not build myself, but I used https://github.com/GoNZooo/vscode-environment-injector to inject DISPLAY using that method*, and ${env:DISPLAY} worked! So it seems you might add the variable in both places and I adapt my launch configs to pass on that variable.

I might still file this issue with vscode - considering environmentVariableCollection may be useful in internalconsole even though it's not strictly a terminal. (I verified that the problem also appears with Java, so it's not specific to VS Code Python. )