Debug Conan recipes in VS Code

coverage

Conan recipes are Python programs. As such, they can have bugs. Or it starts to become complicated when the build is, well, complicated 🙂

In such situations, having internal information about what is going on in a recipe during a build can become crucial. The self.output methods of the ConanFile class, or the python print function, might help to transport some information to the outside. But sometimes this is not enough. Running the Conan recipe in a debugger can help understand what is going on, which configuration values have been set or detected, and why a build runs as it does. Or does not run at all.

Setting up the debug launcher

I show the setup for VS Code. But you can also use PyCharm, or any other python development IDE or environment to do the same.

Running conan as a module

The actual conan command, when running conan, is

${PYTHON_INSTALL_DIR}/site-packages/conans/conan.py

The module is conans.conan. This can be verify by running

python3 -m conans.conan --help

Running this will show conan’s command line help.

Python module debug launcher

Knowing that we actually run a module, we can now create a debug launch configuration for the recipe we want to debug. The Python module debug configuration in VS Code might look like this:
(Imagine you are in the root Conan Center Index, and you work on the PACKAGE recipe)

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: PACKAGE",
            "type": "python",
            "request": "launch",
            "module": "conans.conan",
            "justMyCode": true,
            "args": [
                "create",
                "--profile",
                "default",
                "${workspaceFolder}/recipes/PACKAGE/all/conanfile.py",
                "PACKAGE/1.0@"
            ],
        }
    ]
}

The concept should be clear.

Just add the actual conan command, followed by any arguments,to the args array and the recipe can be debugged.

Looking at the right recipe file

Be aware that, depending on the conan action you execute, conan will export the PACKAGE conanfile.py file to

${CONAN_USER_HOME}/.conan/data/PACKAGE/1.0/_/_/export/conanfile.py

and run this version of the recipe.

Therefore, breakpoints needs to be set in the exported file! It can become confusing to work on one, but step through another, conanfile.py. Extra caution required!

There is a -ne, --not-export to not export the conan file option for some conan command, but that did not work for me. Conan complained that the conanfile.py could not be found when I used it. If you know any way to make -ne, --not-export work, please let me know.

Enjoy debugging your conan files :-)

In an optimal world recipes would not need to be debugged. Builds would be straight forward. But we do not live in such a world. There are some insane complex recipes which are more complex than some stand alone programs.

Therefore, when working with them, tooling is required. A debugger is a good tool.

Acknowledgement

A big thank you to Maciej Nowak from the cpplang slack conan channel who helped me getting started on that topic!