colcon-bash
colcon-bash is an extension for the colcon build system, primarily used in robotics development (e.g., ROS 2). It provides the functionality to generate Bash shell scripts (e.g., `setup.bash`) within colcon workspaces. These scripts are crucial for setting up the environment, allowing users to easily source packages and make their executables and libraries discoverable. The current version is 0.5.0, and it follows the colcon project's release cadence, often coinciding with ROS 2 releases or colcon-core updates.
Common errors
-
colcon: command not found
cause The `colcon` command-line tool is not installed or not in your system's PATH.fixInstall `colcon` and its common extensions: `pip install colcon-common-extensions`. -
No such file or directory: install/setup.bash
cause The `colcon build` command either failed, was not run, or did not produce the expected `setup.bash` file in the `install` directory. This can happen if the build failed or if `colcon-bash` was not active.fixEnsure `colcon build` completes successfully. Check the build output for errors. Verify that `colcon-bash` is installed (`pip show colcon-bash`) and that `colcon` is configured to use bash extensions (which it typically is by default). -
my_executable: command not found (after building)
cause You have built your package, but you have not sourced the `colcon` workspace's `setup.bash` file, which adds your executable's path to your shell's PATH variable.fixRun `source install/setup.bash` from your workspace root directory (`my_colcon_ws` in the quickstart example) to update your environment.
Warnings
- gotcha colcon-bash only generates Bash-compatible setup scripts (`setup.bash`). Users of other shells (like Zsh or Fish) will need to install corresponding colcon extensions (e.g., `colcon-zsh`, `colcon-fish`) to get native setup scripts for their shell.
- gotcha Sourcing multiple `setup.bash` files (e.g., from different `colcon` workspaces or ROS distributions) can lead to environment variable conflicts or unexpected behavior. The order of sourcing matters, and the last sourced environment typically takes precedence.
- gotcha After running `colcon build`, the new packages, executables, or libraries will not be immediately available in your shell. You must explicitly `source install/setup.bash` in your terminal to update the environment variables (like PATH, LD_LIBRARY_PATH).
Install
-
pip install colcon-bash -
pip install colcon-common-extensions
Imports
- PackageBashExtension
from colcon_bash.bash.package import PackageBashExtension
Quickstart
# Create a colcon workspace
mkdir -p my_colcon_ws/src
cd my_colcon_ws/src
# Create a minimal C++ package (CMake)
mkdir my_pkg
cd my_pkg
cat << EOF > CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(my_pkg NONE)
add_executable(my_executable my_executable.cpp)
EOF
cat << EOF > my_executable.cpp
#include <iostream>
int main() {
std::cout << "Hello from my_pkg!" << std::endl;
return 0;
}
EOF
cd .. # back to src
# Build the workspace using colcon (colcon-bash will generate setup.bash)
cd .. # back to my_colcon_ws
# Note: ensure colcon-bash is installed (e.g., pip install colcon-common-extensions)
colcon build --packages-select my_pkg
# Source the generated setup script to make the package discoverable
source install/setup.bash
# Run the executable from the environment
my_executable