How to get full autocompletion for ROS in your IDE.
Creates necessary configuration for VSCode and CLion (PyCharm) to get autocompletion working with ROS 2 (both C++ and Python packages).
In order for the C++ autocompletion to work correctly, you need to enable Compilation database generation for every build:
colcon build --cmake-args -DCMAKE_EXPORT_COMPILE_COMMANDS=1
Instead of having to write (and remember) --cmake-args -DCMAKE_EXPORT_COMPILE_COMMANDS=1
arguments over and over, you
can use a colcon mixin from
the default colcon mixin repository.
If you've never used colcon mixins before, you'll have to do an initial set up:
# 1. install colcon mixin plugin via apt
sudo apt install python3-colcon-mixin
# or via pip
python3 -m pip install -U colcon-mixin
# 2. add default mixins repository and download the mixins from it
# see https://github.com/colcon/colcon-mixin-repository/
colcon mixin add default https://raw.githubusercontent.com/colcon/colcon-mixin-repository/master/index.yaml
colcon mixin update default
Then, you can use the compile-commands
mixin like this:
colcon build --mixin compile-commands
It's a standalone Bash script. You can download it, place it anywhere, and run it (after you make it executable).
- Download
ros-ide-sync.sh
to~/bin/ros-ide-sync
and make it executable.mkdir ~/bin cd ~/bin wget https://raw.githubusercontent.com/pokusew/ros-setup/master/ide/ros-ide-sync.sh mv ros-ide-sync.sh ros-ide-sync chmod +x ros-ide-sync
- Restart your terminal. If you are running Ubuntu 20, you should have
~/bin
dir (and thusros-ide-sync
executable) on your PATH by default. You can verify it by attempting to runros-ide-sync
in an arbitrary dir:If that's not the case, you can easily fix it by adding the following at the end of theros-ide-sync
~/.bashrc
:export PATH="$HOME/bin:$PATH"
What's great on JetBrains IDEs:
- Autocompletion, linting, IntelliSense, Git integration, coding assistance and overall features are superior to other editors/IDEs.
What's not so good on JetBrains IDEs:
- The remote mode is very limited and does not work well.
There are two different situations to consider:
- ROS and JetBrains IDE are both running in the same system (OS).
- ROS is running in the remote/virtual machine (or in Docker container).
Further, ROS 2 setup is different from ROS 1 setup.
The best JetBrains IDE to use is CLion as it has also Python support (so it will work as well as PyCharm). However, CLion does NOT support remote Python interpreter and Jupyter notebooks (you can always use PyCharm for that).
C/C++: The best way (and practically the only way) is to use the CLion's support for Compilation database.
Python: The best way is to set Python interpreter to python.local.sh
(a wrapper executable script generated
by ros-ide-sync
script that fixes PYTHONPATH
).
Project setup:
- Install and read how to use ros-ide-sync
- Run
ros-ide-sync
in your project root. - Open your project root in CLion.
- Make sure the project is detected as a Compilation database project.
- Create a new System Interpreter. Set location to project's
python.local.sh
(a wrapper executable script generated byros-ide-sync
script that fixesPYTHONPATH
). Set this newly created interpreter as the project's Python interpreter.
All combinations of ROS 1 / ROS 2, local/remote are possible to use. I tried them all, but I haven't got time to document the setup (yet). In case you need help, feel free to contact me. Nevertheless, the remote mode in JetBrains IDEs is not good and for remote work I recommend VSCode.
- Official CLion Help > ROS 1 setup tutorial
- Official CLion Help > ROS 2 setup tutorial
- (not recommend, just for reference) CLion top level ROS2 Workspace CMakeLists
- (plugin for ROS 1, but also usable for ROS 2) https://github.com/duckietown/hatchery
- https://www.reddit.com/r/ROS/comments/hkpgao/ros2_and_clion/
What's great on VSCode:
- It can be used in Remote mode over SSH.
- The Remote mode is a first-class feature and everything works really great.
- Compared to that, CLion's Remote mode is a very bad.
What's not so good on VSCode:
- Autocomplete, linting, IntelliSense, coding assistance, Git integration and overall features are way worse than what CLion offers.
That's currently the setup I use.
VSCode extensions:
- C/C++
ms-vscode.cpptools
- Python
ms-python.python
Project setup:
- Install and read how to use ros-ide-sync
- Run
ros-ide-sync
in your project root.- If there is no
.vscode/c_cpp_properties.json
in your project, the script will create a new one for Linux + ROS If there is already one, it won't get updated. - Note the
compileCommands
option. It tells VSCode to use the Compilation Database produced by colcon build. - Feel free to modify it. Especially
includePath
, as sometimescompileCommands
option is not enough.
- If there is no
- Open your project root in VSCode (or add it to your workspace using File > Add Folder to Workspace...).
${workspaceFolder}/.vscode/settings.json
python.autoComplete.extraPaths
- used by Python autocompletion (IntelliSense)
- however, ignored by linting tools (such as pylint) (even when they are invoked automatically under the hood by VSCode), see below for solution (.env file)
auto-managed by the ROS extensionit turned out that my solution is better, see below- the ROS extension will automatically detect the
devel/setup*
file (for both ROS 1 and ROS 2), - note! When
PYTHONPATH
changes, the whole VSCode must be restarted in order for run it and extract thePYTHONPATH
from it (and updatepython.autoComplete.extraPaths
) the ROS extension to detect the change. The setupdevel/setup*
file is evaluated only on the ROS extension's activation and then the PYTHONPATH is cached (even when using the action ROS: Update Python Path).
- the ROS extension will automatically detect the
- now it is automatically updated while running
ros-vsc-sync.sh
script (workspaces not directly in workspaceFolder are also supported)
${workspaceFolder}/.env
(can be changed via settingpython.envFile
)- respected by tools run by Python extension (e.g. pylint)
- does not affect VSCode's Terminal
- see https://code.visualstudio.com/docs/python/environments#_use-of-the-pythonpath-variable
- so the workaround is to run the
ros-vsc-sync.sh
script that creates.env
file with correctPYTHONPATH
- https://code.visualstudio.com/docs/python/environments#_use-of-the-pythonpath-variable
- https://stackoverflow.com/questions/43574995/visual-studio-code-pylint-unable-to-import-protorpc
- microsoft/vscode-python#2791
- ROS extension
ms-iot.vscode-ros
- actually it does not work well (at least for ROS 2)