Search code examples
visual-studio-codevscode-remote

VSCode settings - how to get a hostname when using Remote-SSH


I am working remotely via the Remote-SSH plugin on a code which is located on an NFS filesystem on a GPU cluster. The project uses CMake. For debugging, I'd like to be able to work out of any machine on the cluster, and I'd like to use machine-specific build directories.

Is there a variable that stores the hostname so I can edit the following line in settings.json appropriately?

"cmake.buildDirectory": "${workspaceFolder}/build-${whatever_variable_stores_hostname}"

Solution

  • It turns out you can use the environment variables in settings.json as expected using ${env:your_var}.

    However, once the VS Code server is started, it keeps running even after closing the client VS Code remote connection. All subsequent connections only check for existing VS Code server installation and connect to it. Therefore, when you update your ~/.bashrc to set additional environment variables, such as:

    export HOST=`hostname`
    

    The VS Code server environment will remain unchanged.

    The solution is to invoke the Remote-SSH Kill VS Code Server on Host command in your VS Code client to force-restart the server on your next login. You will notice that after your next login, the VS Code server installation log will show the output of the printenv command, which should now reflect the updates to the ~/.bashrc file.

    Note on the $HOSTNAME variable

    For some reason, the $HOSTNAME variable which is usually set when you log in to a machine with an interactive session is not set in the environment of the VS Code server instance - that's why I ended up using a custom variable set in ~/.bashrc.