Given the following auto-generated Visual Studio Code launch.json config:
I'd like this to launch the browser to localhost:5000/swagger when I debug, but I've tried half a dozen different things and nothing works. It just opens to localhost:5000. What am I missing here? There's no general documentation (that I could find) on all of the attributes available aside from hitting Ctrl+space to see a list, which doesn't help much.
I left out my failed attempts at getting this to work how I want...
{
"name": "Launch Demo.Api",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceRoot}/Demo.Api/bin/Debug/netcoreapp2.1/Demo.Api.dll",
"args": [],
"cwd": "${workspaceRoot}/Demo.Api",
"stopAtEntry": false,
"launchBrowser": {
"enabled": true,
"args": "${auto-detect-url}",
"windows": {
"command": "cmd.exe",
"args": "/C start ${auto-detect-url}"
},
"osx": {
"command": "open"
},
"linux": {
"command": "xdg-open"
}
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
this one also works for me on VSCode 1.39.2
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)",
"uriFormat": "%s/swagger"
},
This one works for me:
"launchBrowser": {
"enabled": true,
"args": "${auto-detect-url}",
"windows": {
"command": "cmd.exe",
"args": "/C start ${auto-detect-url}/swagger"
}
}
I tried the following and it seems to work
"launchBrowser": {
"enabled": true,
"args": "${auto-detect-url}/swagger",
An alternative would be using VSCode 1.48 (July 2020), which adds a Debug: Open Link command:
A new Debug: Open Link command has been added to quickly debug any URL.
Previously, to debug a browser, you had to install the Debugger for Chrome extension and write a launch.json config file to debug a page.
This command allows you to debug any URL without needing additional launch configurations.
Theme: Earthsong
If you have a URL selected in your active editor, it will open that automatically.
Otherwise, VS Code will prompt you to enter a URL, pre-filling with the URL in your clipboard, if any.
You can adjust the debug configuration used in this command via the debug.javascript.debugByLinkOptions setting.
That last setting can be used for specifying the right certificate
By default we'll use a different user data dir per workspace.
You can use a "stable" directory by adding something like to your user settings:
"debug.javascript.debugByLinkOptions": {
"userDataDir": "C:/Users/user/my-user-data-dur"
}
This will then allow any flags or settings you have to stick. I'm not sure which flags you need to fiddle with to make Chrome happy, but that config should let you set them in a way that won't be reset.
And VSCode 1.50 (Sept. 2020) will improve that feature:
Adding a button to launch it directly from the Run and debug tab (instead of having to open the Palette).
Currently the Run and debug tab only proposes to create a launch.json file, or to use Node.js Debug Terminal, but now it would be simpler to propose the Open link feature directly.
It would also be great if, when the URL is filled, it is automatically saved in .vscode/settings.json, to avoid to have to fill it again every time.
See commit dc22997 as a result.
If you are debugging node project this worked for me.
launch.json in my front-end app (Angular)
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch via npm",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "npm",
"runtimeArgs": [ "run-script", "start" ],
"console": "externalTerminal"
}
]
}
package.json that contains the npm scripts
In my case since I was debugging Angular using ng serve I had to specify the --open=true argument so the browser would launch
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve --ssl --ssl-cert %APPDATA%\\ASP.NET\\https\\%npm_package_name%.pem --ssl-key %APPDATA%\\ASP.NET\\https\\%npm_package_name%.key --open=true",
"build": "ng build",
"watch": "ng build --watch --configuration development",
"test": "ng test",
"prestart": "node aspnetcore-https"
},
So i had this issue trying to run a C# API on Linux and this is the solution that worked for me
in .vsocde/launch.json add this in the configuration property
"launchBrowser": {
"enabled": true,
// change this to your OS name, linux, osx or windows
// you also need to change the command since xdg-open only works on linux
"linux": {
"command": "xdg-open",
"args": "${auto-detect-url}/swagger/index.html"
}
}
Related
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.
The '+' button to open terminal in my visual studio code does not work.
Also, the default profile selection button does not work.
The only thing I changed was from "launch.json" to "externalConsole: false->true".
Even if I try to restore it to its original state, it remains the same. I tried reinstalling the VSCode, deleted the "setting.json" file, and recreated it. But... :(
Originally, I was using git bash as a standard, but you can see it. There is only "JavaScript Debug Terminal" left.
Please help me, friends.
My "launch.json" file
"version": "0.2.0",
"configurations": [
{
"name": "gcc.exe - 활성 파일 빌드 및 디버그",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "C:/MinGW/bin",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "gdb에 자동 서식 지정 사용",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe 활성 파일 빌드"
}
]
I am on macOS with an issue like this as well. My terminal instantly closes when I try to open it and I have not found a good solution. The best I can do it just reinstall VScode.
I use Windows, hence commands are for Windows, but you can definitely find the alternative commands for your OS in case you happen to use an OS other than Windows.
Open your settings.json file(File -> preferences -> Settings or Ctrl+,) and look for the configuration terminal.integrated.profiles.windows (newer recommended setting). If you seeterminal.integrated.shell.windows then it is the older deprecated setting.
If you are on linux or Mac, then look for the terminal.integrated.profiles.linux or terminal.integrated.profiles.osx. If you don't find these, then perhaps you don't have any terminal profiles setup and you need to set it up.
Setting up a terminal profile is quite easy.
In your settings.json file you need to create a new setting with key terminal.integrated.profiles.windows (or terminal.integrated.profiles.linux or terminal.integrated.profiles.osx based on your system). Start typing the above key and once VSCode shows the suggestion hit Enter(Return). If you don't see any suggestion for auto-complete try hitting Ctrl+Space. Your settings will auto-populate against the above key and will look something like following:
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"icon": "terminal-powershell"
},
"Command Prompt": {
"path": [
"${env:windir}\\Sysnative\\cmd.exe",
"${env:windir}\\System32\\cmd.exe"
],
"args": [],
"icon": "terminal-cmd"
},
"Git Bash": {
"source": "Git Bash"
}
}
In addition to above you can also setup a default terminal profile. Include the below setting(here Git Bash has been configured as the default terminal profile) :
"terminal.integrated.defaultProfile.windows": "Git Bash"
You can always use the Ctrl+Space to force VScode to provide you with the possible values.
Few handy links :
To create a new profile.
To go to the command pallette(Ctrl+Shift+P on windows)
I recently have been using GDB to debug a C++ program. For standard usage, I usually do:
$ cd compiledir
$ compilescript
$ gdb compiled.out
$ run inputfile
compilescript is a program that compiles the code for the particular software I am working on, and only works in compiledir. It reads an external file for compiler flags. For gdb, I include the neccisary -g flag. This works for me to debug via the text interface. However, this text interface is becoming increasingly frustrating to use compared to a IDE, and I know that Visual Studio uses gdb as a backend to debug C++ files by default.
To get started, I let visual studio generate default C++ debugging configurations and tried to change the commands, but I have no idea what it's doing, and there doesn't appear to be much documentation on making custom build/debug configurations, particularly for for the linux version of VScode.
Currently I have:
launch.json (default)
{
// 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": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "Custom Build",
"command": "compilescript" ,
"options": {
"cwd": "compiledir"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compile using the custom build script."
}
],
"version": "2.0.0"
}
Running this task, and running it indirectly through the launch.json file both fail. I get the following:
> Executing task: Custom Build<
Starting build...
and it hangs there. I tried to put echo into the build command to see if it was even running, or have it create a file. However, it's like nothing is being run at all and I can't see any printing or files being created anywhere.
What is the proper way to create a custom GDB build/debug task for Visual Studio (Linux Edition)?
Update
I've edited both tasks.json and launch.json, and am able to compile successfully and run GDB with the inputfile I want. However, the environment variables are not configuring properly. I need environment variables to be configured to properly run the inputfile. I currently have:
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": compiled.out,
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [{"VAR1": "VAR1_VALUE", "VAR2": "VAR2_VALUE"}],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
However, when I run the build/debug task, it builds, but doesn't debug, and GDB complains of an error. It appears the "environment" variable is setting something, but I get the following error:
Unable to start debugging. Unexpected GDB output from command "-interpreter-exec console "set env"". Argument required (environment variable and value).
This is how you can set up debugging in C++ without using any Task extensions. Only the launch.json file will be needed.
First, compile your program with the debug flag: -g, and keep it anywhere in your folder.
For example in your active folder (or workspace folder which you have currently opened), Let's say only your source code is present
Program.cpp
# Other files...
Compile your program using the -g flag and create an executable as shown:
gcc -g Program.cpp -o a.out
If your compilation is successful, the executable a.out will be created in the folder, and your root folder will look like
a.out
Program.cpp
# Other files...
Next, you need to create your launch.json file. Since you are not using any Tasks extensions, you can choose the default configuration which will create the said file with most properties filled with their default values.
Now edit the program path in the launch.json to the path of this executable. I've also seen that there's a bug for some Linux OSes for VS Code's C++ debugger that does not work with the internal console. So you might also need to set the "external console" option as true
in this case, you'll make two edits in your launch.json file as
"configurations": [
{
#.... Other stuff
"program": "${fileDirname}/a.out",
#.... Other stuff
"externalConsole": true,
#.... Other stuff
}
]
}
That's all you need. Set breakpoints in your program, and start debugging using the button in the Debug Panel. An external console window will pop up running your program, and the breakpoints should be working.
Also if you're new to debugging in general, remember that every time you change your source code (Program.cpp in this case), you will need to compile and build your debuggable executable again. If you do not do so, your debugging would be glitchy.
Edit:
I see you want to add environment variables. I have a hunch on what you're doing wrong.
It seems to me that you want to set two variables such that
VAR1 = VAR1_VALUE
VAR2 = VAR2_VALUE
Your syntax for this in your launch.json file is incorrect. It should be as shown:
#... Other stuff
"cwd": "${fileDirname}",
"environment":
[
{
"name": "VAR1", "value": "VAR1_VALUE"
},
{
"name": "VAR2", "value": "VAR2_VALUE"
}
],
"externalConsole": false,
#... Other stuff
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"
],
}
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}"
}
]
}