conan-io / docs

conan.io reStructuredText documentation
http://docs.conan.io
MIT License
104 stars 346 forks source link

[question] [conan2] [config] How do I specify a path containing "~" in global.conf? #3670

Open MattLichter opened 2 months ago

MattLichter commented 2 months ago

What is your question?

I'm on Conan 2.2.2, trying to find a workaround for conan-io/docs#3732 on Windows. I can bake certificates into a file and then reference it in global.conf.

However, on both Windows and Linux, Conan is very picky about the directory string. It doesn't recognize the meaning of the tilde:

core.net.http:cacert_path = ~/.conan2/cacert.pem

It also doesn't recognize environment variables:

core.net.http:cacert_path = ${HOME}/.conan2/cacert.pem

And relative paths don't work either (with both files in the same directory):

core.net.http:cacert_path = cacert.pem

If I manually type out the full path, then things work:

core.net.http:cacert.path = /home/foo/.conan2/cacert.pem

But this doesn't make for an easily generalizable system that works out of the box for all devs and CI systems.

I then thought about setting the configuration value from a bash script, letting bash expand the path. But I see that Conan2 no longer supports conan config set foo=bar. Between this and conan-io/docs#3732 I'm getting frustrated with the removal of critical features I was using in Conan 1.61.0.

Finally I tried looking into the Jinja2 API to see if I could construct a path that way, using the double-braces templating. But I got lost very quickly in that as I've never used it before.

Windows 10 and Ubuntu 22.04 Conan 2.2.2 gcc and msbuild

Have you read the CONTRIBUTING guide?

shoeffner commented 2 months ago

The global.conf is rendered as a Jinja template, so you can customize it in quite a few ways. Additionally, a few things are already exposed to the render environment (See config.py ): os, platform, distro, conan_version, conan_home_folder, detect_api.

While most of these are undocumented features and they might change in the future, you can use the power of the full os.path module.

Some examples:

core.net.http:cacert.path = {{ os.path.expanduser(os.path.join('~', 'cacert.pem')) }}
core.net.http:cacert.path = {{ os.path.join(conan_home_folder, 'cacert.pem') }}
MattLichter commented 2 months ago

@shoeffner Thank you. I had problems with the os.path functions, but ultimately this worked.

core.net.http:cacert_path = {{conan_home_folder}}/cacert.pem

I wish this were documented somewhere. I don't feel that Conan users should need to understand the internals of how a simple configuration file is formatted by some third-party Python package. This is not normal in my experience with Linux and Windows config files, and the ability to use ~ in Linux config files is practically universal.

memsharded commented 2 months ago

I wish this were documented somewhere. I don't feel that Conan users should need to understand the internals of how a simple configuration file is formatted by some third-party Python package. This is not normal in my experience with Linux and Windows config files, and the ability to use ~ in Linux config files is practically universal.

This is documented in https://docs.conan.io/2/reference/config_files/global_conf.html#configuration-file-template

The reason why this file has been made a jinja template is because users requested the possibility of having more power in conan configuration files (profiles too), including variables, if-conditionals and for-loops. It didn't make sense to invent a custom syntax for it, so we decided to use a popular template engine which is jinja2 in Python.

Could we consider the question responded and close it? (thanks very much @shoeffner!) Or maybe do you want to suggest some improvements to the docs? Thanks!

MattLichter commented 2 months ago

This is documented in https://docs.conan.io/2/reference/config_files/global_conf.html#configuration-file-template

I meant the {{conan_home_folder}} is not documented. Is there a place where such useful variables are listed? As someone who needs to modify global.conf, it would be very handy to know what options are available. I did read the documentation you linked, but I know nothing about Jinja I didn't find the Jinja documentation very helpful. Learning just Conan has been steep enough without getting into the weeds of templated config files.

We can close this ticket. I'd write a PR to update the documentation, but I don't even know enough to write something useful.

memsharded commented 2 months ago

It is, but it is true that very succintly:

The Python packages passed to render the template are os and platform for all platforms and distro in Linux platforms. Additionally, the variables conan_version and conan_home_folder are also available.

It is also true that most of the examples of jinja are in the profiles docs, not the global.conf, but the syntax is the same: https://docs.conan.io/2/reference/config_files/profiles.html#profile-rendering

We could add a couple of examples and links to the global.conf about this jinja syntax if that helps, moving this ticket to the docs repo, we can try to add something there, thanks for the feedback!