Skip to content

IDE Development Using Visual Studio Code

Zach Hannum edited this page Nov 2, 2022 · 11 revisions

Intro

This is a setup for using Visual Studio Code as your development environment. The notes here assume you're on macOS and a VM is being used for your development. Even though the setup here was intended to work with server builds, there's nothing preventing it to be used for Desktop builds as well.

Basics

Download https://code.visualstudio.com/download

Keyboard Shortcuts

  • Cmd+P: Search for files
  • F1/Cmd+Shift+P: List all available VSCode commands
    • Reload window
  • Cmd+F: Search
  • Shift+Cmd+F: Global search
  • Control+~: Terminal toggle

Configure Auto-Save

Otherwise, you have to save files individually.

https://code.visualstudio.com/docs/editor/codebasics#:~:text=Save%20%2F%20Auto%20Save%23,to%20explicitly%20save%20the%20file.

Remote Dev Extension

https://code.visualstudio.com/docs/remote/remote-overview

Remote SSH

https://code.visualstudio.com/docs/remote/ssh-tutorial

Install extension:

Name: Remote - SSH
Id: ms-vscode-remote.remote-ssh
Description: Open any folder on a remote machine using SSH and take advantage of VS Code's full feature set.
Version: 0.51.0
Publisher: Microsoft

VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-ssh

Name: Remote - SSH: Editing Configuration Files
Id: ms-vscode-remote.remote-ssh-edit
Description: Edit SSH configuration files
Version: 0.51.0
Publisher: Microsoft

VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-ssh-edit

Tips

Use a SSH Agent locally for GitHub SSH key forwarding to remote:

  • ssh-add -k
  • eval "$(ssh-agent -s)"
  • Add to your shell

Confirm it works on a remote terminal in VSCode: ssh -T git@github.com

Beware: No hosts with ports numbers, configure your local SSH for forwarding alias Example ~/.ssh/config alias:

Host ide
    HostName localhost
    Port 2222
    ForwardAgent yes

Beware: Remote SSH extension won't work unless your shell on the remote host is bash. Change your login shell to bash if you experience a timeout when connecting. See https://github.com/microsoft/vscode-remote-release/issues/2509 for details.

Windows

On Windows we use Ninja as our make program. This requires some custom configuration.

First, make sure you have the CMake and CMake Tools extensions installed.

Next add a new CMake toolkit for Ninja by modifying the cmake-tools-kits.json file as follows:

  1. Use ctrl-shift-p to open the command pallet.
  2. Select CMake: Edit User-Local CMake Kits to open cmake-tools-kits.json in the VS Code editor pane.
  3. Make a copy of the Visual Studio Build Tools kit with the correct architecture:

image

  1. Change the following fields
    • "name" from "Visual Studio Build Tools 2017 Release - amd64" to something else, e.g. "Ninja"
    • the "name" field in the "preferredGenereator" object to "Ninja" (case matters)
    • remove the "platform" and "toolset" fields from the "preferredGenereator" object

image

  1. Save and close this file.

Next, it is necessary to set the path to both ninja.exe and cl.exe by doing the following:

  1. Use ctrl-, to open the Settings pane.
  2. Under Extensions select CMake Tools configuration.
  3. Scroll down to find the Cmake: Configure Settings entry and select Edit in settings.json to open the settings.json file.
  4. In the "cmake.configurationSettings" object that has been created, add the following three values:
  • "CMAKE_MAKE_PROGRAM": "<drive>:/Path/To/ninja.exe"
  • "CMAKE_C_COMPILER": "<drive>:/Path/To/cl.exe"
  • "CMAKE_CXX_COMPILER": "<drive>:/Path/To/cl.exe"
  1. Save and close this file.

Next, delete any pre-existing build folder and restart VS Code. Then from the kit selection menu, choose the new Ninja kit and configure CMake. You may find that CMake is unable to find the correct Qt executable or R executable. If that's the case, open the Settings > Extensions > CMake Tools configuration as above, scroll to CMake: Configure Args and pass additional arguments to CMake. For Qt, pass -DQT_QMAKE_EXECUTABLE=C:/path/to/qmake.exe (e.g. C:/Qt/5.12.10/msvc2017_64/bin/qmake.exe). For R, pass the path to the bin/R.exe for the appropriate version of x64 R as DLIBR_HOME=C:/path/to/R (e.g. C:/R/R-devel). By default, CMake will find the version of R that has been most recently added to the registry.

Extensions:

Name: C/C++
Id: ms-vscode.cpptools
Description: C/C++ IntelliSense, debugging, and code browsing.
Version: 0.29.0
Publisher: Microsoft

VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools

IntelliSense C++

Edit the C/C++ configuration (Cmd + Shift + P, C/C++: Edit Configurations) file to include the libs. This is what you want it to look like:

{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/**",
                "/opt/rstudio-tools/arm64/**", //path to where the RStudio dependencies were installed
                "/usr/local/include/**", //this has some R includes
                "${workspaceFolder}/../build-server/session" //this has a C header that will be present after your first build, adjust path to where you are building RStudio
            ],
            "defines": [],
            "macFrameworkPath": [],
            "compilerPath": "/opt/homebrew/bin/gcc-11",
            "cStandard": "gnu11",
            "cppStandard": "gnu++11",
            "intelliSenseMode": "macos-gcc-arm64"
        }
    ],
    "version": 4
}

The includePath is what you'll update with any necessary includes. The paths should have the R includes, Boost, and your workspace. The Problems view will tell you if IntelliSense can't find any headers and you can update the include paths in the configuration.

Debugging C++

Sample configuration for attach:

        {
            "name": "(gdb) rserver",
            "type": "cppdbg",
            "request": "attach",
            "program": "/usr/lib/rstudio-server/bin/rserver",
            "processId": "${command:pickProcess}",
            "MIMode": "gdb",
            "logging": { "engineLogging": true, "trace": true, "traceResponse": true },
            "setupCommands": [
                //{
                //    "description": "Follow children",
                //    "text": "-gdb-set follow-fork-mode child",
                //},
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                },
                {
                    "description": "Enable all-exceptions",
                    "text": "-exec \"catch throw\"",
                    "ignoreFailures": true
                }
            ]
        }
Name: CMake Tools
Id: ms-vscode.cmake-tools
Description: Extended CMake support in Visual Studio Code
Version: 1.4.1
Publisher: Microsoft

VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-vscode.cmake-tools

gdb Debugging with Remote SSH

If you are attempting to debug using GDB with the configuration above and your VS Code environment is setup to develop remotely via the Remote - SSH Extension (common when developing with a Linux VM and running VS Code locally), you may run into some issues and errors when attempting to run the debug configuration.

Allow ptracing by non-root users

One error that may crop up when trying to debug is:

==== AUTHENTICATING FOR org.freedesktop.policykit.exec ===
Authentication is needed to run `/usr/bin/gdb' as the super user
Authenticating as: Parallels (parallels)
Password: [1] + Stopped (tty output)       /usr/bin/pkexec "/usr/bin/gdb" --interpreter=mi --tty=${DbgTerm} 0<"/tmp/Microsoft-MIEngine-In-abwd3x5z.w1d" 1>"/tmp/Microsoft-MIEngine-Out-avlro1zq.zz5"
You have stopped jobs.

To resolve this, ptracing by non-root users can be allowed by running:

echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope

The file /etc/sysctl.d/10-ptrace.conf can also be modified to change this setting permanently:

kernel.yama.ptrace_scope = 0

See this page for more troubleshooting information regarding this solution.

Running gdb with sudo

Unfortunately, this may not be enough to get debugging working over Remote SSH. You may still see issues such as error messages attempting to attach to processes, or missing debug symbols (unable to set breakpoints or see the call stack, for example). You may also need to configure your launch.json to run gdb with sudo.

Create an executable script to launch gdb with sudo, such as:

gdb.sh:

#!/bin/bash
sudo /usr/bin/gdb "$@"

Then, in your launch.json configuration, add the property:

"miDebuggerPath": "$PATH_TO/gdb.sh",

This modification, along with the change to the ptrace_scope setting, should allow you to debug remotely via VS Code.

GWT:

Id: vscjava.vscode-java-pack
Description: Popular extensions for Java development and more.
Version: 0.9.1
Publisher: Microsoft

VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack

Git Support

Built-in is enough for most uses:

Various states:

Remove change from commit:

Add change to commit or revert:

Optional Git Extensions:

Name: Git Extension Pack
Id: donjayamanne.git-extension-pack
Description: Popular Visual Studio Code extensions for Git
Version: 0.1.3
Publisher: Don Jayamanne

VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=donjayamanne.git-extension-pack

Options with extension:

Without:

Other Optional Extensions:

Name: Project Manager
Id: alefragnani.project-manager
Description: Easily switch between projects
Version: 11.1.0
Publisher: Alessandro Fragnani

VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=alefragnani.project-manager

Name: Restore Git Branch Tabs
Id: gkotas.restore-git-branch-tabs
Description: Remembers and restores which tabs were opened for each git branch.
Version: 0.4.1
Publisher: gkotas

VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=gkotas.restore-git-branch-tabs

Name: Visual Studio IntelliCode
Id: visualstudioexptteam.vscodeintellicode
Description: AI-assisted development
Version: 1.2.9
Publisher: Microsoft

VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=VisualStudioExptTeam.vscodeintellicode

Name: Rainbow Brackets
Id: 2gua.rainbow-brackets
Description: A rainbow brackets extension for VS Code.
Version: 0.0.6
Publisher: 2gua

VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=2gua.rainbow-brackets

Clone this wiki locally