I'm trying to debug a Spring bootRun application via VSCode. I'm not sure what the proper launch configuration is.
This is how I launch the program in a terminal
./gradlew bootRun -Dspring.profiles.active=local
These are the current configurations I've tried with no luck.
Launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Debug",
"args": [
"bootRun",
"-Dspring.profiles.active=local"
],
"mainClass": "com.test.Application",
"request": "launch"
},
{
"type": "java",
"preLaunchTask": "gradle",
"name": "Debug Task",
"request": "attach",
"hostName": "localhost",
"port": 5005
}
]
}
Tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "gradle",
"type": "shell",
"command": "./gradlew",
"args": [
"bootRun",
"-Dspring.profiles.active=local",
"--debug-jvm"
],
"problemMatcher": []
}
]
}
The "Debug" configuration spits out the following error
No active profile set, falling back to default profiles: default
The "Debug Task" configuration runs the task, but it waits until the task finishes which it never will. So I can't debug it.
EDIT 1:
So if I run this task
{
"version": "2.0.0",
"tasks": [
{
"label": "gradle",
"type": "shell",
"command": "./gradlew",
"args": [
"bootRun",
"-Dspring.profiles.active=local",
"--debug-jvm"
],
"problemMatcher": []
}
]
}
Then run this launch configuration
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "task 2",
"request": "attach",
"hostName": "localhost",
"port": 5005
}
]
}
I can debug the application, but this only attaches the debugger to the process. So I have to manually kill the process when I am done debugging. Ideally I would like to start and stop the application with vscode via a launch configuration.
EDIT 2:
I can achieve what I want in IntelliJ with this configuration, but I want to be able to do this in vscode.
EDIT 3:
This is my current configuration which works pretty well. I can start the program with CMD-SHFT-B then F5 to start the debugger.
Launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Debug",
"request": "attach",
"hostName": "localhost",
"port": 5005
}
]
}
Tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "gradle",
"type": "shell",
"command": "./gradlew",
"args": [
"bootRun",
"-Dspring.profiles.active=local",
"--debug-jvm"
],
"dependsOn": [
"kill-java"
],
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "kill-java",
"type": "shell",
"command": "pkill",
"args": [
"java"
]
}
]
}
You can achieve that by adding the following in .vscode/settings.json file:
{
"gradle.javaDebug": {
"tasks": [
"bootRun"
],
"clean": true
}
}
After saving the file, next to Run Task will apear the Debug Task option in Gradle - Gradle Tasks view:
You need to have the Gradle Tasks, Debugger for Java and Language Support for Java extensions installed.
It's worth taking a look at https://github.com/badsyntax/vscode-gradle which will make this easier for you. You can debug your app, and restart the debugging in one step after making code changes.
I just right click on the main Spring Boot Application class (ie. as annotated with #SpringBootApplication) and click "Debug". That will spin up the server and allow you to halt at breakpoints. Seems to work OK for me.
(I have the Microsoft Java Extension Pack Plug-in installed)
Related
I'm trying to setup my debug environment on VS Code to run and debug a MERN app.
I currently have this launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Node: Nodemon",
"runtimeExecutable": "yarn",
"runtimeArgs": [
"dev"
],
"outputCapture": "std",
},
]
}
It's working just fine, but with it I can only have breakpoints in the backend and not in the React app on the frontend.
The script yarn dev (package.json in the backend) runs both backend and frontend with concurrently:
Here's my scripts in package.json in the backend:
"scripts": {
"start": "node backend/server.js",
"server": "nodemon backend/server.js",
"client": "yarn --cwd frontend/ start",
"dev": "concurrently \"yarn server\" \"yarn client\""
}
What would be a working launch.json that would allow me to have breakpoints in the frontend as well?
One way to do this is to use VS Code's Multi-target debugging:
https://code.visualstudio.com/docs/editor/debugging#_multitarget-debugging
"Using multi-target debugging is simple: after you've started a first debug session, you can just launch another session. As soon as a second session is up and running, the VS Code UI switches to multi-target mode".
If you prefer to use launch.json, you can create a compound launch configuration, like this:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Node: Nodemon",
"runtimeExecutable": "yarn",
"runtimeArgs": [
"dev"
],
"outputCapture": "std",
},
{
"type": "node",
"request": "launch",
"name": "Client",
"program": "${workspaceFolder}/client.js"
}
],
"compounds": [
{
"name": "Server/Client",
"configurations": [
"Node: Nodemon",
"Client"
],
"preLaunchTask": "${defaultBuildTask}",
"stopAll": true
}
]
}
The configurations will be launched in parallel.
I am using VSCode to debug my Python files. I have two scripts, let's say a.py and b.py. These two files work with differents args. I would like to configure launch.json in such a way that, if I run a.py, it should input its corresponding args and same for other files. However, there is no if statement in launch file. And if I write two configurations, it just runs them sequentially. What should my launch.json look like? Here is my first attempt.
{
// 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": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${WorkspaceFolder}/src/data_processing/main.py",
"console": "integratedTerminal",
"env": {
"PYTHONPATH": "${workspaceRoot}"
},
"args": [
"/home/gokberk/Desktop/filtered_data",
"--gdc",
"gdc-client",
"--no-download",
"--manifest",
"/home/gokberk/Desktop/filtered_data/gdc_manifest.2021-05-26.txt",
"--source-slides-folder",
"/home/gokberk/Desktop/filtered_data"
]
},
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${WorkspaceFolder}/src/training.py",
"console": "integratedTerminal",
"env": {
"PYTHONPATH": "${workspaceRoot}"
},
"args": [
"--input-data-folder",
"/home/gokberk/Desktop/filtered_data",
"--alpha",
"0.2",
"--beta",
"0.2",
"--max-bag-size",
"50",
"--underlying-model-type",
"resnet18",
"--pretrained",
"--no-download",
"--source-slides-folder",
"/home/gokberk/Desktop/filtered_data"
]
}
]
}
I have an url in a test.txt file, and i want to use it in launch.json. I try many things without success, like setting a var in a task but it is impossible to use it in launch.json according what I've read (is it exact ?).
Exemple of code in launch.json I've tested, doesn't work (I am on macOS), I get the error "command shell not found" in VSCode when I launch the debugger (F5):
{
"inputs": [
{
"id": "DEBUG_URI",
"type": "command",
"command": "shell",
///"command": "cat test.txt",
"args": {
"command": "cat test.txt"
}
}
],
"version": "0.2.0",
"configurations": [
{
"name": "attach",
"request": "attach",
"type": "dart",
"preLaunchTask": "test.txt generator",
"observatoryUri": "${input:DEBUG_URI}"
}
]}
I've also tested "shellCommand.execute" (exemple here) instead of "shell" but it doesn't work...
thanks,
the "shellCommand.execute" comes from Tasks Shell Input extension, with this it works !! :
"inputs": [
{
"id": "DEBUG_URI",
"type": "command",
"command": "shellCommand.execute",
"args": {
"command": "cat test.txt",
"useFirstResult": "true",
}
}
],
"version": "0.2.0",
"configurations": [
{
"name": "attach",
"request": "attach",
"type": "dart",
"preLaunchTask": "test.txt generator",
"observatoryUri": "${input:DEBUG_URI}"
}
]}
with "useFirstResult": "true" you can avoid VSCode prompt
I have this launch.json running. Pressing F5, it first executes my Makefile (make) which produces the executable. I then simply like to run and get the terminal output until it's closed.
{
// 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 my app",
"program": "${workspaceFolder}\\myApp.exe",
"args": [
"-d",
"-v",
"-l",
"debug.log"
],
"preLaunchTask": "make",
"postDebugTask": "",
"cwd": "${workspaceFolder}"
}
]
}
While the tasks.json looks like this:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "make",
"type": "shell",
"command": "make",
"args": [],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal": "silent"
}
}
]
}
Sadly, VSCode always returns error
Cannot launch program 'C:\Users\Volker\src\myApp\myApp.exe'; setting
the 'outFiles' attribute might help.
I examined using ProcessExplorer and no one even tries to run the executable :-(
How to run the executable after it was build?
I want to use the VSCode debugger and want to set the environment variables before launching the app. The configuration in the Launch folder looks something like this.
{
"name": "Launch on iOS",
"type": "nativescript",
"request": "launch",
"platform": "ios",
"appRoot": "${workspaceRoot}",
"sourceMaps": true,
"watch": true,
"environment": [
{
"BUILD_ENV": "local"
}
]
}
This doesn't seem to work though. I am using a Mac.
Perhaps this works:
"env": {
"BUILD_ENV": "local"
}
or
"envFile": "${workspaceFolder}/.env",
or
"osx": {
"environment": [
{ "name": "",
"value": ""
}
]
},
See platform-specific properties.