Debug powershell in Visual studio code with parameters from command line - debugging

Using Powershell ISE I often set breakpoints in a script and start the script from command line. ISE then stops at the breakpoint and lets me debug from there. How do I do the same from Terminal in Visual Studio Code? Below is a script just to show you what I mean. Starting from a terminal I would write:
.\hello.ps1 -firstName "firstName" -lastName "theLastName"
But doing that from terminal it just starts a new window.
param(
[string]$firstName,
[string]$lastName
)
Write-Host "Starting test script"
try
{
Write-Host "Hello $firstName $lastName"
}
catch
{
Write-Error "Error. Exception: $_"
}
write-host "Script done"

To make the info from Eickhel Mendoza's link explicit - the solution is to set up a Run and Debug config file. This is created in .vscode/launch.json. The content for this case would be:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch Hello Script",
"type": "PowerShell",
"request": "launch",
"script": "${workspaceFolder}\\hello.ps1",
"cwd": "${workspaceFolder}",
"args": ["-firstName \"firstName\" -lastName \"theLastName\""]
}
]
}
Then just open the "Run and Debug" sidebar pane on VSCode (the play button with the bug or Ctrl+Shift+D), where you can run your launch script and any breakpoints you set will be hit as expected.

Maybe this might help... it involves modifying the launch.json file in your workspace.
https://github.com/PowerShell/vscode-powershell/tree/master/examples#passing-arguments-to-the-script

Related

Running vscode task that includes which fails

I have a task created that runs some unittests via bash_unit.
The bash unit script seems to fail based on its use of which.
If I replace:
CAT="$(which cat)"
and other which referances in bash unit to point to my local commands all runs great.
If I run bash)unit directly all good, but if i run it as a vscode task it fails.
I have simplified the task below to the minimum failure:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "run which",
"type": "shell",
"command": "/usr/bin/which which",
"problemMatcher": [],
"group": "test",
}
]
}
This produces the following:
The terminal process "/bin/bash '-c', '/usr/bin/which which'" failed to launch (exit code: 1).
Any ideas what is happening?

How to create a directory in Visual Studio Code tasks.json in windows?

I'm trying to write a task to create a directory using Visual studio Code tasks (in tasks.json) for windows users using the mkdir command, it's working well except when the folder already exists.
tasks.json
{
"label": "(release) create build directory",
"type": "shell",
"linux": {
"command": "mkdir -p ./build/release"
},
"windows": {
"command": "mkdir .\\build\\release", // Not working when folder already exists !
}
},
What I tried:
"command": "IF NOT EXIST .\\build\\release mkdir .\\build\\release"
But then I get the error:
At line:1 char:3
+ IF NOT EXIST .\build\release mkdir .\build\release
+ ~
Missing '(' after 'IF' in if statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingOpenParenthesisInIfStatement
If it is not possible to do it this way, is it possible to run this task by ignoring the exit code ? ( so that the tasks continue to build my project)
Environment:
Visual Studio code 1.40.2
Windows 10 Pro x64
I found a way of doing it by using cmd.exe with /C option (Run Command and then terminate)
{
"label": "(release) create build directory",
"type": "shell",
"linux": {
"command": "mkdir -p ./build/release"
},
"windows": {
"command": "cmd",
"args": ["/C", "if not exist .\\build\\release mkdir .\\build\\release"]
}
},

Why aren't my Visual Studio Code task executing?

I am struggling with getting my Visual Studios code task to work. The problem is that it seems like the task/.bat file is not being executed.
VS Code task configuration:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "My Label",
"type": "shell",
"windows": {
"command": "'c:\\Program Files (x86)\\path\\to\\the\\file.bat${file}'"
},
"presentation": {
"reveal": "always"
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
For testing purposes the file.bat contains:
echo "------"
echo %1
echo "------"
The output in Visual Studio Code terminal is:
> Executing task in folder User: 'c:\\Program Files (x86)\\path\\to\\the\\file.bat c:\project\file.abc' <
c:\\Program Files (x86)\\path\\to\\the\\file.bat c:\project\file.abc
Terminal will be reused by tasks, press any key to close it.
I expected that the ${file} argument/value to printed in the console. The problem is that nothing is printed. It doesn't matter if I intentionally make syntax errors in the bat file. From VS Code it seems like the .bat file is not executed at all.
The shell being used is PowerShell
Configuration:
"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
Thank you in advance!
T
You just have a slight syntax error using double quotes and single quotes together.
Change to:
...
"windows": {
"command": "c:\\Program Files (x86)\\path\\to\\the\\file.bat ${file}"
}
...

How to debug Unit Tests with Karma/Jasmine in Visual Studio Code?

I'd like to be able to debug unit tests in Visual Studio Code, but so far it has been a mixed bag.
My setup:
launch.json
{
"version": "0.2.0",
"configurations": [
         {
             "name": "Debug tests",
             "type": "chrome",
             "request": "attach",
             "port": 9222,
             "sourceMaps": true,
             "webRoot": "${workspaceRoot}"
         }
]
}
karma.config.js
customLaunchers: {
Chrome_with_debugging: {
base: 'Chrome',
flags: ['--remote-debugging-port=9222']
}
}
This does seem to work in a way, if I launch the VS Code debugger it appears to attach (bottom bar turns orange). If I make a change, Karma kicks in and the debugger, too - but it invariably pauses in zone.js (this is an Angular project by the way) without me interfering in any way:
If I hit 'Continue' it actually hits my breakpoint
and I can inspect some variables but not all of them,
For example, I can't see the value of actual passed into Jasmine's expect method.
So a) Why does the debugger always pause inside zone.js - the tested code is from a Redux reducer and is invoked outside of any Angular context, and b) What am I missing in regards to not being able to inspect local variables (which is a showstopper right now)?
In karma.conf.js I updated added debug option in your version.
customLaunchers: {
Chrome_with_debugging: {
base: 'Chrome',
flags: ['--remote-debugging-port=9222'],
debug: true
}}
launch.json
Add below snippet as launch configuration,
{
"name": "Debug tests",
"type": "chrome",
"request": "attach",
"port": 9222,
"sourceMaps": true,
"webRoot": "${workspaceRoot}"
}
Then triggered the tests using below command,
ng test --browsers Chrome_with_debugging
Use Visual Studio Code debug option "Debug tests" to get attached to UT. With this I am able to debug unit tests using breakpoints in "Visual Studio Code + Debugger for Chrome extension".

Visual Studio Code Task Argument

I'm trying to create some tasks in Visual Studio Code to run all the tests in my go project.
I usually execute the tests on the command line using:
go test ./...
In Visual Studio Code my tasks.json looks like this:
{
"version": "0.1.0",
"command": "go",
"tasks": [
{
"taskName": "build",
"isBuildCommand": true
},
{
"taskName": "test",
"isTestCommand": true,
"args": ["./..."]
}
]
}
So Build works fine (CTRL + SHIFT + B)
But when I try to run the tests (CTRL + SHIFT + T) the following error occurs:
go: unknown subcommand "./..."
It seems to be omitting the "test" param, but when I comment out the args it runs go test fine.
Any ideas?
THIS MAY BE A BUG
VSCode Reverse Args and Task as of v0.8.0
This may be a bug that still persists in the newer versions. As of v0.9.1 I have not had a chance to test. Prior to 0.9.1 at least one hack worked by reversing the task and it's arg as in the following example:
{
"version": "0.1.0",
"command": "go",
"tasks": [
{
"taskName": "build",
"isBuildCommand": true
},
{
"taskName": "./...",
"isTestCommand": true,
"args": ["test"]
}
]
}
It's hard to believe that this has still persisted until v0.8.0 so there may be a preferred solution that I have not discovered.
Here is a link to a prior post that deals with a similar issue:
Define multiple tasks in VSCode
Scroll down to my answer for more explanation.

Resources