Visual Studio Code

General Knowledge

VSCode is a multi-platform open-source programming editor developed by Microsoft and volunteers. It has support for many programming languages using extensions. This is the recommended editor for Firefox development.

For more general information on the VSCode project see repo.

C/C++ Features and Support

For C++ support we offer an out of the box configuration based on clangd.

Leveraging the clang toolchain compiler we now have support in the IDE for the following features:

1. Syntax highlighting

2. IntelliSense with comprehensive code completion and suggestion

../_images/auto_completion.gif

3. Go-to definition and Go-to declaration

../_images/goto_definition.gif

4. Find all references

../_images/find_references.gif

5. Open type hierarchy

../_images/type_hierarchy.gif

6. Rename symbol, all usages of the symbol will be renamed, including declaration, definition and references

../_images/rename_symbol.gif

7. Code formatting, based on clang-format that respects our coding standard using the .clang-format and .clang-format-ignore files. Format can be performed on an entire file or on a code selection

../_images/format_selection.gif

8. Inline parsing errors with limited auto-fix hints

../_images/diagnostic_error.gif

9. Basic static-code analysis using clang-tidy and our list of enabled checkers. (This is still in progress not all checkers are supported by clangd)

Clangd-specific Commands

Clangd supports some commands that are specific to C/C++:

"clangd.switchheadersource"

This command navigates from the currently open header file to its corresponding source file (if there is one), or vice versa.

This command can be invoked from the command menu (activated via F1), or using its keybinding of Alt+o (Alt+cmd+o on Mac). The keybinding can also be customized in Keyboard Shortcuts.

Generating Configuration

In order to build the configuration for VS Code simply run from the terminal:

./mach ide vscode

After that, subsequent ./mach build invocations will automatically run the Clangd integration.

If VS Code is already open with a previous configuration generated, please make sure to restart VS Code otherwise the new configuration will not be used, and the compile_commands.json needed by clangd server will not be refreshed. This is a known bug in clangd-vscode extension

Useful preferences

When setting the preference

"editor.formatOnSave": true

you might find that this isn’t working on large source code files, but triggering formatting manually works. This is due to the default timeout for formatOnSave, which is quite short (750ms). You might want to increase this timeout, e.g.

"editor.formatOnSaveTimeout": 5000

When using Mercurial in mozilla-central, VS Code will treat your build directories as ordinary directories by default, causing some undesirable behavior including long indexing times, Go to Definition will open files in the build directory instead of the source tree, and Search Files by Name will find duplicate files from the source tree and the build directory (note: when using Git, VS Code will not do this since it reads .gitignore). You can follow these directions to have VS Code largely ignore your build directories:

  1. Go to Preferences -> Settings

  2. Search “exclude” in the Settings

  3. (optional) Select “Workspace” below the search bar to only change this setting for the mozilla-central repository

  4. Under “Files: Exclude”, click “Add Pattern”, type obj-* (assuming your build directory names start with the default text, obj-), and click “OK”

  5. Repeat the step above for the “Files: Watcher Exclude” setting

  6. Reload VS Code: the easiest way to do this is to quit and reopen it.

Despite excluding the build directories above, Go to Definition will still correctly open files that only appear in the build directory such as generated source code. See Bug 1790517 for our effort to automatically exclude the build directories.

Remote Development over SSH

VS Code provides an extension that lets you use any remote machine with a SSH server as your development environment. No matter if it’s Linux based, macOS or Windows, as long as the target machine offers a SSH connection, it can be used for development.

No source code needs to be on your local machine to use VS Code remotely since the extension runs commands and other extensions directly on the remote machine.

In order to setup a connection please follow these steps:

1. Open VS Code and select from the left side panel Remote Explorer

../_images/remote_explorer.png

2. From the Remote Explorer panel select SSH Targets and click on Add and enter the connection details

../_images/remote_explorer_add.png ../_images/remote_explorer_add_wind.png

3. Click on the connection that you just configured at the previous step

4. Finally you should be connected to the desired remote SSH server

../_images/connection_done.png

Please note that during the first connection VS Code will install itself remotely and also install all of the needed dependencies.

Filing Bugs

Bugs should be filed in the Firefox Build System product under Developer Environment Integration, preferably blocking Bug 1662709.