How do I set my workspace folder in Visual Studio Code? - debugging

How do I set my workspace folder in Visual Studio Code?
This appears to be necessary for debugging, and I suspect it's why my breakpoints aren't getting hit.
Further reading:
This question about adding workspaces doesn't have an accepted answer.
What is a 'workspace' in VS Code?, while providing a wealth of information, does not explain how to set a workspace for debugging.
User and Workspace Settings. The documentation also does not mention how to set a workspace for debugging.

How do I set my workspace folder in Visual Studio Code?
Open Visual Studio Code. You should be on the Welcome page.
Add workspace folder...
File explorer opens. Select the folder you want for your workspace. Add.
Now your workspace folder is shown in the left pane :-)

Create a launch.json and set your workspace in "cwd"
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "${config:python.pythonPath}",
"program": "${file}",
"cwd": "*your/new/workspace/*",
"env": {},
"envFile": "${workspaceFolder}/.env",
"debugOptions": [
"RedirectOutput"
]
}
]
}

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.

I can't open terminal in Visual Studio Code

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)

How to properly debug an ASP.NET MVC Core with Visual Studio Code

I'm having difficulties debugging a MVC ASP.NET Core application with Visual Studio Code.
I'm opening the folder that contains the project I want to run in VS Code.
The relevant part of the .csproj file is the following:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UserSecretsId>aspnet-XXXXXXX-941AF9EA-C0DF-419D-B0F8-69FE3A477A65</UserSecretsId>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath>$(SolutionDir)Builds\Debug\</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
</PropertyGroup>
</Project>
Now the launch.json content is the following:
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "watch",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/Builds/Debug/XXXXXXX.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
// 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+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
Now, when I open a code file, set a breakpoint and choose to launch the app, the compilation takes place but the browser is not ran at the end...
More surprisingly, the output of the build goes to the terminal, not the debug console:
Executing task: dotnet watch run /Users/omatrot/Documents/bealink/bealink_server/XXXXXXX/XXXXXXX/XXXXXXX.csproj /property:GenerateFullPaths=true /consoleloggerparameters:NoSummary <
Then I press CTRL+C to stop the hosting process...
VS Code then immediately runs it again, this time outputting in the debug console, opening the browser, and stopping on my breakpoints.
This seems crazy to me.
EDIT 1: This works under Visual Studio by removing the following reference:
<PackageReference Include="Microsoft.Web.LibraryManager.Build" Version="2.1.113" />
You need read official doc first, and know we need add some extensions in vscode.
And you can read Jason Watmore's blog, In the blog, how to debug is introduced in detail. You can create task.json and launch.json. There are also video tutorials for our reference.

Disable "Failed to continue: Check debug console" error message in vscode

How do I get rid of this annoying message every time there's an error in my code?
I found the solution to this problem:
The error message isn't actually caused by VScode, it's caused by the golang extension.
To disable it you have to go to the extension manager and click the little gear next to the installed extension and revert it to a previous version from the context menu. The version I reverted to is from about a year ago.
If you don't see the little gear icon you need to have the extension installed first.
Add your main.go file path to "program" attribute located in 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": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "E:/GoProjects/main.go",
"env": {},
"args": []
}
]
}
Check the Go Lang extension settings and turn off the "Build On Save" feature.

How can I build an existing Windows-only Visual Studio project from Bash using WSL

I have an existing C++ project that I've configured and built in Visual Studio. This project's only target is Windows, no other platforms. I'm using Bash in WSL to launch the executable.
I prefer to develop in Visual Code (not Visual Studio). I prefer to build and launch applications through Bash (strong Linux background).
Right now, my development workflow is:
Edit code in VS Code
Switch to Visual studio and click the build button
Switch to Bash and execute the built program
Since I only keep Visual Studio open for building, I would much prefer to build by command line through Bash.
My naive approach was to use an open source tool to convert the Visual Studio project file into a CMake file. Then cmake & make from Bash, but I stopped when I started encountering errors looking for windows.h (maybe I just need to add some windows include paths to my include_path).
I'm not sure what the best way to go about this would be. Any suggestions would be appreciated!
If the project is entirely C++, there should be no reason to leave WSL. Building and launching the application can be easily handled right there!
You can absolutely build by the command line in bash by using
g++ -o <outputfile> <inputfiles>
However, the easiest way to run the program is to create a build configuration in Visual Code. You will need 2 files: launch.json and tasks.json
To create the launch file, hit F1 (or open your command pallet) and select Tasks: Configure Default Build Task. It should look something like this.
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}", //input files
"-o",
"${fileDirname}/a.out" //output file
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
To create launch.json, go to the 'debug' tab and select 'create a launch.json file'. It should look something like this
{
// 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}/a.out", //output file
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
with both of these files in place, all you have to do is hit the run button like in Visual Studio.
MSBuild.exe is provided with my installation of Microsoft Visual Studio. From within WSL bash, I can invoke MSBuild.exe and give the .sln file of my project as the first and only argument.
The compilation output is written to the terminal.

Resources