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

Viewed 913

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: '{}');
3 Answers

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!

Related