When using VSCode and fish as the default user shell, I cannot overwrite the environment variables set by fish in VSCode launch configs.
Example:
in $XDG_CONFIG_HOME/fish/config.fish:
set -x FOO BAR
In .vscode/launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "My launch",
"type": "cppdbg",
"request": "launch",
"program": "/bin/sh",
"args": ["-c", "echo $FOO"],
"environment": [
{
"name": "FOO",
"value": "OVERWRITTEN"
}
],
"cwd": "${workspaceFolder}"
}
]
}
I expected this to print OVERWRITTEN, but I got BAR.
It seems VSCode is setting the environment variable, then running the user shell to run my program. Why do I have this behavior with fish but not bash for example? Is there a way to avoid using the user shell? What would be a good practice?
So far, the best fix I could get was to wrap my fish.config in a if status is-interactive.
The environment portion causes VSCode to launch the program (in this case, fish) with an environment variable set. The program may then overwrite that variable, which is what the set command does.
This will also occur with bash or any shell. The most likely reason you are not seeing this with bash is that bash only sources ~/.bashrc for interactive shells (docs on this). If you were to set $BASH_ENV to a file, bash would execute that and potentially overwrite environment variables in the same way.
To avoid overwriting the variable in fish, you can conditionalize it on whether you are interactive or not:
status is-interactive && set -x FOO BAR
alternatively you can detect if it's already set:
set -qx FOO || set -x FOO BAR
This second approach implements a "default" value for the variable, allowing the environment to take precedence.
Related
I'm trying to figure out how to create and use custom tasks.
I have been able to create a tasks.json file, but when I try to run the task from the command palette, nothing shows up.
My current .vscode/tasks.json file contains:
{
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "echo Hello"
}
]
}
I have also tried:
{
"version": "2.0.0",
"tasks": [
{
"label": "New Test Task",
"type": "shell",
"command": "echo 'hello new task'",
"windows": {
"command": "echo 'hello new task'"
},
"presentation": {
"reveal": "always",
"panel": "new"
}
}
]
}
And I have also tried the following combinations of folder/file:
.vs/tasks.json
.vs/tasks.vs.json
.vscode/tasks.vs.json
.vscode/tasks.json
Despite all of this, I keep getting a "No matching commands" error message. Is there something that I have overlooked or am doing wrong? Any guidance will be much appreciated.
Screenshot:
From the command palette, it requires 2 (or 3) steps/commands.
First, you need to get "Tasks: Run Tasks" in the command palette.
(By just entering "tasks", you should see it)
Then, after selecting that, it should list you all the tasks defined in your .vscode/tasks.json (yes, this is the correct folder/file combination as seen from the example in the Custom Tasks section of the VS Code docs). Specifically, all the label's defined in your tasks.json:
Now, depending on the task, it may prompt a third selection, like this one for your echo task:
If you want a single-step/command, you can use the other options of the Tasks: command group such as re-running the last run task or configuring a default task:
Or instead of the command palette, use a keybinding/shortcut to run your task: Is it possible to assign different shortcuts to different tasks in VS Code?.
you can add shortcut bindings to vscode's run task tasks.json by As in the following steps.
My OS environment is macOS
Use the shortcut "shift+command+P" to open the console
Then configure the shortcut key command as follows
{
"key": "f8",
"command": "workbench.action.tasks.runTask",
// "args": "echo", // You can also define your task label directly
"args": "${input}",
}
Finally, enter the shortcut key Use the shortcut key F8 to see the results of the task output
A bit simple but might help someone!
My colleague had by mistake clicked on Run Test Task and there nothing appeared. When clicking Run Task the stuff from .vscode/tasks.json appeared.
I want to test the wildcard treatment of my Python script. Therefore I want to handover a file path which contains wildcards e.g. data/*.xml.
If I call my script directly in the shell
my_script.py data/\*.xml
The escape of the wildcard works fine and the wildcard is seen by my script.
However, I'm not able to achieve this with my launch configuration of vscode.
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: my_script",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/my_script.py",
"console": "integratedTerminal",
"args": ["mine", "${workspaceFolder}/data/\\*.xml"]
}
]
}
This launch fails:
% cd /Users/tom/Documents/evaluate ; env /Users/tom/Library/python3.8/bin/python /Users/tom/.vscode/extensions/ms-python.python-2020.8.103604/pythonFiles/lib/python/debugpy/launcher 52992 -- /Users/tom/Documents/evaluate/my_script.py mine /Users/tom/Documents/evaluate/data/\\*.xml
zsh: no matches found: /Users/tom/Documents/evaluate/data/\*.xml
I tried several other variants to quote the wildcard without success e.g. the shell escape didn't work and the shell expanded the wildcard before the path is handed over to the script.
Any idea how I have to define the path properly in the "args:" of my launch configuration?
It really seems to be a bug. The escaping does not work as expected and differs depending on the used console.
Please refer the discussion in the related Github ticket.
I'm debugging a Python script. My configuration looks something like this:
{
"name": "debug script.py",
"type": "python",
"request": "launch",
"program": "/path/to/script.py",
"console": "integratedTerminal"
}
When running the script, I need to prefix it with an executable aws-access to give myself access to certain resources on AWS (otherwise I get Permission Denied errors):
aws-access python script.py
How can I add this prefix to the debug command?
Note this is easy to do when executing my code using the Code Runner plugin:
"code-runner.executorMap": {
"python": "aws-access $pythonPath -u $fullFileName"
}
It's a little less smooth than usual, but here's how to do it:
You'll need to have debugpy installed
To initiate the debugging, you'll need a separate function or script that waits for the debugger to attach. I have mine in a separate script that looks like:
import debugpy
debugpy.listen(5678)
debugpy.wait_for_client()
from foo import bar
bar.run()
Where bar.run() is what you're trying to debug.
You'll then need to specify a launch.json configuration for VSCode - you can create this yourself in the project directory that you're trying to debug under /.vscode/launch.json or create one from within VSCode
launch.json should look something like:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Attach Standard",
"type": "python",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5678
}
}
]
}
a general debugging flow being:
within the VSCode terminal, set up your aws credentials either with environment variables, or something like aws-vault (which in turn, will set environment variables)
from the same terminal, run the debug function or script e.g. python debug_script.py
run the function or script with debugging from the VSCode UI(again, I use a script as it makes this part easier and less invasive to your code)
It will respond to UI debug points set in VSCode, and also on debugpy.breakpoint() within the code. More importantly, it will use the same terminal session you've set your AWS environment variables in.
Another alternative is to run:
saml2aws exec --exec-profile [your-profile-name] --session-duration=3600 -- $SHELL
in your VS terminal. Upon subsequent debugging executions, you will stay authenticated (up to the session duration).
With VS Code 1.27.0 on Mac, I'm setting a breakpoint with Python debugger. I've setup a launch config to run Celery. Celery does stop in my code, if I move the breakpoint I can change what's logged before it stops. So it is stopping.
However the VS Code UI does not show that its stopped, and I cannot inspect anything or do anything. VS Code launches celery something like this:
cd /Users/bob/project ; env "PYTHONPATH=/Users/bob/.vscode/extensions/ms-python.python-2018.8.0/pythonFiles/experimental/ptvsd" /usr/local/bin/python3 -m ptvsd --host localhost --port 49650 /Library/Frameworks/Python.framework/Versions/3.5/bin/celery -A pipeline.app worker -l info -f worker.log
I'm unclear why it needs ptvsd and ports since it's not doing remote debugging. If I make a completely stand-alone script and run it with Python: Current File it debugs fine.
The launch.json entry is like:
{
"name": "Python: Celery",
"type": "python",
"request": "launch",
"program": "/Library/Frameworks/Python.framework/Versions/3.5/bin/celery",
"args": [
"-A",
"my_module.app",
"worker",
],
},
There is a specific debugger, celery.contrib.rdb
See if that makes it work
Vscode Version: 1.19.3
I was wondering if there was a way for one task to call another, like the "preLaunchtask" but for regular tasks.
The reason is because when I want debug my code, I need to recompile my executable to the latest version so I have the "preLaunchTask" call the CMakeTask which then needs to call make, to make my executable.
You can make it depends on another task. Example:
{
"label": "secondTask",
"type": "shell",
"command": "<Your second task's command here>",
"dependsOn": [
"firstTask"
]
},
{
"label": "firstTask",
"type": "shell",
"command": "<Your first task's command here>"
}
Chaining tasks is possible. use "dependsOn": ['Build'] in task Deploy.
source
Overall
Yes, you can automatically call custom scripts for other languages, though not yet for C. From the task docs,
VS Code currently auto-detects tasks for the following systems: Gulp,
Grunt, Jake and npm. We are working with the corresponding extension
authors to add support for Maven and the C# dotnet command as well. If
you develop a JavaScript application using Node.js as the runtime, you
usually have a package.json file describing your dependencies and the
scripts to run.
For C (or other custom)
You'll want to define a custom task as in the build task group so that it's run there.
Not all tasks or scripts can be auto-detected in your workspace.
Sometimes it is necessary to define your own custom tasks. Assume you
have a script to run your tests since it is necessary to setup some
environment correctly. The script is stored in a script folder inside
your workspace and named test.sh for Linux and macOS and test.cmd for
Windows. Run Configure Tasks from the global Tasks menu. This opens
the following picker.
You can make an entirely arbitrary command as long as your system recognizes the binary to use, and it can be a powershell, bash, batch, etc script that calls your build steps in order. This could be a command listing other commands or you could simply add multiple arbitrary tasks to this build group.
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Run tests",
"type": "shell",
"command": "./scripts/test.sh",
"windows": {
"command": ".\\scripts\\test.cmd"
},
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal": "always",
"panel": "new"
}
}
]
}
As the default build task it is executed directly when triggering Run Build Task (Ctrl+Shift+B).
Task property notes:
label: The tasks's label used in the user interface.
type: The task's type. For a custom task, this can either be shell or process. If shell is specified, the command is interpreted as a shell command (for example: bash, cmd, or PowerShell). If process is specified, the command is interpreted as a process to execute. If shell is used, any arguments to the command should be embedded into the command property to support proper argument quoting. For example, if the test script accepts a --debug argument then the command property would be: ./scripts/test.sh --debug.
command: The actual command to execute.
windows: Any Windows specific properties. Will be used instead of the default properties when the command is executed on the Windows operating system.
group: Defines to which group the task belongs. In the example, it belongs to the test group. Tasks that belong to the test group can be executed by running Run Test Task from the Command Palette.
presentation: Defines how the task output is handled in the user interface. In this example, the Integrated Terminal showing the output is always revealed and a new terminal is created on every task run.