Start the Server
The magic-script build command sets the debug flag in the digest file by default. This flag is required to enable runtime debugging. The magic-script run command runs the process on the device with mldb launch and watches the output of mldb log for a line specifying the debug port on the device. Once the debug port is identified, the magic-script run command calls mldb forward <port number> to forward the port to the host computer.
Start with a Fixed Port
The debug port is randomly selected by the operating system on the device. There are three ways to set the port manually in your workspace.
Enter in a command line session: magic-script run -p <port number>
Set the INSPECTOR_PORT environment variable in your workspace and enter in a command line session:
magic-script run calls mldb launch and mldb forward for you. To do this yourself, enter in a command line session:
mldb launch -v INSPECTOR_PORT=<port number> --auto-net-privs $APPNAME
To setup the port forward to the host, enter in the command line session:
mldb forward tcp:<port number> tcp:<port number>
Connect to an Inspector Client
Connect to an inspector client in order to debug with inspector clients. This guide covers connecting to and debugging with the Google Chrome DevTools and Visual Studio Code inspector clients.
Connect to Chrome DevTools Inspector Client
There are two ways to connect to the Chrome DevTools Inspector Client:
Copy the chrome URL returned by magic-script run and paste it into the address bar. This is the default workflow.
If you started the server with port 9229, you can enter chrome://inspect in the address bar and click Open dedicated DevTools for Node. Only one app can be debugged at a time on this port.
Note: Although the DevTools UI displays Node.js, MagicScript is running on the device.
Connect to Visual Studio Code Inspector Client:
Configure debugging for your project by following Visual Studio Code's Debugging guide. When a launch.json file is created, add the following to .vscode/launch.json:
Ensure the port number matches the port number either returned from the magic-script run command or the port number you set with MLDB.
With your project open in Visual Studio Code, launch the debugger.
Debug with Inspector
To help debug your code and print out values, you can use console.log(), console.warn(), console.info(), or console.error(). These print their results to the MLDB log as well as the inspector client's console pane.
With Chrome DevTools and other inspector clients, you can live-edit your MagicScript app. The V8 inspector protocol includes limited, built-in support to live-edit your code. To verify if you can live-edit code, change the update handler to log the delta and check if logs print out. The changes made while live-editing are not added to your source files and are only for modifying the state of the current application session.}