How can I use the same / existing external terminal (session) when debugging? - visual-studio-code

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.

Related

Change current working directory in powershell from Visual Studio Code

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

Send messages to gdb in VS Code

Is there a way to send messages to gdb programmatically in tasks.json? I can send messages in the debug window, but I would like to automate this.
I am using a microcontroller and openocd. I would just like to do things like re-flash the chip through gdb (remote connection) without having to type commands out every time, and integrate gdb commands into my build tasks.
Pass commands to gdb with -ex arguments:
{
"label": "Gdb help",
"command": "gdb",
"type": "shell",
"args": [
"-q",
"-ex", "help",
"-ex", "quit"
],
"problemMatcher": []
}
But I would propose to write a command file (https://sourceware.org/gdb/onlinedocs/gdb/Command-Files.html). So you will be able to reuse it in many places. The controller's basic initialization could be interesting for debugging (launch.json file). Command file could be passed via -command=file argument.

VS Code task to open VS Code's simple browser with a url

I intend to write a task, that first runs a dev server command, and parallely opens VS code's Simple Browser (Command Pallette -> Simple Browser) to the url.
I dont need the task to dynamically get the url from the dev server.
I've managed to make task to open the Simple Browser using input variable
{
"version": "2.0.0",
"tasks": [
{
"label": "open simple browser",
"command": "${input:openSimpleBrowser}"
}
],
"inputs": [
{
"id": "openSimpleBrowser",
"type": "command",
"command": "simpleBrowser.show",
"args": ["https://xkcd.com"]
}
]
}
One thing that I didn't find the documentation of is to find the name of the command. I found the name simpleBrowser.show by chance in VSCode itself. Apparently, you can get the name of each command in the command palette by:
search the command in the palette first
select the Configure Binding cog icon, which will take you to Keyboard Shortcuts
in the search bar, the name of the command will be written, e.g. #command:simpleBrowser.show
you'll only need the name after : for input variable

Escaping wildcards in VStudio launch configuration broken?

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.

Add prefix to debug command in VSCode

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).