VSCode - Can a task call another task? - visual-studio-code

Vscode Version: 1.19.3
I was wondering if there was a way for one task to call another, like the "preLaunchtask" but for regular tasks.
The reason is because when I want debug my code, I need to recompile my executable to the latest version so I have the "preLaunchTask" call the CMakeTask which then needs to call make, to make my executable.

You can make it depends on another task. Example:
{
"label": "secondTask",
"type": "shell",
"command": "<Your second task's command here>",
"dependsOn": [
"firstTask"
]
},
{
"label": "firstTask",
"type": "shell",
"command": "<Your first task's command here>"
}

Chaining tasks is possible. use "dependsOn": ['Build'] in task Deploy.
source

Overall
Yes, you can automatically call custom scripts for other languages, though not yet for C. From the task docs,
VS Code currently auto-detects tasks for the following systems: Gulp,
Grunt, Jake and npm. We are working with the corresponding extension
authors to add support for Maven and the C# dotnet command as well. If
you develop a JavaScript application using Node.js as the runtime, you
usually have a package.json file describing your dependencies and the
scripts to run.
For C (or other custom)
You'll want to define a custom task as in the build task group so that it's run there.
Not all tasks or scripts can be auto-detected in your workspace.
Sometimes it is necessary to define your own custom tasks. Assume you
have a script to run your tests since it is necessary to setup some
environment correctly. The script is stored in a script folder inside
your workspace and named test.sh for Linux and macOS and test.cmd for
Windows. Run Configure Tasks from the global Tasks menu. This opens
the following picker.
You can make an entirely arbitrary command as long as your system recognizes the binary to use, and it can be a powershell, bash, batch, etc script that calls your build steps in order. This could be a command listing other commands or you could simply add multiple arbitrary tasks to this build group.
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Run tests",
"type": "shell",
"command": "./scripts/test.sh",
"windows": {
"command": ".\\scripts\\test.cmd"
},
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal": "always",
"panel": "new"
}
}
]
}
As the default build task it is executed directly when triggering Run Build Task (Ctrl+Shift+B).
Task property notes:
label: The tasks's label used in the user interface.
type: The task's type. For a custom task, this can either be shell or process. If shell is specified, the command is interpreted as a shell command (for example: bash, cmd, or PowerShell). If process is specified, the command is interpreted as a process to execute. If shell is used, any arguments to the command should be embedded into the command property to support proper argument quoting. For example, if the test script accepts a --debug argument then the command property would be: ./scripts/test.sh --debug.
command: The actual command to execute.
windows: Any Windows specific properties. Will be used instead of the default properties when the command is executed on the Windows operating system.
group: Defines to which group the task belongs. In the example, it belongs to the test group. Tasks that belong to the test group can be executed by running Run Test Task from the Command Palette.
presentation: Defines how the task output is handled in the user interface. In this example, the Integrated Terminal showing the output is always revealed and a new terminal is created on every task run.

Related

Debugging NET Azure Functions project using an external terminal in VS code on Windows

The VS code Azure Functions extension automatically created a launch config for debugging my project locally.
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to .NET Functions",
"type": "coreclr",
"justMyCode": false,
"request": "attach",
"processId": "${command:azureFunctions.pickProcess}"
}
]
}
This works well. However, I'd like to use an external terminal to debug instead of the integrated one. According to this doc, there is an option for this case, but it does not work in my case. Still, this is supposed to work for others; but I'm told then the debugger must not attach to the process but instead launch it itself (type key in json). And I have not gotten this to work that way.
Do any of you have working debug config for the Functions/VScode/ExternalTerminal/Win combo?
Cheers
I tried to repro using this Kenichiro Nakamura site, got the same issue.
I have opened the external terminal and used the below commands to run the debug:
Creating a function in a new folder
mkdir debugmyfunction
cd debugmyfunction
func new -n debugtest --worker-runtime dotnet-isolated
There will be a list of triggers, I selected HttpTrigger and then run the command func start
3)
Set the breakpoint after the Main() function in the program.cs
As I'm using VS Code So after pressing F5, it asked me AzureWebJobsStorage is not configured, then I disabled it to false and run again.
or
Run this command by opening the another external terminal and moving to your project directory:
func start --dotnet-isolated-debug
By default, .Net Core is the process attached in my debug console.
Then it takes a bit of time and show the following
Output:

Visual Studio Code: Is it possible to add a task to tasks.json that reruns the previous task?

I want to add a task to my VS Code tasks.json file that can rerun the last task that I ran.
I know there is a command Rerun Last Task that I can use when I press F1 to Show All Commands. However, not every developer knows about typing F1 to show all commands, but they know about F7 for build tasks. That's why I want to add something to our baseline tasks.json.
Is there a way to somehow call the Rerun Last Task command in tasks.json? Or maybe there is a variable that has the last task run?
{
"label": "rerun last command",
"command": "${command:workbench.action.tasks.reRunTask}",
"type": "shell",
"problemMatcher": [],
},
That task will rerun the last task. You see you can make any command into a variable for use within launch.json or tasks.json with the form:
${command:some command ID here}
See https://code.visualstudio.com/docs/editor/variables-reference#_command-variables
That creates a "command variable" which can be used as an argument or as a command.

Add prefix to debug command in VSCode

I'm debugging a Python script. My configuration looks something like this:
{
"name": "debug script.py",
"type": "python",
"request": "launch",
"program": "/path/to/script.py",
"console": "integratedTerminal"
}
When running the script, I need to prefix it with an executable aws-access to give myself access to certain resources on AWS (otherwise I get Permission Denied errors):
aws-access python script.py
How can I add this prefix to the debug command?
Note this is easy to do when executing my code using the Code Runner plugin:
"code-runner.executorMap": {
"python": "aws-access $pythonPath -u $fullFileName"
}
It's a little less smooth than usual, but here's how to do it:
You'll need to have debugpy installed
To initiate the debugging, you'll need a separate function or script that waits for the debugger to attach. I have mine in a separate script that looks like:
import debugpy
debugpy.listen(5678)
debugpy.wait_for_client()
from foo import bar
bar.run()
Where bar.run() is what you're trying to debug.
You'll then need to specify a launch.json configuration for VSCode - you can create this yourself in the project directory that you're trying to debug under /.vscode/launch.json or create one from within VSCode
launch.json should look something like:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Attach Standard",
"type": "python",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5678
}
}
]
}
a general debugging flow being:
within the VSCode terminal, set up your aws credentials either with environment variables, or something like aws-vault (which in turn, will set environment variables)
from the same terminal, run the debug function or script e.g. python debug_script.py
run the function or script with debugging from the VSCode UI(again, I use a script as it makes this part easier and less invasive to your code)
It will respond to UI debug points set in VSCode, and also on debugpy.breakpoint() within the code. More importantly, it will use the same terminal session you've set your AWS environment variables in.
Another alternative is to run:
saml2aws exec --exec-profile [your-profile-name] --session-duration=3600 -- $SHELL
in your VS terminal. Upon subsequent debugging executions, you will stay authenticated (up to the session duration).

Is there a wa to execute PowerShell script then a Node script within (task.json) for Custom Task?

I am creating a Custom Azure DevOps Task to replace my task group. I have my custom task working for straight PowerShell now, but I am also trying to integrate a 'Standard' task (Publish Build Artifacts) within the task. To do this I have added the azure-pipelines-tasks repo as a submodule of my project and I am compiling the task that I need and copying the output to my task.
The way the script is designed to work is run some PowerShell actions then run the "Publish Build Artifacts" js script.
The problem is when I run Node "publishbuildartifacts.js" from within the PowerShell script, it cannot read the task.json file correctly to get the info it requires, even though its within the same folder.
The only way I have been able to get "publishbuildartifacts.js" to run correctly is a direct call within the task.json.
I'm ok with this approach except for I need to run my PowerShell script before this action.
The issue I have been having is when I have the PowerShell action and the Node action within the execution action it will only run he node component, and when I add "platforms": ["windows"] to the PowerShell component; only the PowerShell will be ran and not the Node.
How can I get them to run in the order specified and in series?
I have also tried the "prejobexecution" and "postjobexecution" but that is now how I want the task to work.
"execution": {
"PowerShell3": {
"target": "ABS-Report_tasks.ps1",
"platforms": [
"windows"
]
},
"Node": {
"target": "publishbuildartifacts.js",
"argumentFormat": ""
}
},
May i know which agent pool did you use for your pipeline? Azure Pipeline allows running cross-platform scripts.Please refer to this document.
There is another workaround for you. You can add a command in you powershell script to run publishbuildartifacts.js such as node publishbuildartifacts.js.
There is an approach to group tasks. Take a look at the info below, see the article for full details.
Visual Studio Code Tasks and Split Terminals
{
"label": "Run Server",
"type": "shell",
"command": "${config:python.pythonPath} manage.py runserver --noreload",
"presentation": {
"group": "groupServerStuff"
}
},
All tasks with the same group will open up as another split terminal
pane in the same terminal window. Very nice.
rather than start each task individually, is there a way to have tasks
"call" or "spawn" other tasks...
{
"label": "Run Server",
"dependsOn": [
"Run TCP Server",
"Run Django Server",
"Tail Log File"
]
},
{
"label": "Run Django Server",
"type": "shell",
"command": "${config:python.pythonPath} manage.py runserver --noreload",
"presentation": {
"group": "groupServerStuff"
}
},
{
"label": "Run TCP Server",
"type": "shell",
"command": "${config:python.pythonPath} scripts/tcp_server.py",
"presentation": {
"group": "groupServerStuff"
}
},
{
"label": "Tail Log File",
"type": "shell",
"command": "tail -f /tmp/logfile.txt",
"presentation": {
"group": "groupServerStuff"
}
},
Update for OP for
Interesting, but will it work for VSTS custom tasks within the execution block. The example is for VSCode. Because these are not tasks and not terminal windows I dont know how this would register in the series of execution I am trying to accomplish.
You did not indicate that this was for VSTS, and since you hit up this PowerShell Q&A, the assumption was VSCode.
Now, all that being said. I've never tried this in VS/VSTS proper, just VSCode. There are those using VSCode for VS builds.
There are several articles on the web on that topic as well. Yet, a quick search shows that task groups were added to VSTS.
See: Visual Studio Team Services: Create and use task groups
… though it's only showing how to do this via the GUI.

Skip VS Code terminal shell arguments for launch.json

In my workspace settings I have
{
"terminal.integrated.shellArgs.linux": [
"-c",
"yarn custom_shell"
],
}
which launches a custom shell that prompts for user input on startup.
When I create a launch.json config that launches using the integrated terminal my yarn custom_shell command will run and wait for input, causing the launch command supplied by VS Code to fail to run. This same issue occurs for extensions that launch a program in my integrated terminal.
Is there a way to launch the integrated terminal with terminal.integrated.shellArgs only when it is an interactive user shell, not a shell started by an extension or launch.json config?
I think a good solution is to keep the integrated shell to behave as expected and to use shell-launcher extension for hacking different shells in vscode (it might also save you the need to wait for user input in your custom shell):
"shellLauncher.shells.linux": [
{
"shell": "bash",
"args": ["-c yarn custom_shell"],
"label": "my_custom_yarn_shell"
}
]