-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Debugging with Visual Studio Code
This page contains tips for editing and debugging scratch-gui
, scratch-vm
, scratch-render
, and related repositories using Visual Studio Code, also known as VS Code. This page does NOT contain tips for using Visual Studio, which is a separate development environment with a similar name.
VS Code by itself provides a very basic experience; for a more complete editing and debugging experience you'll need to install some extensions. This section lists some extensions that work well with the Scratch 3.0 repositories.
Please note that the extensions marketplace changes rapidly and this information may be out of date; this is just meant as a starting point.
Extension Name | Extension ID / search term | Notes |
---|---|---|
Code Spell checker | streetsidesoftware.code-spell-checker | |
Debugger for Chrome | msjsdiag.debugger-for-chrome | See below for setup help |
EditorConfig for VS Code | editorconfig.editorconfig | |
EJS language support | digitalbrainstem.javascript-ejs-support | |
ESLint | dbaeumer.vscode-eslint | |
GitLens — Git supercharged | eamodio.gitlens | |
GLSL Lint | cadenas.vscode-glsllint | Specifically for editing shaders in scratch-render
|
HTML CSS Support | ecmel.vscode-html-css | Primarily for scratch-gui
|
Markdown All in One | yzhang.markdown-all-in-one | |
markdownlint | davidanson.vscode-markdownlint | |
Node.js Modules Intellisense | leizongmin.node-module-intellisense | |
npm | eg2.vscode-npm-script | |
Visual Studio IntelliCode | visualstudioexptteam.vscodeintellicode | |
XML | redhat.vscode-xml | |
YAML | redhat.vscode-yaml |
These instructions will focus on debugging in Chrome or Chromium. It should be possible to use other browsers but you'll need different VS Code extensions and at least some of the setup instructions will be different.
Goal: Install all software necessary for a basic Scratch 3.0 debugging experience.
- Install VS Code
- Download: https://code.visualstudio.com/
- Homebrew:
brew cask install visual-studio-code
- Chocolatey:
cinst vscode
- Install Chrome or Chromium
- NOTE: Consider using a different browser from the one you use for regular browsing. It can occasionally be nice to restart the browser for debugging purposes and that can be annoying if it also means closing all your
Stack Overflow and MDNreference tabs. - Download
- Chrome: https://www.google.com/chrome/
- Chromium: https://chromium.woolyss.com/ (WARNING: this is an unofficial site)
- Homebrew:
brew cask install google-chrome
orbrew cask install chromium
- Chocolatey:
cinst GoogleChrome
orcinst chromium
- NOTE: Consider using a different browser from the one you use for regular browsing. It can occasionally be nice to restart the browser for debugging purposes and that can be annoying if it also means closing all your
- In VS Code, install the "Debugger for Chrome" extension (search for
msjsdiag.debugger-for-chrome
) - Clone the Scratch 3.0 repositories: see Getting Started
Goal: Edit Scratch 3.0 files in VS Code.
I like to set up a multi-root workspace in VS Code so that I can see the code for multiple repositories all together. This also allows you to save the workspace files outside the repository clone. Keep in mind that you'll need to use npm link
(or something similar) to make full use of a multi-root workspace. See the "linking repos" section of the Getting Started page and the associated example for more details.
Using a single-root workspace is simpler but won't make it easy to work in multiple repositories. In addition, VS code will leave some files in a .vscode
subdirectory in your repository. Be careful not to include this directory when making commits that you intend to include in a PR for the official Scratch 3.0 repositories, as we generally avoid storing files for any IDE in Scratch 3.0 repositories.
- Pick "Open Folder..." under the "File" menu.
- Select your
scratch-gui
repository.
Further setup steps will involve editing the launch configuration for your workspace. To do this in a single-root workspace:
- Open the "Run" panel in the VS Code UI (looks like a bug and a triangle)
- Depending on the state of your workspace, one of two options will appear:
- If you see "create a lunch.json file" then select that and, in the drop-down menu, select "Chrome"
- Otherwise, click the "Open launch.json" button near the top (looks like a gear)
Before moving on, make sure there is a "configurations" item at the top level. That is, the minimum launch.json
should look something like:
{
"version": "0.2.0", // do not edit this version number
"configurations": [ // this section may be empty or may have an entry with type "chrome"
]
}
From here on we will manipulate only the configurations
section of this file.
- Pick "Add Folder to Workspace..." under the "File" menu.
- Select your
scratch-gui
repository. - Repeat steps 1 and 2 for each additional repository you plan to edit or debug.
- I suggest starting with at least
scratch-gui
andscratch-vm
.
- I suggest starting with at least
Further setup steps will involve editing the launch configuration for your workspace. To do this in a multi-root workspace:
- Select the gear icon in the lower left of the VS Code window
- Select "Settings"
- Select "Workspace"
- Type "launch" into the search bar, which should present you with the "Launch" setting under the "Debug" section of "Features"
- Select "Edit in settings.json"
Before moving on, make sure there is a "launch" item at the top level with a "configurations" item underneath it. That is, the minimum workspace configuration should look something like:
{
"folders": [
// no need to edit the folders section
],
"launch": {
"configurations": [], // this line is REQUIRED
"compounds": [] // this line is optional
}
}
From here on we will manipulate only the configurations
section of this file.
Goal: Hit breakpoints and inspect state using VS Code
- If you haven't already, start the webpack dev server by running
npm start
in thescratch-gui
directory. It takes a while to fully start up, so let this run in a separate terminal window in the background. - In your
configurations
section, add a "Chrome: Launch" entry. If you start typing "Chrome" you should be able to pick "Chrome: Launch" from a list then modify it, or you can just copy & paste this:
{
"type": "chrome",
"request": "launch",
"name": "Launch scratch-gui in Chrome",
"sourceMapPathOverrides": {
"webpack://GUI/./*": "${workspaceFolder}/*", // use ${workspaceFolder:scratch-gui} in a multi-root workspace
},
"url": "http://localhost:8601", // note the port number!
"webRoot": "/:" // map other / generated files to an impossible path
}
- Temporarily add a
debugger
statement somewhere in the code. I recommend putting it near the top ofscratch-gui/src/lib/blocks.js
:
import ScratchBlocks from 'scratch-blocks';
debugger;
- Pick "Start Debugging" in VS Code (select the green triangle, or the item under the "Run" menu, or press F5, or...)
- You should see Chrome or Chromium start up and start to load
scratch-gui
- When the
debugger
statement is reached, the program should pause in VS Code and allow you to inspect local variables, the call stack, etc.- If this doesn't work, stop here and troubleshoot!
- Change the
debugger;
statement to something else, likeconsole.log("hello");
or something. - Use VS Code to place a breakpoint on that line 8 Pick "Start Debugging" again to verify that the breakpoint works
Congratulations! You can now use VS Code to debug Scratch 3.0 code!
Goals:
- Get VS Code to display the non-minified code for
scratch-vm
, and ideally all dependencies - Get VS Code to hit breakpoints in
scratch-vm
, and ideally all dependencies
Setting your workspace's sourceMapPathOverrides
configuration like this is a step in the right direction:
"sourceMapPathOverrides": {
"webpack://GUI/./*": "${workspaceFolder:scratch-gui}/*",
"webpack://GUI/./node_modules/scratch-vm/*": "${workspaceFolder:scratch-vm}/*",
},