Always source after you build

When creating new packages or modifying existing ones, many changes will not be visible by the system unless our workspace is re-sourced.

For example, if we try the following in the terminal window we used to first build this example package

ros2 run python_package_with_a_node sample_python_node

it will not work and will output

Package 'python_package_with_a_node' not found

As the workspace grows bigger and the packages more complex, figuring out such errors becomes a considerable hassle. My suggestion is to always source after a build, so that sourcing errors can always be ruled out.

cd ~/ros2_tutorial_workspace
colcon build
source install/setup.bash

Hint for the future you

In rare cases, the workspace can be left in an unclean state in which older build artifacts cause build and runtime issues, such as failed builds and programs that do not seem to match their intended source code. These artifacts might include old files that should have been removed, issues with dependencies, and so on. In those cases, it might be good to remove the build, install, and log folders before rebuilding and re-sourcing.

Hint for the future you

It might also be the case that certain packages fail to build after build, install, and log are removed, or that the build only works after colcon is called twice in a row. This is usually because the dependencies of the packages in your workspace are poorly configured and, in consequence, ROS2 is not building them in the correct order. If your workspace does not build properly after being cleaned as mentioned above, you must correct its dependencies until it builds properly.