I have the following in my workspace settings.json file:
"terminal.integrated.env.osx": {
"AUTH_TOKEN": "secret_XXXXXX"
}
However, when trying to pass this via a launch command (defined in launch.json):
{
"name": "Example: Query",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/examples/query.py",
"args": [ "${env:AUTH_TOKEN}" ]
}
The resulting command contains an empty string for the argument:
/usr/bin/env /.../.venv/bin/python /.../debugpy/launcher 58644 -- /.../examples/query.py ""
However, if I print the variable from within the script, it is set properly.
I believe there is an ordering issue, such that the launch.json commands are generated before the terminal environment is set up - resulting in empty vars. Any ideas how to propagate the env value to the command line?
Update: I have also tried using a .env file for the variables (rather than settings.json), but the result is the same.
Try using "env" in launch.json...
{
"name": "Example: Query",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/examples/query.py",
"args": ["${AUTH_TOKEN}"], // using var from env on args
"env": {
"AUTH_TOKEN": "XXXX",
"ENV2" : "XXX"
}
}
you can use envs from file too
{
// ...
"args": ["${AUTH_TOKEN}"],
"envFile": "${workspaceFolder}/local.env",
}
You can create a .env file, and then put the variable in there, and read it from the environmental variables in the program instead of it being an argument.
Related
I must be missing something very obvious here, but I cannot seem to get this to work.
I want to set an environment variable FOO to be available in the VSCode debug sessions started on the current file by hitting the Debug button in the top right corner
This one: .
I tried setting the env dictionary in the launch.json file like so:
{
// 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": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"env": {
"FOO": "BAR"
}
}
]
}
But when I try to read the variable in my code, is get a KeyError since the variable hasn't been set.
import os
print(os.environ["FOO"])
yields this:
Traceback (most recent call last):
File ".../save_model.py", line 74, in <module>
print(os.environ["FOO"])
File ".../lib/python3.10/os.py", line 679, in __getitem__
raise KeyError(key) from None
KeyError: 'FOO'
I have a simple Powershell script that accepts one parameter. That parameter will contain JSON.
param (
[Parameter(Mandatory = $true)][string] $parm1
)
Write-Output $parm1
The json I'm sending is extremely simple for debugging purposes (from vscode launch.json):
{
"type": "PowerShell",
"request": "launch",
"name": "PowerShell: Launch Script",
"script": "c:/myrepo/scripts/script1.ps1",
"args": ["{\"testKey\": \"testValue\"}"],
"cwd": "c:/myrepo"
}
When I run the script, I get an exception: Unexpected token ':' in expression or statement
How is this handled in Powershell?
When I open VS Code, the default terminal is PowerShell and the default path is PS E:\Research\GM\Articles\Modularity\Covariance network\Graph theory\Metric basics\Consensus clustering\clustering_programs_5_2.
Q1: How could I change the default path in PowerShell to C:\Users<UserName>? (red line below)
Q2: How could I change the default terminal from PowerShell to cmd? (yellow circle below)
#############################################################################
I followed Geeky's method which worked well. However, the default path is still E:\Research\GM\Articles\Modularity\Covariance network\Graph theory\Metric basics\Consensus clustering\clustering_programs_5_2 rather than something like C:\Users<UserName>:
Press Ctrl + Shift + P. Type "def" and the default terminal selection option pops.
Click on it and select your preferred terminal
Alternatively, add this in your settings.json file
"terminal.integrated.defaultProfile.windows": "Command Prompt",
If the following code exists already in your settings.json file or else add the following code also
"terminal.integrated.profiles.windows": {
"PowerShell": {
"source": "PowerShell",
"icon": "terminal-powershell",
"path": "C:\\windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
},
"Command Prompt": {
"path": ["${env:windir}\\Sysnative\\cmd.exe", "${env:windir}\\System32\\cmd.exe"],
"args": [],
"icon": "terminal-cmd"
},
}
I have a script which sets path variables. I have to run this script manually every time I start a new terminal instance.
I am looking for an automated way to run this script at startup for every terminal instance.
I have tried the below approach which doesn't work
{
"terminal.integrated.profiles.windows": {
"Command Prompt": {
"path": "cmd.exe",
"args": [
"-noexit",
"-file",
"h:\\all-languages-env.cmd"
]
}
},
"terminal.integrated.defaultProfile.windows": "Command Prompt",
}
I am expecting the script h:\all-languages-env.cmd to run at every terminal(Command Prompt) startup.
The script file is as below
#ECHO OFF
call devtools isSupported nodejs 16.13.0 || exit /B %ERRORLEVEL%
call devtools isSupported phantomjs 2.1.1 || exit /B %ERRORLEVEL%
set npm_config_registry=http://example.com
set npm_config_user_agent="npm/{npm-version} node/{node-version} {platform} {arch} | NDS %USERNAME% %COMPUTERNAME%"
set SASS_BINARY_SITE=http://example.com/node-sass
set SPAWN_WRAP_SHIM_ROOT=H:\.nodejs
set CHROME_BIN="C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
set npm_config_prefix=H:\.nodejs\npm\8
set npm_config_cache=H:\.nodejs\npm\8\cache
set npm_config_tmp=H:\.nodejs\npm\8\tmp
set NODEJS_16_13_0=H:\nodejs\16
set PATH=%NODEJS_16_13_0%;H:\.nodejs\npm\8;%PATH%
type nul > H:\nodejs\16\lastUsed
set PHANTOMJS_2_1_1=H:\phantomjs\2\bin
set PHANTOMJS_BIN=%PHANTOMJS_2_1_1%\phantomjs.exe
set PATH=%PHANTOMJS_2_1_1%;%PATH%
type nul > H:\phantomjs\2\lastUsed
ECHO javascript environment set
cmd.exe uses different arguments
{
"terminal.integrated.profiles.windows": {
"Command Prompt": {
"path": "cmd.exe",
"args": [
"/K",
"h:\\all-languages-env.cmd"
]
}
},
"terminal.integrated.defaultProfile.windows": "Command Prompt",
}
in tasks.json I am using the "args" property to specify the arguments to pass to "command":"gulp". But when I run the task in vscode, only the first argument is being passed to gulp.
I want to run a gulp task against a single file. In gulpfile.js I am using the process.argv array to retrieve the command line arguments. So, on the command line I enter "gulp copy3 --file abc.js" and the copy3 task is run. The code then reads the argv array to get the name of the file being copied.
this code works from the command line. But does not work when I run it as a task in vscode. How to do that?
the gulpfile.js code:
gulp.task('copy3', function( )
{
console.log(process.argv) ;
let pattern = '*.js' ;
// single file to copy
if (( process.argv.length >= 5 ) && ( process.argv[3] == '--file' ))
{
let fileName = process.argv[4] ;
pattern = fileName ;
}
console.log('pattern:' + pattern ) ;
return gulp.src(pattern).pipe(gulp.dest('dev'));
}) ;
the tasks.json file
{
"version": "2.0.0",
"tasks": [
{
"taskName": "copy3",
"command": "gulp",
"args": [ "copy3", "--file", "${fileBasename}" ],
"problemMatcher": []
}
]
}
Here is the terminal output:
[10:52:57] Using gulpfile C:\vscTest\rpgproj\gulpfile.js
[10:52:57] Starting 'copy3'...
[ 'C:\\Program Files\\nodejs\\node.exe',
'C:\\vscTest\\rpgproj\\node_modules\\gulp\\bin\\gulp.js',
'copy3' ]
pattern:*.js
[10:52:57] Finished 'copy3' after 16 ms
thanks,
I made a couple of small changes, try:
{
"label": "Tasks: copy3",
"type": "shell",
"command": "gulp",
"args": [ "copy3", "--file", "${fileBasename}" ],
"problemMatcher": []
}
and your entire code works perfectly. Make sure to reload vscode after modifying the tasks.json.
VSCode appears to have a built-in gulp extension. This seems to scan your gulpfile for tasks and list them for you. It also seems to ignore the args option.
The workaround is to use the full path to gulp as the command e.g. ./node_modules/.bin/gulp to bypass it.