Variable names in vscode debugger are reduced to one letter instead of full variable names - visual-studio-code

I created a configuration in launch.json.
When I run using this configuration the variable names in my code are not used as variable names in the vscode debugging console.
Instead all variable names are reduced to one letter. For example a variable defined as const name in the code will show as the letter e in the vscode debugging console.
This also causes that I am unable to hover variables in the code to look for their value.
How come the variable names are reduced to one letter in the vscode debugging console?
Here is the launch config I use. Here it's used to run serverless functions locally. Though I suspect the issue is not related to that.
If necessary I can provide more info.
{
"type": "node",
"request": "launch",
"name": "Run one Serverless function",
"program": "${workspaceFolder}/node_modules/.bin/sls",
"args": [
"invoke",
"local",
"--function",
"nameOfTheFunction",
"--path",
"./myEvent.json"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"env": { "AWS_PROFILE": "myProfile", "SLS_DEBUG": "*" }
}

Related

How can I change the capitalization of the project directory VS Code passes when debugging?

I'm running a Next.JS project in VS Code. My launch.json has the following configuration:
{
// ...
"configurations": [
{
"name": "Frontend: Dev Server",
"request": "launch",
"runtimeArgs": [
"run-script",
"dev",
"--preserve-symlinks", // To debug linked
],
"runtimeExecutable": "npm",
"skipFiles": [
"<node_internals>/**"
],
"type": "node",
}
],
}
When I run this configuration from VS Code, I get an error like the following:
C:\Program Files\nodejs\npm.cmd run-script dev --preserve-symlinks
> web-client#0.1.0 dev
> next dev -p 5555
warn - Invalid casing detected for project dir, received c:\etcetc actual path C:\etcetc, see more info here https://nextjs.org/docs/messages/invalid-project-dir-casing
I know that I want VS Code to pass in the drive letter in upper case instead of lower case, but I don't see any option to set that and I tried opening the project using code C:\etcetc directly.
How can I change the capitalization of the directory that VS Code applies to the launch configuration?
Add a current working directory to your launch.json configuration:
{
// ...
"configurations": [
{
"cwd": "${workspaceFolder}",
"name": "Frontend: Dev Server",
// ...
}
]
}
This passed an upper case drive letter to Node for me. You may also want to check that VS Code is opening the folder with the right case, i.e. node C:\etcetc.
If you still have casing errors, try deleting the .next directory.

Use machine hostname inside VSCode launch.json file

I'd like to know if it's possible to have the launch.json file of VSCode using the hostname of my machine. To be more precisely, I'd like to use the hostname in the configurations.url properties, something like this:
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Launch",
"url": "https://<<hostname>>/",
"webRoot": "${workspaceFolder}"
}
]
}
As you can see, the url properties has <<hostname>>; though, I'd like that, when pressing F5, that <<hostname>> is automatically replaced with the machine hostname (e.g. DESKTOP-JUWKA3).
I've tried to look for answer on stackOverflow, as well as VSCode official site:
https://code.visualstudio.com/docs/nodejs/browser-debugging
https://code.visualstudio.com/docs/editor/variables-reference
I thought something was possible with the information in the second link, but unfortunately I haven't been able to do that (maybe I'm not capable of doing 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!

Launch.json: how to reference an environment variable

In order to define my environment variables in a single place a configured a task in which a run a shell script. The task is run as preLaunchTask in my launch.json.
In my launch.json I now try to reference the environment variables I configured in the script (like export AWS_REGION="eu-west-1").
The launch.json looks as follows:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
//..
"env": {
//"AWS_REGION": "us-east-1", //works
"AWS_REGION": "${env:AWS_REGION}", //doesn't work, why?
},
"args": [],
"preLaunchTask": "setupEnv",
}
] }
Doesn't work, why?
According to this post from user weinand...
The ".env" file is read and processed after VS Code has substituted
variables in the launch config. So your debugged program will indeed
see the environment variable "FOO" with the correct value but VS
Code's variable substitution in the launch.json will not see it.
The reason for this is that ".env" files are a node.js concept and not
a generic platform mechanism. So VS Code does not know anything about
.env files, but the node.js debugger knows about .env files.
... this functionality in launch.json is specific for applications running on Node.js, although that's not what M$ explains in their documentations for VSCode.
Possible solution
For Python applications (possibly for other platforms as well) environment variables defined in a .env file (or whatever name you like) will be available for your application as long as the following configuration is present in launch.json...
{
"version": "0.2.0",
"configurations": [
{
[...]
"envFile": "${workspaceFolder}/.env", // Path to the ".env" file.
[...]
}
]
}
Note that just exporting a variable...
export SOMEVAR_A=1234
... will not make the environment variable SOMEVAR_A available for the application being executed by the VSCode debugger nor for the settings - especially inside "env" and "args" ("configurations") - in launch.json as, for example, in this case...
{
"version": "0.2.0",
"configurations": [
{
[...]
"env": {
"SOMEVAR_A": "${env:SOMEVAR_A}"
},
"args": [
"${env:SOMEVAR_A}"
]
[...]
}
]
}
NOTE: In our tests the ${env:SOMEVAR_A} syntax did not work in any scenario. That is, didn't work for the application ("env") and didn't work for the settings ("args") in launch.json.
PLUS I: Dirt Hack
For values present in "args" ("configurations") you can use the hack below...
{
"version": "0.2.0",
"configurations": [
{
[...]
"envFile": "${workspaceFolder}/.env",
"args": [
"`source \"${workspaceFolder}/.env\";echo ${SOMEVAR_A}`"
]
[...]
}
]
}
... as the configuration in "envFile" doesn't work.
Notice, although, that the following construction...
[...]
"args": [
"`echo ${SOMEVAR_A}`"
]
[...]
... would also work for "args" as long as the environment variable "SOMEVAR_A" has been previously exported in the conventional way.
The same reasoning would work for a tasks (tasks.json), but in both cases we can't guarantee that.
TIP: An .env File Example
SOMEVAR_A="abcd"
SOMEVAR_B="efgh"
SOMEVAR_C=123456
PLUS II: Export Variables
There are cases where you will need to export variables (eg. export SOMEVAR_A="abcd") so that they can be consumed by certain resources. In these cases there may be problems, because the fact that we export variables prevents (we don't know why) that they are seen in the context of the "envFile" configuration "envFile": "${workspaceFolder}/.env".
A workaround to get around these limitations is to add set -a before the variables set and set +a after it. With this we were able to meet the two scenarios as this example...
#!/usr/bin/env bash
set -a
SOMEVAR_A="abcd"
SOMEVAR_B="efgh"
SOMEVAR_C=123456
set +a
... or in a more compatible and safe way use set -a/set +a as in this example...
[...]
"args": [
"`set -a;source \"${workspaceFolder}/.env\";set +a;echo ${SOMEVAR_A}`"
[...]
VSCode's support for environment variables is a mess! 🙄
Conclusion
We don't know if the limitations we are dealing with here are from VSCode's own design or are bugs. Anyway, it doesn't seem to make much sense.
These procedures were tested on Manjaro Linux (Arch based).
Thanks! 🤗
[Ref(s).: https://unix.stackexchange.com/a/79077/61742 , https://stackoverflow.com/a/30969768/3223785 ]
Looking at the issue comment quoted below, it seems this is currently not possible.
${env:...} only expands environment variables that were set in the parent shell that ran code. It doesn't expand variables set in the tasks.json env options.
https://github.com/Microsoft/vscode/issues/47985#issuecomment-460678885
It doesn't work as Eduardo Lucio stated. Here is some alternative that works at least on my case that sometimes uses env.sh file to load the environment variables and require .env file for VsCode debugging in Go project. To launch the application normally, load the env using commmand $ source env.sh. On this case, you want to load .env file instead.
Env-Example: Linux/WSL2
If the .sh is what I tought of, probably just some line of export commands: env.sh
export DBUrl="sql-connection-string-here"
export DBPass="somedbpass"
Create the prelaunch task to generate the .env file .vscode/tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "buildenv",
"command": "sed",
"args": ["s/export //g", "local_env.sh", ">", ".env"],
"type": "shell"
}
]
}
you can see that it calls sed to replace any export with empty string and rewrites a .env file.
On .vscode/launch.json, load the preLaunchTask and change the target envFile to the generated file:
{
"version": "0.2.0",
"configurations": [
{
"name": "My App Debug",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${workspaceFolder}",
"preLaunchTask": "buildenv",
"envFile": "${workspaceFolder}/.env",
}
]
}
Now, everytime VsCode run the debugger, it generates .env file and only need to maintain single env.sh file.
Reference: https://stackoverflow.com/a/38746951/12325366

Node debugging, make `${file}` dynamic?

With the right debug config file I can make VSCode run the currently focussed file through Mocha. However, I find it frustrating that if I am working on the actual code, rather than the spec file and I press F5, it tries to run the actual code as a spec file through Mocha.
So, my question is; given a file structure like this:
Folder
File.js
File.spec.js
And a debug config (.vscode/launch.json) like this:
{
// 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": "Unit Tests: Current File",
"program": "${workspaceRoot}/node_modules/.bin/_mocha",
"cwd": "${workspaceRoot}",
"args": [
"-u", "tdd",
"--timeout=999999",
"--colors",
"--opts", "${workspaceRoot}/mocha.opts",
"${file}" // I want to make this dynamic
],
}
]
}
Is it possible to get VSCode to debug the spec file whether the spec file (File.spec.js) or it's subject (File.js) are selected?
You can introduce a new dynamic variable by writing a simple extension that just defines one command, e.g. a smartFile command.
Then you can refer to that command in your launch config as ${command:smartFile}.
For the implementation of the command you can use everything available in VS Code extension API. So you can not only calculate a path based on your folder structure, but you can even pop-up UI. E.g. you could use QuickPick to select a test case from the list of all tests.