conan-io / conan

Conan - The open-source C and C++ package manager
https://conan.io
MIT License
8.13k stars 968 forks source link

[bug] conan_api.cache_folder contains path not to cache folder #16940

Closed lo1ol closed 6 days ago

lo1ol commented 1 week ago

Describe the bug

Hi!

I noticed, that in my custom command conan_api.cache_folder returns '/home/user/.conan2'. But In global.conf it set to another folder: "core.cache:storage_path=/conan/some_folder".

Is it Ok?

How to reproduce it

Add custom command:

from conan.cli.command import conan_command

try:
    from conan.cli.formatters.graph import format_graph_json as formater
except ImportError:
    # Old versions of conan uses this formatter,
    # but in new ones it's replaced by format_graph_json
    from conan.cli.commands.export_pkg import json_install as formater

@conan_command(group="Custom commands", formatters={"json": formater})
def print_cache(conan_api, parser, *args):
    print(conan_api.cache_folder)
CONAN_HOME=$(conan config home)
echo "core.cache:storage_path=/conan/some_dir" >> "$CONAN_HOME/global.conf";
conan print_cache
memsharded commented 1 week ago

Hi @lo1ol

There are 2 different concepts here:

The core.cache:storage_path conf changes the second one, the package storage, but not the Conan home/cache folder.

lo1ol commented 1 week ago

ok. So what is the proper way for getting package storage path? Something like this?

        config = ConfigAPI.load_config(conan_api.home_folder)
        cache_folder = config.get("core.cache:storage_path")

        if cache_folder is None:
            cache_folder = os.path.join(conan_api.home_folder,"p")
memsharded commented 1 week ago

The subapis shouldn't be instantiated, you should always use the object from the conan_api:

conan_api.sub_api.method()
conan_api.config.get(...)

Still your code above is not correct, the cache_folder will point in the second case (if storage_path is not defined) to the Conan home, while if storage_path is defined, it will return the "package storage", which is a different folder.

Also, some guidelines about the cache and package storage:

Not sure what you are aiming to do with the "storage_path" folder, but please note that everything there is an internal implementation detail (can change at any time in any future Conan version), and shouldn't be modified externally, that could be undefined behavior and fail in unexpected ways.

lo1ol commented 6 days ago

Not sure what you are aiming to do with the "storage_path" folder

We have several docker containers that could build project in parallel. All of them started on the same machine. Containers has the cache drive, that mounted to /conan_cache folder. We want to sync access to it via flock.

So, this flock has to be located in common space too.

I got your idea that this folder is read-only. I think, we will start to keep cache in /conan/cache folder and will create lock file in /conan folder