How do I debug HTML and JavaScript together in VSCode (Visual Studio Code)? - debugging

I want to run and debug an html page with a javascript file in a mini website when I hit F5.
How do I configure VSCode to open the html page in the browser and then allow me to set breakpoints in the javescript file which will be triggered by my interaction with the app in the browser?
In Visual Studio this would "just work", because it fires up its own web server, IIS Express. In VSCode I'm not sure how I set up launch.json and/or tasks.json to create a simple node.js web server and serve index.html.
I have seen some examples of debugging javascript apps, for example this launch.json:
{
"version": "0.1.0",
"configurations": [
{
"name": "Launch Bjarte's app",
"type": "node",
"program": "app.js",
"stopOnEntry": true,
"args": [],
"cwd": ".",
"runtimeExecutable": null,
"runtimeArguments": [],
"env": {},
"sourceMaps": false
},
{
"name": "Attach",
"type": "node",
"address": "localhost",
"port": 5858,
"sourceMaps": false
}
]
}
This will run the js file, but I don't understand how I can interact with the app.

It is now possible to debug Chrome web pages in vscode via Chrome remote debugging with a extension released by Microsoft.
Debugger for Chrome
As you can see from that page there are two modes of debugging, Launch and Attach. I only managed to use the Attach mode, probably because i don't have a server running. This extension has all important debug tools functional: local variables, breakpoints, console, call stack.
Another reason to revisit vscode is that it now has IntelliSense support for ECMAScript 6, which displays methods not visible in other "IntelliSense like" solutions I've tried, like SublimeCodeIntel or the latest version of WebStorm.

It seems what I want to do is not possible in VSCode (yet?). My solution at the moment, is to use the node package live-server. Install with
> npm install -g live-server
Then open VSCode, right-click any file in the root folder of your project and select "Open in Console". Then type
> live-server
to start a server with your project as root folder. Live-server will open your default browser and also monitors your project folder for any file changes, and reloads the html page every time you do any changes.
I should mention that my solution using live-server doesn't allow me to debug my app in VSCode, only run it in the browser. I am debugging in Chrome.

Like others have said you install this:
You can use https://marketplace.visualstudio.com/items?itemName=msjsdiag.debugger-for-chrome
And if you are not running a localhost but some HTML and JavaScript you can use this launch.json code.
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch index.html",
"type": "chrome",
"request": "launch",
"file": "${workspaceFolder}/index.html"
}
]
}

VSCode will use node to launch your app, which means your app is running on some PORT. You can interact with your app by visiting http://localhost:PORT/
If you set a breakpoint in app.js it should be hit once you visit your site that is running local via node.
Here is a nice demo https://channel9.msdn.com/Blogs/cloud-with-a-silver-lining/hello-visual-studio-code-nodejs

I didn't want to run a server just for some HTML and JavaScript (unlike a similar example) this VS Code launch configuration along with the 'Debugger for Chrome' extension did the trick on my Windows 10 machine:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch HTML file",
"type": "chrome",
"request": "launch",
"file": "${file}"
}
]
}

In case you have # in path like C:\C#\mypage.htm, you may use FF & ex. fileBasename or Similar variable - does not work in Chrome:
.vscode\launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch HTML file",
"type": "firefox",
"request": "launch",
"file": "C:/C%23/${fileBasename}"
}
]
}
Or simple full path tested with node.js:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/${fileBasename}"
}
]
}

You can use https://marketplace.visualstudio.com/items?itemName=msjsdiag.debugger-for-chrome
In the launch.json you just have to pu the url value of the server that you are using and then you can debug your html + js with your editor visual studio code
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://127.0.0.1:8081",
"webRoot": "${workspaceFolder}"
}
]
}

Related

Cannot debug Rust in Visual Studio Code?

I am trying to debug a Rust program in VS Code, but I get an error:
After clicking OK, VS Code opens "settings.json":
I have these extensions installed:
My program is a simple "hello world" app.
Unfortunately VS Code can't debug Rust out of the box :( But no need to worry, just few steps of configuration will do the work :)
Steps
Install C/C++ extension if you are on windows and CodeLLDB if on OS X/Linux
Click Debug -> Add Configuration, a launch.json file should open, you need to change the program name here manually
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceRoot}/target/debug/foo.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true
},
{
"name": "(OSX) Launch",
"type": "lldb",
"request": "launch",
"program": "${workspaceRoot}/target/debug/foo",
"args": [],
"cwd": "${workspaceRoot}",
}
]
}
Make sure Allow setting breakpoints in any file is checkend under File -> Preferences -> Settings
For detailed steps and more you can refer the article I used to answer this
Credits- Forrest Smith
From your screenshots, you're on Windows, so here's how to proceed. This assumes you already took care of the basics:
VsCode has the recommended rust-analyzer extension installed.
Your project folder was initialized with cargo init. (Your project's folder name must be the same as the name of your package in Cargo.toml.)
You can cargo run from within your project directory and it works.
As indicated by various locations on the 'Net, you need to install another VsCode extension to make it so you can debug. Since you're on Windows, you want to use the MS C++ DevTools extension for VsCode, instead of the CodeLLDB one.
Next, you need a "launch" configuration setup. Select VsCode's Run >>> Add Configuration... menu item. Choose the C/C++: (Windows) launch option in the drop-down. You'll now have a launch.json file with a single configuration object.
You'll need to change the program property to "${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe"; this depends on your package name being the same as the project folder's name. (I also changed the cwd property to "${workspaceFolder}", though I'm not sure it matters.) To be clearer, here's the configuration I have presently in my launch.json file (the preLaunchTask property is for later):
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe",
"preLaunchTask": "rust: cargo build",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"console": "externalTerminal"
}
At this point, as long as you've already built your project at least once, you can hit F5 and debug.
If you want F5 to also save your changes and rebuild your project before debugging, then you also have to add a build task and configure it to run before debugging starts.
To do that, add the build task by opening the Show All Commands box (either F1 or Ctrl+Shift+p) and choosing Tasks: Configure Task. Select rust: cargo build. It'll create a tasks.json file next to your launch.json; the defaults are all you need. My file looks like this:
{
"version": "2.0.0",
"tasks": [
{
"type": "cargo",
"command": "build",
"problemMatcher": [
"$rustc"
],
"group": "build",
"label": "rust: cargo build"
}
]
}
Then, to hook everything up, you just need to manually add the preLaunchTask property to your launch configuration with a value equal to the label in your task. E.g. "preLaunchTask": "rust: cargo build",, like what I have in my example launch.json up above.
At this point, whenever you press F5, VsCode will save your work, rebuild your project, then start debugging it.
Visual Studio Code is a general editor, but it can be configured to debug rust code.
Step 1.
Assuming that Visual Code, rust and cargo are installed, the first step is to enable the required extensions:
rust-analyzer
CodeLLDB
These only need to be installed one.
Step 2
The second step is to create the rust code. I have a folder called Rust, in which I keep the rust code. Changing to that folder, I use cargo new hello_world to create a new rust project.
Step 3
The third step is to change the folder for the project. There are two plausible options, but only one of them will work.
I changed to my Rust folder, and then I can edit the source code by following ``hello_world - src`.
To debug the code, it is necessary to create a launch.json file, using Run - Add configuration... However, the file isn't correct, with <your program> where the correct name should be. This is the wrong approach.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/<your program>",
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
The documentation is a bid thin at this point. The correct approach is to pick a different folder, the top level of the project hello_world. The Cargo.toml file is available.
Now, when Run - Add configuration... is used, and the option of LLDB is selected -
the Cargo.toml file can be picked up -
and then the Cargo.toml file is used to correctly build the launch.json file -
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'hello_world'",
"cargo": {
"args": [
"build",
"--bin=hello_world",
"--package=hello_world"
],
"filter": {
"name": "hello_world",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'hello_world'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=hello_world",
"--package=hello_world"
],
"filter": {
"name": "hello_world",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}
Now, both Run - Start debugging and Run - Run without Debugging both work properly.

"You don't have an extension for debugging 'JSON with Comments'" warning when debugging VS Code theme

I generated the files necessary for creating a color theme in VS Code. I did this with the generator-code node package.
My file structure is as follows
When I run VS Code's debugger, I get this warning that prevents the debugger from running.
Here are the contents of my launch.json file for reference:
{
"version": "0.2.0",
"configurations": [
{
"name": "Extension",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}"
],
"outFiles": [
"${workspaceFolder}/out/**/*.js"
],
}
]
}
In case you're wondering what I'm expecting to happen when I run the debugger, here's the moment in the tutorial I was following where I ran into this problem.
Edit: Well, I evaded the problem somehow by deleting the files and starting over. I'm not sure what was causing the problem before.
This popup only appears for me when trying to launch the debugger while having the launch.json or tasks.json file open. Switching to one of my test files and launching the debugger fixes it
I had this error when my launch.json was not according to the version + configurations scheme, I copy pasted the content of configurations to the file instead of putting it in the array.
{
"name": "Extension",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}"
],
"outFiles": [
"${workspaceFolder}/out/**/*.js"
],
}

Firefox does not open when debugging in Visual Studio Code

When I start debugging with my launch configuration for Chrome, the browser opens. But when I use my launch configuration for Firefox, the browser does not open. A Firefox process is started though. There are no error messages.
Visual Studio Code 1.36.1
Debugger for Firefox 1.8.1
Firefox 68.0.1
Windows 10 1803
Angular 7.0.0 (should not matter)
.vscode/launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Chrome + localhost",
"url": "http://localhost:4200",
"webRoot": "${workspaceFolder}"
},
{
"type": "firefox",
"request": "launch",
"reAttach": true,
"name": "Firefox + localhost",
"url": "http://localhost:4200",
"webRoot": "${workspaceFolder}"
},
]
}
To use attach mode, you have to launch Firefox manually from a terminal with remote debugging enabled. Note that if you don't use Firefox Developer Edition, you must first configure Firefox to allow remote debugging. To do this, open the Developer Tools Settings and check the checkboxes labeled "Enable browser chrome and add-on debugging toolboxes" and "Enable remote debugging" (as described here). Alternatively you can set the following values in about:config:
Preference Name Value Comment
devtools.debugger.remote-enabled true Required
devtools.chrome.enabled true Required
devtools.debugger.prompt-connection false Recommended
devtools.debugger.force-local false Set this only if you want to attach VS Code to Firefox running on a different machine (using the host property in the attach configuration)
Then close Firefox and start it from a terminal like this:
Windows
"C:\Program Files\Mozilla Firefox\firefox.exe" -start-debugger-server
OS X
/Applications/Firefox.app/Contents/MacOS/firefox -start-debugger-server
Linux
firefox -start-debugger-server
Navigate to your web application and use this launch.json configuration to attach to Firefox:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch index.html",
"type": "firefox",
"request": "attach"
}
]
}
If your application is running on a Webserver, you need to add the url and webRoot properties to the configuration (as in the second launch configuration example above).
My configuration and work flow.
P.S. I updated Firefox configuration in about:config, but I didn't get a needed simple result like Google Chrome.
I have installed the following browsers (Ubuntu 20.04 x64):
Firefox for Ubuntu 94.0
Google Chrome Version 96.0.4664.45 (Official Build) (64-bit)
Microsoft Edge Version 96.0.1054.34 (Official build) beta (64-bit)
For all the browsers I didn't change their configuration.
My extensions for debugging (only one): Debugger for Firefox.
My VS Code:
Version: 1.62.3
Commit: ccbaa2d27e38e5afa3e5c21c1c7bef4657064247
Date: 2021-11-17T08:00:36.721Z
Electron: 13.5.2
Chrome: 91.0.4472.164
Node.js: 14.16.0
V8: 9.1.269.39-electron.0
OS: Linux x64 5.11.0-40-generic
Configuration
{
"configurations": [
{
// Start Debug -> Open Firefox, Open Debug console, Click an line in JS File and F5
"type": "firefox",
"request": "launch",
"reAttach": true,
"name": "Firefox + File",
"file": "${file}"
},
{
// Start Debug -> Open Firefox, Open Debug console, Click an line in JS File and F5
"type": "firefox",
"request": "launch",
"reAttach": true,
"name": "Firefox + Server",
"url": "http://localhost:8080/${fileBasename}",
// "url": "http://localhost:8080/index.html",
"webRoot": "${workspaceFolder}/public/"
}
{
"type": "pwa-chrome",
"name": "Chrome + File",
"request": "launch",
"file": "${file}"
},
{
"type": "pwa-chrome",
"name": "Chrome + Server",
"request": "launch",
"url": "http://localhost:8080/${fileBasename}",
// "url": "http://localhost:8080", // the index file
"webRoot": "${workspaceFolder}/public"
},
{
"name": "Edge + Server",
"request": "launch",
"type": "pwa-msedge",
"url": "http://localhost:8080/${fileBasename}",
// "url": "http://localhost:8080",
"webRoot": "${workspaceFolder}/public",
"runtimeExecutable":"/opt/microsoft/msedge-beta/msedge"
}
]
}
I can run my index.html and other html files as a local file and as a server page only in the public directory. On my server the file is in the public directory. I work from the root directory where the public directory is in it.
I have some unpleasant problems only with Firefox. Now it works with the following workaround and the configuration above.
Steps to start debugging in Firefox:
Use the provided configuration and we can change paths or files if it needs. https://code.visualstudio.com/docs/editor/variables-reference But for me it works when I use specified file in the URL, for example, /index.html.
Start the debugging (choose the configuration and press F5).
Firefox have been opened, but the debugging hasn't still worked.
Open DevTools (F12). Yes, without it the debugging doesn't work for me.
Reload the page (press F5 in the browser or Ctrl+R or another combination in your OS).
Debugging has been started with your breakpoints.
I have got the following message of VS Code, but it works without additional paths:
This file's path isn't mapped to any url that was loaded by Firefox. Either this file hasn't been loaded by Firefox yet or your debug configuration needs a pathMapping for this file - do you think the file has already been loaded and want to let the Path Mapping Wizard try to create a pathMapping for you?
I have had a little fix for the Microsoft Edge browser. I set a path to the bin file of the browser: "runtimeExecutable":"/opt/microsoft/msedge-beta/msedge".

Using Visual Studion Code to connect to the gdb-server of the OP-TEE which is running in the QEMU

I am setting up the op-tee in ARM-64. and I'm wondering if it is possible to debug it using visual studio code running under ubuntu 18.04.
So far I was able to compile and run the op-tee in QEMU. and also being able to connect the gdb-server using the command line gdb (following this link: https://www.op-tee.org/docs/debug/).
Now I would like to use some GUI instead of gdb. Since I was working wih visual studio code, so I was wondering if it is possible to configure vsCode to do so?
I have tried installing the cortex-debug extension (I'm not sure if that it the right one) and also tried c/c++ debug attach as well. But I cannot make them work!
Here is my launch.json file:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Attach",
"type": "cppdbg",
"request": "attach",
"program": "${workspaceFolder}/optee_os/out/arm/core/tee.elf",
"miDebuggerServerAddress": "localhost:1234",
"processId": "${command:pickProcess}",
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"text": "optee"
}
]
},
{
"cwd": "${workspaceRoot}",
"executable": "${workspaceFolder}/optee_os/out/arm/core/tee.elf",
"name": "Debug Microcontroller",
"request": "attach",
"type": "cortex-debug",
"servertype": "openocd"
}
]
}
I expect to be able to debug the arm application by remotely connecting to the gdb-server which is running under QEMU using the Microsoft visual code.
Any suggestion of using extensions is appreciated.
I found the solution which works for me:
First it is needed to install the Native Debug extension for VS Code.
Then Add the following configuration into the launch.json file:
{
"type": "gdb",
"request": "attach",
"name": "Attach to QEMU",
"executable": "${workspaceFolder}/optee_os/out/arm/core/tee.elf",
"target": "localhost:1234",
"remote": true,
"cwd": "${workspaceRoot}",
"gdbpath": "~/devel/optee/toolchains/aarch64/bin/aarch64-linux-gnu-gdb"
}
Notes:
You can connect to QEMU only if the app is not running:
It should either be at initial state before typing c in QEMU
or it is stopped in a breakpoint
There should be no other clients connected to it.
Reference:
http://austinhanson.com/vscode-gdb-and-debugging-an-os

"actionssdk-smart-home-nodejs" debug with VSCode

I successfully installed the "actionssdk-smart-home-nodejs" and it works well if i launch it using the command "node index.js" within the folder "...actionssdk-smart-home-nodejs\smart-home-provider".
I need now to debug some part of the code, thus i tried to debug it using VS Code as editor and with the launch.json configured like
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${file}"
}
]
}
When i launch the debugger all seems to start fine, but when i use the Google Chrome browser to access the main page (http://localhost:3000/) i get as response only "Cannot GET /".
It seems like the static route defined in the file "smart-home-provider-cloud.js" app.use('/', express.static('./frontend'));
isn't matched.
So i can't figure out why launching without debugging all works fine and if a start the debugger express returns only "Cannot GET /".
I would be grateful for any help or clue for further investigation !
After long digging i found my issue. The config file for debugging was missing a config line.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${file}",
"cwd":"${workspaceRoot}/smart-home-provi
}
]
}

Resources