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.
Related
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.
Specific lines of code in vscode can be linked to via application url as documented here:
https://code.visualstudio.com/docs/editor/command-line#_opening-vs-code-with-urls
It would be nice to be able right-click on a line of code and get the url in your clipboard. I can't seem to find if there is an existing extension for that feature, wondering if anyone knows.
The use case I can think of is pasting the url into a task manager so when it comes time to work on that line of code, you can easily jump right to it versus trying to remember the code file / LOC
you can use the following task to print the url to the terminal, you can copy it from there
{
"version": "2.0.0",
"tasks": [
{
"label": "file URL",
"type": "shell",
"command": "echo vscode://file${command:extension.commandvariable.file.filePosix}:${lineNumber}",
"problemMatcher": []
}
]
}
It uses the extension Command Variable to get the correct file separators on Windows (/). It only removes the : of the drive, because you don't want that in Cygwin and MSys.
You can try if this works in opening VSC with the file and line number
"command": "echo vscode://file${file}:${lineNumber}",
Every time I debug (using an external Terminal) it opens a new session.
Is it possible to reuse the same window / session?
Or if not to close the current one so it doesn't keep opening a new window?
Similar questions are related to the internal Terminal.
I'm using iTerm2 on macOS.
Here's my launch.json config:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File (External)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "externalTerminal",
"python": "/Users/josip/.virtualenvs/myproject-JnedzVU7/bin/python",
},
{
"name": "Python: Current File",
"type": "python",
// "justMyCode": false,
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
]
}
This problem only exists with iTerm, as everything works as expected with Terminal and the existing session is used for successive run/debug executions.
The difference in behaviour is due to the way the applescripts that send the debugging command to the external terminal are written. The script for Terminal (readable gist) contains logic for finding available session and telling the app to run the command with that session. Conversely, the script for iTerm (readable gist) is about as simple as can be for sending commands to run in iTerm.
While the Terminal script has functions for determining the right session and window to use, the iTerm script has only the following (excl. validation steps which are identical):
tell application "iTerm"
activate
set new_term to (create window with default profile)
delay 1
tell new_term
tell the current session
write text cmd
end tell
end tell
end tell
This is where the script is loaded and used to run the command in the configured terminal
I've scanned the iTerm scripting documentation and it should be possible to replicate the Terminal behaviour using the iTerm APIs if you want to make a feature request or submit a pull request with the change.
When debugging Python using VS Code, I am using windows terminal with PowerShell as my external terminal. For normal use, I have starting directory in PowerShell set as %USERPROFILE% (C:\Users\username) but when running python code I need to change the starting folder to %__CD__% (C:\Windows\system32) so that the current working directory would be the same as the python file I am running. (I found that this is the way it works thanks to testing.) This is necessary when working with files using python. However, I don't want to change the startingDirectory setting in PowerShell setting to %__CD__% due to practicality and personal preference.
So my question is, is it possible to temporarily set the startingDirectory setting for PowerShell from VSCode just to run the code? So that when i open WT for any other non debugging reason the starting directory is %USERPROFILE%.
These are my VS Code launch configurations:
"launch": {
"configurations": [
{
"name": "Python: Integrated Terminal",
"type": "python",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"console": "integratedTerminal"
},
{
"name": "Python: External Terminal",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "externalTerminal"
}
]
},
"terminal.external.windowsExec":"wt -p cmd cmd",
This is my PowerShell configuration:
{
"colorScheme": "One Half Dark",
"commandline": "powershell.exe",
"fontFace": "Consolas",
guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
hidden": false,
name": "Windows PowerShell",
startingDirectory": "%USERPROFILE%",
tabTitle": "PowerShell"
}
Thanks for any suggestions.
Thank you Michael Z and la-dispute. Posting your suggestions as an answer to help other community members.
You can try the following two ways to fix the issue:
open the folder containing your code and save it as a workspace. Then type in CTRL+SHIFT+P -> "Workspace Settings (JSON)", set the CWD for "Launch" within that workspace to the target directory
To configure the powershell profile type
$profile
New-Item -path $profile -type file –force
In the terminal window, it will give you a link for the PowerShell profile
location. Open that file and add
set-location "file path, for example C:\Windows\system32"
save the file and reload the vscode!
You can refer to Managing Current Location, How to quickly change shell folder to match the currently open file and vscode does not set current directory in the terminal if a powershell $PROFILE also sets current directory
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).