Remote development using Visual Studio Code🔗

Visual Studio Code is a code editor developed by Microsoft available for Linux, macOS and Windows. The editor supports several useful features for code development, such as attaching and editing files on a remote system over SSH. This guide will show you how to run an instance of Visual Studio Code on your local machine and attach to a C3SE system for remote development or debugging.

The instructions below assumes you already have Visual Studio Code installed and have a working SSH login to one of our systems.

Note that the instructions belows is for running Visual Studio Code on your local computer.

Prerequisite: Install the Remote-SSH extension🔗

Your installation of Visual Studio Code might not include the extension needed for remote development. You can check if you have the Remote - SSH extension installed by pressing F1 and searching for "Remote SSH". If nothing shows up you are likely missing it. The instructions belows shows you how to install the Remote - SSH extension.

1) Click on Extension (or press Ctrl+Shift+X) and search the Marketplace for "Remote SSH".

2) Select the Remote - SSH extension and click on install. The installation should only take a few seconds.

remote-ssh

Remote attaching to a system🔗

1) Open the Command Palette under View, or press F1, and search for "Remote SSH".

2) Select Remote - SSH: Add New SSH Host

3) Fill in the SSH command as suggested by Visual Studio Code. You must at least include a username (CID) and the hostname of the login node. If you want to connect to Alvis you would enter:

$ ssh <CID>@alvis1.c3se.chalmers.se

4) You will be prompted to select a SSH configuration file (if you are using one).

5) The remote host should now be available. Click on the Remote Explorer button and right click on the remote host and select Connect to Host in Current Window. If everything goes well we can now start to work remotely from Visual Studio Code.

6) Move to the Explorer and select the Open Folder. Pick the default suggested folder (which should be your home directory).

7) You should now see the files in your home directory.


Save often: When editing files remotely make it a habit to you save your work frequently to minimize the risk of losing changes due to broken connections, crashes or login node reboots.


remote illustration

Import a SSH configuration file🔗

If you are already using an SSH configuration file you can import the configuration file in the Remote SSH extension and instantly make all your SSH endpoints (including options) available in the Remote Explorer.

1) Press F1 and search for "Remote-SSH: Open SSH Configuration File..."

2) Select your SSH configuration file.

The default SSH configuraiton file for OpenSSH is ~/.ssh/config on macOS, Linux and WSL, and %programdata%\ssh\sshd_config on native Windows.

Observe that if you have specified an SSH configuration file and use the Remote SSH extension to add a new SSH host, the configuration file will get updated with the new host. If you enter the same host as already defined you will get an duplicate entry.

Attaching to a particular environment🔗

For productivity and development purposes you might want to load a particular environment when you remote attach using Visual Studio Code. This can be done by updating your .bash_profile, as seen in the example below.

# VS-code remote session:
if [ -n "$VSCODE_IPC_HOOK_CLI" ]; then
    module load fosscuda/2020b TensorFlow/2.4.1
fi

The above snippet looks for the environment variable $VSCODE_IPC_HOOK_CLI, which should be exclusive to the Visual Studio Code remote session, and loads TensorFlow.

Using a RemoteCommand to set up a module environment🔗

Using a RemoteCommand allows you to connect to the login node via your local VSCode application, while landing in an environment that already has your modules loaded. You can set up your ssh-config file for this as follows (assuming we want to connect to the vera3 login node):

Host vera3-module-load
    HostName vera3@c3se.chalmers.se
    user username
    RemoteCommand source load_modules.sh; bash
    RequestTTY yes

where load_modules.sh is a file containing the modules load commands that you want to run, e.g.

module load TensorFlow

Open your settings in VSCode, and search for SSH. Make sure Enable remote command is turned on.

Then, you may need to kill any pre-existing session of VSCode - you can do this by hitting F1 in VSCode and typing kill - this should bring up the option Kill VS Code server on host. Select it, select vera3-module-load, and enter your credentials. Wait until you get a confirmation popup that tells you that the remote session has been killed.

Finally, connect to vera3-module-load using Remote-SSH, and enter your credentials. Open a terminal and type module list to verify that your modules have been loaded.

You will need to specify the Python interpreter path in VSCode; you can find out the correct path by typing which python into the terminal.