ROS Unsource script

Every little :turtle: knows how to source a ROS workspace, right? But what do they not tell you in the turorials? What they don’t want you to know??? How to unsource a workspace!

I’ve put together this bash script that does exactly that:

Example!

$ source ~/colonize_mars_ws/install/setup.bash
$ echo $LD_LIBRARY_PATH
# many entries

$ source unsource_ros.bash
$ unsource_ros
$ echo $LD_LIBRARY_PATH
# only your few initial entries!

$ source ~/colonize_venus_ws/devel/setup.bash
# now you can safely go on to colonizing Venus
# without the fear of dragging some Martians with you

It works both for ROS 1 and 2.

In ROS 1, it relies only on a list of known environment variables which it cleans. But if some packages install workspace setup scripts that also set other variables, there’s nothing that could be done (in ROS 1).

In ROS 2/colcon, thanks to the DSV files, the unsetting can work much more reliably. So, at least theoretically, in ROS 2, the unsource should be complete. However, crawling all DSV files can be slow, so this functionality has to be enabled via the --dsv option.


Somebody may ask why do I need this. Why I don’t just open up a new terminal. Yes, I could. However, one of the things the tutorials suggest (and I like it) is to put some sourcing into .bashrc. But hey, once you do that and you source, say, Kilted by default, how are you supposed to try Rolling? Or Jazzy? Hmm, edit your .bashrc? Ok, so ROS unsource is basically the answer. You can have a default ROS sourced, but you can also decide for something else and you should not carry all the burden from other distros with you!


The usual disclaimer: The script is provided as is. If you store your bitcoin wallet password in ROS_MY_WALLET_PATH, it will get smashed! There was some initial LLM-generated code I started from, but in the end I rewrote basically every part of it.

7 Likes

The asked why people needed to climb everest or go to the moon as well.

Thanks for the contribution!

3 Likes

“We choose to unsource a ROS workspace in this decade and do the other things, not because it was easy, but because it was hard”

– Jazzy Foxy Kilted

6 Likes

opening a new terminal is way too much work

but what about direnv?

jncfa@crabberry:~/projects$ echo "${ROS_DISTRO-no ROS distro}"
no ROS distro

jncfa@crabberry:~/projects$ cd myworkspace/
direnv: loading ~/projects/myworkspace/.envrc
direnv: loading /opt/ros/kilted/setup.bash
direnv: export +AMENT_PREFIX_PATH +CMAKE_PREFIX_PATH +LD_LIBRARY_PATH +PYTHONPATH +ROS_AUTOMATIC_DISCOVERY_RANGE +ROS_DISTRO +ROS_PYTHON_VERSION +ROS_VERSION ~PATH

jncfa@crabberry:~/projects/myworkspace$ echo "${ROS_DISTRO-no ROS distro}"
kilted

jncfa@crabberry:~/projects/myworkspace$ cd ..
direnv: unloading

jncfa@tankberry:~/projects$ echo "${ROS_DISTRO-no ROS distro}"
no ROS distro
1 Like