current subfolder variable for tasks.json - visual-studio-code

How can i use the current folder name in tasks.json using VSCODE ?
related to the following variables-reference
example of file path: /home/your-username/your-project/folder/subfolder/file.ext
i want to get only the subfolder name.
there is a predefined variable called ${activeFolderShort} in the following documentation for window title settings. but it's not working in tasks.json file.
here is my task code:
{
"label": "reload",
"windows": {
"command": "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"
},
"args": [
"http://localhost:52945/${activeFolderShort}/${fileBasenameNoExtension}"
],
"problemMatcher": []
}

You can use the extension Command Variable
Use the variable
${command:extension.commandvariable.file.fileDirBasename}

Related

VS Code: Create file inside the current directory of the editor

Is it possible to create a new file inside the same folder as the file that is active in the editor by the time the keyboard shortcut ctrl+n for explorer.newFile is hit?
I can tell if it's the correct folder by looking at the breadcrumbs and mostly have the file explorer toggled off. The command, however, seems to create a file inside the last folder opened or focused by the file explorer.
You can do this by overloading that explorer.newFile command's default keybinding to run a task instead. Here is the keybinding:
{
"key": "ctrl+n",
"command": "workbench.action.tasks.runTask",
"args": "newFile",
"when": "editorFocus" // must have an editor focused
},
And the task (in tasks.json):
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "newFile",
"command": "touch '${fileDirname}/${input:fileName}' && code '${fileDirname}/${input:fileName}'",
"type": "shell",
"problemMatcher": [],
},
],
"inputs": [
{
"type": "promptString",
"id": "fileName",
"description": "Complete my file name.",
"default": "new file name"
}
]
}
That will create a file in the same directory as the active editor's directory - it will ask you for the filename - and then open it.
I used the bash command touch to create the file (if it doesn't already exist), if you are using a different shell you will need the equivalent of touch.

How do i setup GLFW in VS Code? GLFW Error

I am developing a project and in process i need to setup GLFW in VSCode. I am using MinGw as compiler and generated the glfw build files using cmake configuring for MinGw. I got the files and placed them in main c:/mingw . I placed glfw3.dll inside bin, libglfw3dll.a inside lib and GLFW/glfw3.h and GLFW/glfw3native.h inside include. "PS: i have placed these files in mingw as well as in my project". And then i add these lines inside tasks.json "-lglfw3" but at the execution terminal shows cannot find -lglfw and if i remove this then glfw and glad functions in main.cpp shows as error.
task.json
{
"version": "2.0.0",
"command": "g++",
"type": "shell",
"reveal": "always",
"tasks": [
{
"label": "Build",
"group": "build",
"windows": {
"suppressTaskName": true,
"args": [
"-g",
"--std=c++17",
"-I", "${workspaceFolder}",
"-I", "${workspaceFolder}/thirdparty/include",
/*===LIBS===*/
"-lopengl32",
"-lglfw3",
/*===END OF LIBS===*/
"main/main.cpp",
"-o",
"builds/windows/Stupefy",
]
},
"linux": {
"suppressTaskName": true,
"args": [
"-g",
"--std=c++17",
"main/main.cpp",
"-o",
"builds/x11/Stupefy"
]
},
"osx": {
"suppressTaskName": true,
"args": [
"-g",
"--std=c++17",
"main/main.cpp",
"-o",
"builds/osx/Stupefy"
]
}
}
],
}
Make sure that GLFW/glfw3.h is in your "thirdparty/include" folder. If it's not, add the path to that file as -I argument (or move it there)
Make sure that path to %your_glfw_folder%/lib-mingw-w64/glfw3.dll is specified by -L argument. (Or move it into more convenient location. As far as path to that file is specified by -L argument, you're good to go).
Make sure that libraries glfw3, opengl32, gdi32 are linked with -l parameters (you're missing -lgdi32)
For GLAD, make sure that path to its includes folder is listed as -I argument or its contents are in your "thirdparty/include" folder
Also add path to src/glad.c to your arguments list.

VScode remote development: How can I run a build task to source environment variables before running the real build task?

I want to setup VScode so I can use build tasks to build my project. The project is build using make, en I have defined a build task that runs make. However, before running make, I normally would source a script that sets my environment variables correctly. If I add a new build task with the source command, and set my main build tasks to first execute the source command, the environment variables are not propagated properly. How can I make sure the enviroment variables are kept between build tasks?
My tasks.json file:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "make",
"command": "make",
"args": [],
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"dependsOn": "Set environment"
},
{
"label": "Set environment",
"type": "shell",
"command": "source path/to/source_me.sh",
"group": "build",
]
}
Not in this way. Sourcing a file is injecting file contents into current shell session, which ends as soon as the task ends. The make task is run in a separate shell session, so these two do not interact. You may try to do a single task that executes a single line: source path/to/source_me.sh && make.
I solved my problem like this:
"tasks": [
{
"label": "all",
"type": "shell",
**"command": "source gitenv.sh && cd ${fileDirname} && alfred all"**,
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
Where alfred is a macro for make command with extra args.
${fileDirname} returns a current directory in which you have open file.
So if you open a file and in the same directory you have Makefile you can CTRL + SHIFT + B to execute this default task.

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

Using visual studio code and running tasks where path to .exe contains a space

I am following the walkthrough from the microsoft docs for using typescript in a vs code project. When I try and run the build task, the terminal comes up with
Executing task: c:\whatever\my path with spaces\Projects\ProjectName\node_modules.bin\tsc.cmd -p "c:\whatever\my path with spaces\Projects\ProjectName\tsconfig.json"
and the error
'c:\whatever\my' is not recognized as an internal or external command,
That is, the space in the folder name is confusing the task runner. I need something like
call "c:\whatever\my path with spaces\Projects\ProjectName\node_modules\.bin\tsc.cmd" -p "c:\whatever\my path with spaces\Projects\ProjectName\tsconfig.json"
How do I set up VS Code so the terminal recieves an input it can interpret with spaces in the directory name? Thank you
current tasks.json:
"version": "2.0.0",
"tasks": [
{
"type": "typescript",
"tsconfig": "tsconfig.json",
"problemMatcher": [
"$tsc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
Try using ''-s to escape the string containing the spaces:
in terminal: c:\whatever\'my path with
spaces'\Projects\ProjectName\tsconfig.json
in .json: "command":
"c:\\whatever\\'my path with
spaces'\\Projects\\ProjectName\\tsconfig.json"
You should use quotation marks \" (slash + quotation mark) for the entire command as in
"command": "\"c:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe\"",