Piping VSCode output to another program - visual-studio-code

Frustratingly there is an old issue that has been closed in relation to this. The linked issues seem to resolve this for python extensions, but I am using Typescript. No comments on it being resolved in general when it was closed so I assume that there must be a way to achieve this.
I wish to use VSCode debugging with individual test files, but also pipe the final output to a different program. VSCode escapes the pipe operator...
launch.json
{
"type": "node",
"request": "launch",
"name": "tap-single",
"skipFiles": [
"<node_internals>/**",
"${workspaceFolder}/node_modules/**"
],
"program": "${file}",
"runtimeExecutable": "/home/user/.npm-global/bin/ts-node",
"runtimeArgs": [
"--transpileOnly",
],
"args": [
"|", // This is output as `\|` in the console...
"tap-arc"
],
"console": "integratedTerminal",
}
Partial output is below;
... /home/user/.npm-global/bin/ts-node --transpileOnly ./test/example.test.ts \| tap-arc
Debugger attached.

Related

VSCode launch configuration to run python file without debugging

I understand that by creating a launch.json file in VSCode (slightly more cumbersome than pycharm), I can set debug configurations to launch individual python files.
For example:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Poker",
"type": "python",
"request": "launch",
"cwd": "C:/Users/dickr/git/poker",
"program": "C:/Users/dickr/git/Poker/poker/main.py",
"console": "integratedTerminal",
"env": {"PYTHONPATH": "${workspaceFolder}${pathSeparator}${env:PYTHONPATH}"}
},
]
}
That all works great, but how can I run it in normal mode, without a debugger? How can this be defined in the launch configuration so I can select this in the dropdown, and potentially pass arguments with it?
Write a task in a tasks.json file. For documentation on tasks, see https://code.visualstudio.com/docs/editor/tasks#_custom-tasks.
Your might look something like this:
{
"version": "2.0.0",
"tasks": [
{
"label": "run python script",
"type": "process",
"command": "python3 C:/Users/dickr/git/Poker/poker/main.py",
"options": {
"cwd": "C:/Users/dickr/git/poker"
},
"presentation": {
"reveal": "always",
}
}
]
}
though I'd suggest that if possible, you use variable substitution to try to get rid of the absolute paths that won't be the same on other machines (assuming you want to be able to run the task on other machines).
Then run the task. You can even bind the task to keyboard shortcuts. See the documentation for more info: https://code.visualstudio.com/docs/editor/tasks.

VSCode launch.json's attribute 'skipFiles' not working as expected

Vscode debugger didn't skip node_internals. I don't know what i'm missing here. Below is my launch.json
{
// 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": "Build and run Project",
"program": "${workspaceFolder}/out/code/main.js",
"preLaunchTask": "npm: build",
"sourceMaps": true,
"smartStep": true,
"env": {"NODE_PATH": "${workspaceFolder}/out"},
"skipFiles": [
"${workspaceFolder}/node_modules/**/*.js",
"<node_internals>/**/*.js",
"/<node_internals>/**",
"**/<node_internals>/**",
"<node_internals>/internal/**",
],
"cwd": "${workspaceFolder}",
"outFiles": [
"${workspaceFolder}/out/**/*.js"
]
}
]
}
I tried all the possible glob patterns but debugger didn't skip the specified paths.
node and npm version
v14.16.0 and 6.14.11
code version(linux)
1.55.0
c185983a683d14c396952dd432459097bc7f757f
x64
Any help is appreciated. :-)
Windows or Linux? I've found that the skip files syntax is dependent on the platform and the / (Linux) or \ (Windows) that each requires. Also I don't believe you need the root locations for the folders e.g. /<node_internals>/
I've had success by providing both forms as I move between Windows and Linux frequently:
"skipFiles": [
"${workspaceFolder}/node_modules/**/*.js",
"${workspaceFolder}\\node_modules\\**\\*.js",
"<node_internals>/**/*.js",
"<node_internals>\\**\\*.js",
]
I do hope there's a simpler answer out there but I have yet to find it.

How to escape a comma in json passed as args in a VSCode launcher.json command?

I would like to debug an APP and need to pass a in-line json as arg.
I did the following:
"configurations": [
{
"name": "app DEV",
"program": "lib/main.dart",
"request": "launch",
"type": "dart",
"args": [
"--dart-define=APP_BACKENDS={[{\"id\":\"default\",\"url\":\"https://localhost\",\"port\": \"8080\"},]}",
]
}
but the APP_BACKENDS const value is getting stripped in the first comma. lock the print of it: {[{"id":"default"
just to be clear. that is the way I'm getting the value I've printed:
static const _APP_BACKENDS =
String.fromEnvironment('APP_BACKENDS', defaultValue: '{}');
After going through a lot of links and reading, finally I found a GitHub issue https://github.com/microsoft/vscode/issues/98471 which solved my similar problem. If you go through the link and read the second last comment it explains the things regarding shell quoting.
"configurations": [
{
"name": "app DEV",
"program": "lib/main.dart",
"request": "launch",
"type": "dart",
"args": [
"--dart-define=APP_BACKENDS={[{\"id\":\"default\",\"url\":\"https://localhost\",\"port\": \"8080\"},]}",
],
"argsExpansion": "none"
}
"argsExpansion": "none" after adding this to my launch.json file I was able to run the code without adding extra \ or spaces as mentioned in other answers. I tried searching for this configuration in vscode docs but did not find any reference, however it worked for me.
I had the same issue - trying to pass a json string as a command line arg to a python script. Works fine from shell, but couldn't do the same from launch.json. VS Code was intepreting the commas as multiple arguments and passing multiple -b args into my program (4x separate -b args in my case).
Answer: add a space " " to the end of the arg value.
I fail to understand why this works, but it does. Note I'm single-slash escaping the double-quotes.
{
"name": "Test my python code",
"type": "python",
"request": "launch",
"program": "${file}",
"args": [
"-r=/store/purchase",
"-b={\"items\":[\"cake\",\"coffee\",\"toilet roll\",\"masks\"]} "
],
"console": "integratedTerminal"
},
After reading this documentation: Launch JSON Reference, specifically
this reference, I got to know that, you need to use \\\ to achieve what you want. For example. In the documentation only this is given
JSON array of command-line arguments to pass to the program when it is launched. Example ["arg1", "arg2"]. If you are escaping characters, you will need to double escape them. For example, ["{\\\"arg1\\\": true}"] will send {"arg1": true} to your application
So, after looking at this, you must pass your JSON to your args in a proper format
args: [
"--dart-define=APP_BACKENDS={[{\\\"id\\\":\\\"default\\\",\\\"url\\\":\\\"https://localhost\\\",\\\"port\\\": \\\"8080\\\"}]}"
]
This will do your job!

Visual Studio Code Task is Prepending the working directory to the command

I'm trying to setup visual studio code for rust to do debugging. In my launch.json file I have
{
"version": "0.2.0",
"configurations": [
{
"name": "(Windows) Launch",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceRoot}/target/debug/vscode-rust-debug-example.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"preLaunchTask": "localcargobuildtask"
}
]
}
and in my tasks.json I have
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "localcargobuildtask",
"command": "echo hi",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Where "echo hi" is in my tasks I eventually want to have something like "cargo build" or "cargo test" or whatever. But this is just a starting step.
But when I press F5 the output I get is
> Executing task: C:\programs\VSCodeWorkspaces\RustProjects\vscode-rust-debug-example-debug-config-included\echo hi <
The terminal process terminated with exit code: 2
Terminal will be reused by tasks, press any key to close it.
Rather than running "echo" from where ever the terminal finds it (working directory first, then check global path variable like you would expect) it is actually looking for a program named "echo" in my root folder of the workspace.
How do I tell visual studio code "No I don't want you to run workspace/echo, I want you to run echo in workspace" ? Or is there another more direct way to tell visual studio code "compile this before you debug it" ?
The answer to this question How to debug Rust unit tests on Windows?
suggests that changing
"command": "cargo build",
into
"command": "cargo",
"args": [
"build"
],
in the tasks.json file works and somehow it does. Maybe the editor is configured to search the %path% for single word commands, or maybe some plugin I installed overwrote the "cargo" command. Maybe the reason echo wasn't found is because it is a terminal command, not a program. Maybe the error message was a lie and it was only reporting that it couldn't find workspacefolder\command despite checking %path%? Or maybe none of that. I have no idea. It is some pretty idiosyncratic behavior.

Where is stdout for VS Code?

I am running Node.js in VS Code. I see output of console.log in the Debug Window.
Where does process.stdout.write go to? I can't find it in the Debug Console or any of the Output windows.
My launch.json is simply this:
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/job.js"
}
]
}
Looking at issues with process.stdout.write the suggested fixes are adding these to your launch config:
"console": "internalConsole",
"outputCapture": "std",
Especially the outputCapture entry is important.
Make sure the Debug Console is visible:
Ctrl + Shift + Y
Can you try to add "console": "internalConsole" to your config and see if it works?
As per the docs these are the available options for the console:
console - what kind of console to use, for example, internalConsole,
integratedTerminal, externalTerminal