Can preLaunchTask and launch start within the same terminal in VSCode?
Asked Answered
A

4

13

I am debugging my CPP code with VSCode. I need to use a preLaunchTask to set my environment before my code run. So my code should run after preLaunchTask right in the same terminal. But it start in two different terminal now. How can I do with this?

And btw how can I start the process in the same terminal next time? Some process will start another terminal next time, I am confuse.

My preLaunchTask:

{
    "label": "source_setup",
    "type": "shell",
    "command": "source ./devel/setup.zsh && export ROS_MASTER_URI=http://localhost:11311/ "
},
Autotype answered 16/8, 2019 at 2:15 Comment(0)
H
4

As stated by @isidorn in this vscode GitHub issue this feature is currently not yet supported. In the meantime, people can achieve the desired behaviour by adding the following code to their .bashrc

# Source ros setup.bash if present
if [ -f '../devel/setup.bash' ]; then . "../devel/setup.bash";fi
Hwu answered 26/9, 2019 at 10:2 Comment(0)
H
1

I think you could just use envFile property.

In my case launch for debugging looks like this (this one is used by python, but it also needs sourcing ./devel/setup.bash before running):

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current ROS File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "envFile": "${workspaceFolder}/devel/setup.bash"
        }
    ]
}
Hitlerism answered 2/9, 2021 at 18:23 Comment(1)
The envFile property does not support loading multi-line environment variables like source does.Hamhung
B
0

If you don't want to manually switch to the terminal once the task has ran, here is a good workaround:

add this line to your launch.json config:

"internalConsoleOptions": "openOnSessionStart"

This will switch to the terminal view once the task script has finished running.

Bronny answered 10/6, 2020 at 14:56 Comment(0)
S
0

This request (with matching backing motivation for wanting to run a script/program to set up an environment for debugging) is tracked by issue ticket Add the ability to run arbitrary code to setup the environment before the debugger extension is called #79932, which is currently marked as out-of-scope. Still, I suggest that you give that issue ticket a thumbs up to show support for it. You can also subscribe to it to get notified about discussion and progress. Please avoid making noisy comments there like ones that just consist of "+1" / "bump".

Someone has described a workaround there where they add to their build task to run that environment setup script and dump the resulting environment to a file, and then reference that file in an envFile member of a launch config (if such a member is supported by the task schema).


Aside: this reminds me of some related CMake Tools questions: Source environment variables before running CMake in VS Code and Source environment script to automatically build CMake project in VSCode with Yocto SDK.

Sesquicentennial answered 22/8 at 19:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.