I have a project created with defined tasks, for other users to use in my institution.
But now one of my tasks needs a parameter that should be a user defined directory.
How can each user have a configuration file in their project that my tasks can read and get the directory they want the out put to go?
This is a example of my task :
{
"label": "Generate File",
"type": "shell",
"command": "cscript.exe",
"args": [
"//NoLogo",
"${workspaceFolder}/scripts/build.vbs",
"0",
"${output}",
],
"options": {
"cwd": "${workspaceFolder}"
},
The output should be a folder define by each of the users in a configuration or setting file they can put in their project.
Edit :
As Requested i tried to do this
inside my .vscode folder i created a file named settings.json
with this as content
{
"env": {
"dirLoc": "C:\\output\\"
}
}
and tried to use this variable in my tasks.json file
{
"label": "Generate File",
"type": "shell",
"command": "cscript.exe",
"args": [
"//NoLogo",
"${workspaceFolder}/scripts/build.vbs",
"0",
"${env:dirLoc}",
],
"options": {
"cwd": "${workspaceFolder}"
},
but still i did not get anything in the value, it came blank
You can use the extension Command Variable.
It has the command extension.commandvariable.file.content where you can read file content and use it in a variable in the task or launch.
The file can be plain text, key-value, json and yaml.
Related
We have a slew of folks doing development through the same GitLab repo. We are using VS Code tasks to execute internal commands. The main command is the same for everyone: internal_command on Windows and internalCommand on Linux.
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label" : "do it",
"type" : "shell",
"windows": {
"command": "internal_command"
},
"linux": {
"command": "internalCommand"
}
}
]
}
This works as expected.
Some users need/want to run a specific command before the main command. For example, one use wants to rename a file, another user wants to change an environment variable, etc...
We don't want to have multiple versions of .vscode/tasks.json cause that is a mess when pushing things to GitLab.
So I am wondering if there is a way to specify user specific tasks in the project's .vscode/tasks.json file?
You can with the help of the extension Command Variable it allows you to use the content of a file as a command in the terminal. The file can also contain Key-Value pairs or be a JSON file.
Say you store this userTask.txt or userTask.json file in the .vscode folder and add the file to the .gitignore file.
With the current version of the extension the file userTask.txt has to exist, I will add an option to supply alternative text in case the file does not exist. You can fill the file with a dummy command like echo No User Task
Set up your task.json like
{
"version": "2.0.0",
"tasks": [
{
"label" : "do it",
"type" : "shell",
"windows": {
"command": "internal_command"
},
"linux": {
"command": "internalCommand"
},
"dependsOrder": "sequence",
"dependsOn": ["userTask"]
},
{
"label" : "userTask",
"type" : "shell",
"command": "${input:getUserTask}"
}
],
"inputs": [
{
"id": "getUserTask",
"type": "command",
"command": "extension.commandvariable.file.content",
"args": {
"fileName": "${workspaceFolder}/.vscode/userTask.txt"
}
}
]
}
As, my title suggests I want to hard-code my output file name, instead of the default one, where it is named after the file.
This is my tasks.json file in VS Code:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-std=c++17",
"-stdlib=libc++",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$gcc"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
From this documentation https://code.visualstudio.com/docs/cpp/config-clang-mac, I found a method of doing it, i.e. by replacing "${fileDirname}/${fileBasenameNoExtension}" with a hard-coded filename (for example "${workspaceFolder}/myProgram.out"). But, this is not working for me, I still get the default one. Should I be changing someother configuration along with this?
This executable with file name fills the VS Code Explorer section with repeatitive stuffs and this creates confusion. I found some methods to hide files with certain extensions like .out, .json etc, but the issue is executable file in VS Code(Mac) doesn't have any file extension.
In tradition, we put launch.json and tasks.json in .vscode folder in order to make debugger works. Meanwhile, VS Code also support developer to put these 2 configurations into workspace by setting them in *.vscode-workspace. Here are the steps to reproduce:
Create new js project by npm init with all default parameters.
Create new index.js with 1 line of code: console.log('done');
Create new ts-sample.code-workspace file within the same root folder, the content should be like following:
{
"launch": {
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/index.js",
"preLaunchTask": "nodeversion"
}
]
},
"tasks": {
"version": "2.0.0",
"tasks": [
{
"label": "nodeversion",
"type": "npm",
"script": "nodeversion",
}
]
},
"folders": [
{
"path": "."
}
]
}
Open the workspace
Open index.js and press F5
Error dialog prompted our with the following error message: Could not find the task 'nodeversion'
Open OUTPUT Panel and Error message is shown as following:
Error: The npm task detection didn't contribute a task for the following configuration:
{
"label": "nodeversion",
"type": "npm",
"script": "nodeversion"
}
The task will be ignored.
For easy demo please clone the sample project from https://github.com/mannok/WorkspaceLaunchTaskDemo
Is this a bug of VS Code or something I have missed?
I am trying to define a VSCode task in tasks.json that would adapt to the specific architecture where VSCode runs. To do this, I want to get the architecture as uname --m (e.g. "aarch64" or "amd64"). My goal is to interpolate the output of uname into an environment variable like so
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "cmake",
"args": [
"-DMYLIB_INCLUDE_DIR=$MYLIB/include",
"-DMYLIB_LIBRARY=$MYLIB/lib"
],
"options": {
"env": {
"MYLIB": "${workspaceFolder}/mylib/${command:get_arch}"
}
},
}
]
In my case, I will have architecture-specific versions of mylib under mylib/aarch64, mylib/amd64, etc.
My attempt so far as been to define a second get_arch task used in the environment definition of MYLIB, that simply runs uname.
{
"label": "get_arch",
"type": "shell",
"command": "uname --m"
}
Of course, this task is not a proper command and so it isn't detected by VSCode and my build task fails. I checked out the documentation on variable substition, but they don't mention if it's possible to substitute a shell command. I guess this would be possible from within an extension, but I want to keep things as simple as possible.
This extension provides a way to launch arbitrary shell commands as a VS Code command:
"tasks": [
{
"label": "test_arch",
"type": "shell",
"command": "echo",
"args": [
"${MYARCH}"
],
"options": {
"env": {
"MYARCH": "${input:get_arch}"
}
},
"problemMatcher": []
},
],
"inputs": [
{
"id": "get_arch",
"type": "command",
"command": "shellCommand.execute",
"args": {
"command": "uname -m"
}
}
]
One disadvantage I discovered is that you have to press Enter once more when the result of the command is prompted in picker. Aside of this, this is the straightest way to implement what you want, and yet it can be utilized in many similar situations.
Alternatively, you can add pickString input with arch picker or create an extension that adds only single command GetArch.
If you don't want to press enter every time, you can add the option useFirstResult: true in the args section.
Could you please help me, how to setup environment variables in visual studio code?
Assuming you mean for a debugging session(?) then you can include a env property in your launch configuration.
If you open the .vscode/launch.json file in your workspace or select Debug > Open Configurations then you should see a set of launch configurations for debugging your code. You can then add to it an env property with a dictionary of string:string.
Here is an example for an ASP.NET Core app from their standard web template setting the ASPNETCORE_ENVIRONMENT to Development :
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/vscode-env.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart",
"launchBrowser": {
"enabled": true,
"args": "${auto-detect-url}",
"windows": {
"command": "cmd.exe",
"args": "/C start ${auto-detect-url}"
},
"osx": {
"command": "open"
},
"linux": {
"command": "xdg-open"
}
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
In the VSCode launch.json you can use "env" and configure all your environment variables there:
{
"version": "0.2.0",
"configurations": [
{
"env": {
"NODE_ENV": "development",
"port":"1337"
},
...
}
]
}
You can load an environment file by setting the envFile property like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${workspaceFolder}",
"envFile": "${workspaceFolder}/.env",
"args": [],
"showLog": true
}
]
}
Place the .env file in your folder and add vars like this:
KEY1="TEXT_VAL1"
KEY2='{"key1":val1","key2":"val2"}'
Further Reading: Debugging go in vscode with environment variables
I run vscode from my command line by navigating to the folder with the code and running
code .
If you do that all your bash/zsh variables are passed into vs code. You can update your .bashrc/.zshrc file or just do
export KEY=value
before opening it.
Could they make it any harder? Here's what I did: open system properties, click on advanced, add the environment variable, shut down visual studio and start it up again.
My response is fairly late. I faced the same problem. I am on Windows 10. This is what I did:
Open a new Command prompt (CMD.EXE)
Set the environment variables . set myvar1=myvalue1
Launch VS Code from that Command prompt by typing code and then press ENTER
VS code was launched and it inherited all the custom variables that I had set in the parent CMD window
Optionally, you can also use the Control Panel -> System properties window to set the variables on a more permanent basis
Hope this helps.
For C/C++ debugging this works for me (docs):
// Defined per configuration in launch.json
"environment": [
{
"name": "<env_name>",
"value": "<env_value>"
}
]
Since VS Code uses powershell in the terminal.
The powershell command is
$env:NAME='VALUE'
To learn more:
https://www.tutorialspoint.com/how-to-set-environment-variables-using-powershell
If you've already assigned the variables using the npm module dotenv, then they should show up in your global variables. That module is here.
While running the debugger, go to your variables tab (right click to reopen if not visible) and then open "global" and then "process." There should then be an env section...
As it does not answer your question but searching vm arguments I stumbled on this page and there seem to be no other. So if you want to pass vm arguments its like so
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "ddtBatch",
"request": "launch",
"mainClass": "com.something.MyApplication",
"projectName": "MyProject",
"args": "Hello",
"vmArgs": "-Dspring.config.location=./application.properties"
}
]
}