25

I am facing an issue with VS code debugger while trying to debug some angular typescript source code, and I think the reason is that some of those VS Code Variables have the wrong value - as suggested here.

I'd like to follow that advice, but I see no way how to query the VS code variables (e.g. display the current values of these variables for my project).

One of these variables is

${workspaceFolder}

They are used in VS code's config files, for this example in the launch.json file.

Do you know if there is a way to display those values? For example, log the values or show them in an alert window would just be sufficient for me to troubleshoot it.

Matt
  • 23,275
  • 15
  • 108
  • 168

1 Answers1

41

Old answer:

There may be a better way but you could run a

//  "preLaunchTask": "Echo vars" in your debug launch like:

{
    "name": "Chrome : Launch with sourcemaps",
    "type": "chrome",
    "request": "launch",
    "url": "http://localhost:3000",
    "webRoot": "${workspaceRoot}",
    "sourceMaps": true,
    "runtimeArgs": [
    "--remote-debugging-port=9222"
    ],
    "preLaunchTask": "Echo vars"
},

in your launch task and then in tasks.json add:

{
   "label": "Echo vars",
   "command": "echo",
   "args": [
     "${env:USERNAME}",
     "workspaceFolder = ${workspaceFolder}"
   ],
   "type": "shell"
},

Those values will be echoed to the terminal.



EDIT:

Because a later version of vscode now supports sending variables to the terminal this simpler keybinding will print out values in the terminal:

[
    {
        "key":  "alt+q",
        "command": "workbench.action.terminal.sendSequence",
        "args": {
            // "text": "echo ${env:USERNAME}",  // this works
            "text" : "echo file = '${file}' : workspaceFolder = '${workspaceFolder}'\u000D"
        }
    }
]

then Alt-q prints out the values.

The \u000D at the end is just a return.

Mark
  • 97,651
  • 19
  • 297
  • 324
  • 2
    That sounds good. Could you show me how to setup that launch task, please? Do you mean
    **Terminal -> Configure Tasks...** ?
    – Matt Nov 26 '18 at 17:26
  • 2
    Yep, that worked, thank you. After adding it to the tasks.json config, I ran Terminal -> Run Task... -> "Echo vars" and then "Never scan..." and it got displayed inside the terminal window. – Matt Nov 26 '18 at 17:40
  • 1
    I just meant you could add the task as a preLaunchTask right in your debug launch.json file. Just in case some variable values change when called through the debug launch rather than a straight task as you did. But both work. – Mark Nov 26 '18 at 17:51
  • I see, didn't think of that. Thank you for the hint! – Matt Nov 26 '18 at 17:53
  • 9
    Regarding the Edit: **Excellent hint, Mark!** For those who had difficulties finding it (like me): To **configure keyboard shortcuts** the way you want, open Keyboard Shortcuts editor (**File -> Preferences -> Keyboard Shortcuts**) and click on the **{}** button on the right of the editor title bar. See [here](https://code.visualstudio.com/docs/getstarted/keybindings#_advanced-customization) – Matt Jul 02 '19 at 07:52
  • I do not know where vscode gets the workspacefolder from. Therefore, can I change it permanent? – Timo Jun 08 '21 at 19:41
  • I get a result with `alt q`for `workspacefolder`, but when I debug a sh script with `bashdb` I get `unable to determine workspacefolder`. I am on Linux. – Timo Jun 08 '21 at 19:50
  • @Timo I assume the variable `${workspaceFolder}` is being resolved by vscode before it is sent to the terminal. Not by the shell or shell program itself. Are you `sendSequence` the whole `sh` script with the embedded variable? – Mark Jun 08 '21 at 21:07
  • What is `sendsequence`? I use the extension `bashdb` by `rogalmic` if that helps. – Timo Jun 09 '21 at 07:45