VSCode Task passing an $Parameter to the command? - makefile

I'm asking because I don't know if it is possible.
But, I'm using VSCode to run a few makefiles.
My tasks.json:
{
"version": "0.1.0",
"command": "sh",
"args": ["-c"],
"isShellCommand": true,
"showOutput": "always",
"suppressTaskName": true,
"tasks": [
{
"taskName": "CTRL + SHIFT _ B",
"isBuildCommand": true,
"args": ["$make run-db"]
},
{
"taskName": "run",
"args": ["make -C ${fileDirname} run;"]
},
{
"taskName": "install",
"args": ["make -C ${fileDirname} install;"]
},
{
"taskName": "test",
"args": ["make -C ${fileDirname} test;"]
}
]
}
I'd like to know If it is possible to create just one run make line, instead of creating one method for each task in my makefile, I just pass the extra argumment in the vscode command pallet(EX: ctrl+p task make install)
{
"taskName": "make",
"args": ["make -C ${fileDirname} $Method;"]
},

you can probably use environment variable
"env": {
"NODE_ENV": "development"
},
as in this exemple

Related

VSCode: How to open two terminals on startup with different directories

I want on startup (on folder open) two terminals.
These should start with different directories and should be in split mode. Is this possible?
I have this currently in tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"presentation": {
"echo": false,
"reveal": "always",
"focus": false,
"panel": "dedicated",
"showReuseMessage": true
},
"tasks": [
{
"label": "Create terminals",
"dependsOn": [
"First",
"Second"
],
// Mark as the default build task so cmd/ctrl+shift+b will create them
"group": {
"kind": "build",
"isDefault": true
},
// Try start the task on folder open
"runOptions": {
"runOn": "folderOpen"
}
},
{
// The name that shows up in terminal tab
"label": "First",
// The task will launch a shell
"type": "shell",
"command": "/bin/bash; cd /var/fpwork/",
// Set the shell type
// Mark as a background task to avoid the spinner animation on the terminal tab
"isBackground": true,
"problemMatcher": [],
// Create the tasks in a terminal group
"presentation": {
"group": "my-group"
}
},
{
"label": "Second",
"type": "shell",
"command": "/bin/bash; cd /var/",
"isBackground": true,
"problemMatcher": [],
"presentation": {
"group": "my-group"
}
}
]
}
This opens two terminals in split mode, but how can I specify different folders for them?
The lines
"command": "/bin/bash; cd /var/fpwork/"
and
"command": "/bin/bash; cd /var/"
just seem to ignore the cd command. How to specify the terminal folders?
The thing that worked for me is
"command": "/bin/bash --rcfile <(echo '. ~/.bashrc; cd /var')"
See How to invoke bash, run commands inside the new shell, and then give control back to user?

how set a variable coming from reading a text file and use it in launch.json in VSCode?

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

How to simply run my executable after it was built?

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?

Compounds debugging in VS Code with delayed launch

I'm trying to launch multiple programs that need to talk to each other in the debugger in VS Code and created a launch.json with a compound that launches each of the executables. The programs launch simultaneously and all try to connect to the host at the same time. Is there any way in VS Code to explicitly set some sort of time delay between launch of each of the executables, say 250ms or so?
{
"version": "0.2.0",
"configurations": [
{
"name": "Host",
"type": "cppdbg",
"request": "launch",
"program": "/home/user/build/bin/host",
"args": [],
"stopAtEntry": false,
"cwd": "/home/user/build/bin",
"environment": [],
"externalConsole": true,
"linux": {
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
},
{
"name": "Node A",
"type": "cppdbg",
"request": "launch",
"program": "/home/user/build/bin/Node_A",
"args": ["ArgA", "ArgB", "ArgC"],
"stopAtEntry": false,
"cwd": "/home/user/build/bin",
"environment": [],
"externalConsole": true,
"linux": {
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
},
{
"name": "Node B",
"type": "cppdbg",
"request": "launch",
"program": "/home/user/build/bin/Node_B",
"args": ["ArgA", "ArgB", "ArgC"],
"stopAtEntry": false,
"cwd": "/home/user/build/bin",
"environment": [],
"externalConsole": true,
"linux": {
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
}
],
"compounds": [
{
"name": "System",
"configurations": ["Host", "Node A", "Node B"]
}
]
}
Yes, you can add a prelaunch task which will sleep for x seconds.
So say you have a client and server on Node.js and the server db connection takes longer to load this causes problems with the client.
Delaying the client debugger on vscode would work like this on a Mac OS X
First create a task in the same folder as the launch.json file called tasks.json which will build out a shell command before launching the client.
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Sleepdelay",
"type": "shell",
"command": "sleep 6",
"windows": {
"command": "ping 127.0.0.1 -n 6 > nul"
},
"group": "none",
"presentation": {
"reveal": "silent",
"panel": "new"
}
}
]
}
Add the following pretask to your launch.json file now to call the task
{
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Client",
"url": "http://localhost:9090",
"webRoot": "${workspaceFolder}/client/src",
"breakOnLoad": true,
"sourceMapPathOverrides": {
"webpack:///./src/*": "${webRoot}/*"
},
"preLaunchTask": "Sleepdelay"
//"runtimeExecutable": "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
},
{
"type": "node",
"request": "launch",
"name": "Server",
"program": "${workspaceFolder}/server/server.js",
"envFile": "${workspaceFolder}/server/.env",
"cwd": "${workspaceFolder}/server/"
}
],
"compounds": [
{
"name": "Server/Client",
"configurations": ["Server", "Client"]
}
]
}
The sleep command is available on Linux and MAC OS X. For Windows just use this hack in place of it:
ping 127.0.0.1 -n 6 > nul
Now you have a simple method to delay the launch of the client before the server.
Complementing Jason's answer
I was getting Error
OpenError: (:) [Out-File], NotSupportedException FileOpenFailure,Microsoft.PowerShell.Commands.OutFileCommand
On Windows (PowerShell Terminal), and have to change the ping hack command from:
"ping 127.0.0.1 -n 6 > nul"
to:
"ping 127.0.0.1 -n 6 > $null"

VScode compile C++ on windows the exe not found

I want to ask how to debug a simple hello world output from a C++ file, on the launch file I have to put the executable but I have only created a C++ file, how to compile it, I have tried everything, please help.
{
"version": "0.1.0",
"command": "g++",
"isShellCommand": true,
// compiles and links with debugger information
"args": ["-g", "-o", "main.exe", "main.cpp"],
// without debugger information
// "args": ["-o", "hello.exe", "hello.cpp"],
"showOutput": "always"
}
Pretty old question but here's a clear explanation for anyone in future.
Issue is that the debugger was looking for a.exe but your build file will probably be named different.
Changing the values of program variable to "${workspaceFolder}\\${fileBasenameNoExtension}.exe" will fix this issue. This should be in the tasks.json and the launch.json.
This env variable takes care that the right name is substituted. Now set a breakpoint and press f5.
Further details here and here.
Here's a full preview of a working launch.json
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "C:\\MinGw\\bin",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\MinGw\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
And a full preview of tasks.json.
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\MinGW\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
NOTE: As explained here and here, you might want to set "externalConsole": true if your code needs input from user.

Resources