VS Code launch.json special symbols in args - visual-studio-code

How can i pass literal string to "args" section in launch.json console .net core app? The problem is with symbols '<' and '>'.
This is part of my launch.json file:
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/FileExtender/bin/Debug/net6.0/FileExtender.dll",
"args": [
"${workspaceFolder}\\UploadedHtml\\invoice-html-example.html",
"${workspaceFolder}\\UploadedHtml\\extend_section.html",
"</body>",
"true"
],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"console": "internalConsole"
},
I'm trying to pass string "</body>" literally to the program args, but VS Code says: "input or output cannot be redirected because the specified file is invalid".
If i use escape characters like this "\<\/body\>" then VS code underlines it as "Invalid escape character in string". However the Program is started, but in the args i get "/body". Symbols '<' and '>' disappear somewhere. Same happens if i use \\\ as escape chars.

Related

Piping VSCode output to another program

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.

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.

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.

vscode debug error. cannot connect to runtime

I want to debug the current .js file opened in vscode. Press F5 to run. Get error:"cannot connect to runtime. Make sure that runtime is in legacy debug mode."
The debug console shows error:
Debugging with legacy protocol because Node.js version could not be determined (Error: connect ECONNREFUSED 127.0.0.1:5858)
node.exe is in my path. And .vscode/launch.json contains type:node, request:launch, program:${file}.
Why the "cannot connect to runtime" error?
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${file}"
},
{
"type": "node",
"request": "attach",
"name": "Attach",
"port": 5858
}]