My vscode setup for Mozilla Firefox Gecko coding

Update 2020-09-13: There is now better built-in support for vscode directly in the Firefox repository, by running ./mach ide vscode. See the documentation at https://firefox-source-docs.mozilla.org/contributing/vscode.html

Building Firefox

If not done yet, get your PC configured to build Firefox for the Desktop: https://developer.mozilla.org/en-US/docs/Mozilla/Developer_guide/Build_Instructions/Simple_Firefox_build

I recommend having an “obj” directory outside of the source directory (usually “mozilla-central”, where hg clone should have put all the files, in the instructions above), so that vscode will have fewer files to monitor.
At the top of your source directory, create a text file called “mozconfig” (no file extension), and write this line: mk_add_options MOZ_OBJDIR=@TOPSRCDIR@/../obj-mc
mozconfig can contain other build options, I recommend the following:
mk_add_options AUTOCLOBBER=1
ac_add_options --enable-debug
ac_add_options --enable-debug-symbols
ac_add_options --enable-optimize
(You can change the last line to --disable-optimize when you need to debug something tricky without optimizations hiding and obfuscating the code, but it will make Firefox way slower!)

Compilation Database

After building Firefox with ./mach build, you also need to build a “compilation database” by running ./mach build-backend -b CompileDB; this is how vscode will know which files are #included.
You will need to do this after every build that may have added or removed files, I usually do it after every rebase to a new central, or when I know my patches add/remove files.

Utility Scripts

To make these operations easier, I have a number of small scripts in my ~/.bin directory, which is in my $PATH:
compiledb: ./mach build-backend -b CompileDB
b: ./mach build "$@"
bb: ./mach build binaries "$@"
cb: ./mach clobber && ./mach build "$@"
bcdb: ./mach build "$@" && ./mach build-backend -b CompileDB
cbcdb: ./mach clobber && ./mach build "$@" && ./mach build-backend -b CompileDB

My start-of-the-day routine is hg pull && hg rebase -d central && bcdb. While I work bb rebuilds just what is needed.

Installing vscode and Extensions

Install Visual Studio Code (aka vscode) : https://code.visualstudio.com/

Open vscode, and open the Firefox source directory (“Open folder…” on the welcome page, or File -> Open Folder…).

vscode will suggest a number of extensions to add, it’s probably safe to just add them all. The most important one is “C/C++” by Microsoft.
I would also recommend “Searchfox”. And I also like “Snake Trail”, it’s pretty 😄.

vscode Configuration

Now you need to configure vscode to better understand the Firefox code.

Open your setting as JSON: Ctrl+shift+P or Command+Shift+P and type “settings”, select “Preferences: Open Settings (JSON)”. It’s probably empty the first time, otherwise update it as needed. Here’s mine:

{
    "editor.rulers": [
        80
    ],
    "editor.renderControlCharacters": true,
    "editor.renderWhitespace": "boundary",
    "editor.largeFileOptimizations": false,
    "python.pythonPath": "C:\\mozilla-build\\python3\\python3.exe",
    "C_Cpp.clang_format_fallbackStyle": "Google",
    "C_Cpp.clang_format_path": "C:/Users/squel/.mozbuild/clang-tools/clang-tidy/bin/clang-format",
    "window.title": "${dirty}${activeEditorMedium}",
    "editor.scrollBeyondLastLine": false,
    "files.associations": {
        "*.js": "typescript"
    },
    "html.format.enable": false,
    "javascript.format.enable": false,
    "json.format.enable": false,
    "typescript.format.enable": false,
    "files.eol": "\n",
    "editor.fontFamily": "'JetBrains Mono', Consolas, 'Courier New', monospace",
    "editor.fontLigatures": true,
    "editor.formatOnSave": true,
    "editor.formatOnPaste": true,
    "editor.formatOnType": true
}

Some of these are personal preferences, I’m sure you can work out what you really need vs what you prefer, and how to change some file paths.

Next, you also need to configure the C/C++ extension: Ctrl+shift+P or Command+Shift+P and type “c++”, select “C/C++: Edit Configurations (JSON)”. Here’s mine, for Windows obviously, and I’m working on the profiler so I’ve got some #defines I need:

{
  "configurations": [
    {
      "name": "Win32",
      "includePath": [
        "${workspaceFolder}",
        "${workspaceFolder}/**",
        "${workspaceFolder}/../obj-mc-dbg/dist/include",
        "${workspaceFolder}/../obj-mc-dbg/dist/include/**",
        "${workspaceFolder}/../obj-mc-dbg/ipc/ipdl/_ipdlheaders",
        "${workspaceFolder}/../obj-mc-dbg/ipc/ipdl/_ipdlheaders/**"
      ],
      "defines": [
        "_DEBUG",
        "UNICODE",
        "_UNICODE",
        "XP_WIN",
        "MOZ_GECKO_PROFILER",
        "MOZ_BASE_PROFILER"
      ],
      "windowsSdkVersion": "10.0.17763.0",
      "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/MSVC/14.22.27905/bin/Hostx64/x64/cl.exe",
      "cStandard": "c11",
      "cppStandard": "c++17",
      "intelliSenseMode": "msvc-x64",
      "compileCommands": "${workspaceFolder}/../obj-mc/compile_commands.json"
    }
  ],
  "version": 4
}

The "compileCommands" is what reads the CompileDB we built earlier, and the "includePath"s help find headers that are copied or generated in a few places in the obj directory.

From here, vscode should be able to interpret most C++ files, and navigate to other symbols: Right-click on a symbol and you should see “Go to definition” and other commands.

BEWARE! When going to declarations or definitions, vscode may open a copy in the obj directory, meaning that changes there will not actually modify the original file, and you will probably lose your changes when building again!
To avoid such mistakes I would recommend installing both:
– The “Workspace Watchdog” extension, which changes the background color of files outside the workspace.
– The “Read-Only Mode Support” extension, which will warn you when you modify and save a file outside of the workspace.

Other Resources

Developing Mozilla C++ code with clangd and VSCode — This may provide more accurate results than my methods above, but I have not tried it yet.

searchfox.org — The best online way to navigate the Firefox source code.

JetBrains Mono — “A typeface for developers”, free, with ligatures! A great successor to Consolas.