# sometimes find\_package() does not look for files in passed PATHS parameter

**URL:** <https://discourse.cmake.org/t/sometimes-find-package-does-not-look-for-files-in-passed-paths-parameter/8614>\
**Category:** Code\
**Tags:** os:linux\
**Created:** [July 27, 2023, 6:33am UTC](https://discourse.cmake.org/t/sometimes-find-package-does-not-look-for-files-in-passed-paths-parameter/8614 "2023-07-27T06:33:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zeroxia](https://discourse.cmake.org/user_avatar/discourse.cmake.org/zeroxia/32/2521_2.png) [@zeroxia](https://discourse.cmake.org/u/zeroxia)\
**Post date:** [July 27, 2023, 6:33am UTC](https://discourse.cmake.org/t/sometimes-find-package-does-not-look-for-files-in-passed-paths-parameter/8614/1 "2023-07-27T06:33:57Z")

</div>

Hello,

We’ve a repository running for a long while, recently, without any related changes, a spurious cmake error happened, only on some build environments.

The environment is Ubuntu 18.04. CMake version is 3.23.0 or 3.23.3. Compiler is GCC 7.5 or 9.

We have a toolchain file, for both x86\_64 build and arm64 build, where it sets:

```auto
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH) # host and install
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

```

When run cmake configure, we pass `-DCMAKE_FIND_ROOT_PATH=<cmake_install_prefix_value>` as every module build its package and install for later module to use.

We use Poco library from here:  
https:github.com/pocoproject/poco/blob/devel/cmake/PocoConfig.cmake.in

And the `PocoConfig.cmake.in` file content is exactly the same as the one we stored in our repo.

The call to use the Poco package is as follows:

```auto
find_package(Poco REQUIRED Foundation)

```

Occasionally, when building either x86\_64 natively or crossing building for ARM64, this error would happen:

```auto
CMake Error at .../install/lib/cmake/Poco/PocoConfig.cmake:30 (find_package):
  Could not find a package configuration file provided by "PocoFoundation"
  with any of the following names:

    PocoFoundationConfig.cmake
    pocofoundation-config.cmake

  Add the installation prefix of "PocoFoundation" to CMAKE_PREFIX_PATH or set
  "PocoFoundation_DIR" to a directory containing one of the above files. If
  "PocoFoundation" provides a separate development package or SDK, be sure it
  has been installed.
Call Stack (most recent call first):
  CMakeLists.txt:19 (find_package)

```

The relevant part from `PocoConfig.cmake` is as follows:

```auto
get_filename_component(_Poco_install_prefix "${CMAKE_CURRENT_LIST_DIR}" ABSOLUTE)

set(_Poco_NOTFOUND_MESSAGE)

# Let components find each other, but don't overwrite CMAKE_PREFIX_PATH
set(_Poco_CMAKE_PREFIX_PATH_old ${CMAKE_PREFIX_PATH})
set(CMAKE_PREFIX_PATH ${_Poco_install_prefix})

foreach(module ${Poco_FIND_COMPONENTS})
    find_package(Poco${module}
        ${_Poco_FIND_PARTS_QUIET}
        ${_Poco_FIND_PARTS_REQUIRED}
        PATHS "${_Poco_install_prefix}" NO_DEFAULT_PATH
    )
  # More omitted...

```

The overall `PocoConfig.cmake` and component file `PocoFoundationConfig.cmake` are at the same path.  
`PocoConfig.cmake` tries to set up some paths (like `CMAKE_PREFIX_PATH`), and also provide the parameter `PATHS` in the `find_package()` call with intention to let cmake find the `PocoFoundationConfig.cmake` file at the same path.

But on some build environment that I just can’t figure out the actual difference, above error would occur, and when in such environment this error happens, it can be reproduced always.

As the error happens, I tried the following actions, it can fix the issue:

1. Inside `PocoConfig.cmake`, add `set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE NEVER)`
2. Inside `PocoConfig.cmake`, add `unset(CMAKE_FIND_ROOT_PATH CACHE)`
3. In toolchain file, change `ONLY` to `BOTH`.

It seems that either of above options can help to fix the issue.

My question is:  
Under what circumstances, the `PATHS` parameter passed to `find_package()` is not used at all?

Because without above said “fix”, the build has been running for a long time under many people’s daily work environment and on Jenkins. But yesterday suddenly this error happened on two people’s work environment, I hope to find out the actual root cause of this failure.

Thanks!

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [July 29, 2023, 9:28pm UTC](https://discourse.cmake.org/t/sometimes-find-package-does-not-look-for-files-in-passed-paths-parameter/8614/2 "2023-07-29T21:28:38Z")

</div>

Does passing `--debug-find` to the configure step give more context?

---

<div class="post-metadata">

**Author:** ![zeroxia](https://discourse.cmake.org/user_avatar/discourse.cmake.org/zeroxia/32/2521_2.png) [@zeroxia](https://discourse.cmake.org/u/zeroxia)\
**Post date:** [August 25, 2023, 8:10am UTC](https://discourse.cmake.org/t/sometimes-find-package-does-not-look-for-files-in-passed-paths-parameter/8614/3 "2023-08-25T08:10:25Z")

</div>

Thanks for the hint. I don’t have the reproducing environment for now. I will try this flag to see if there is more info.
