HaoQChen
HaoQChen

Reputation: 141

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

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/ "
},

Upvotes: 14

Views: 3154

Answers (4)

starball
starball

Reputation: 50014

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.

Upvotes: 0

pktiuk
pktiuk

Reputation: 270

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"
        }
    ]
}

Upvotes: 1

TOPKAT
TOPKAT

Reputation: 8638

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.

Upvotes: 1

rickstaa
rickstaa

Reputation: 520

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

Upvotes: 4

Related Questions