# \<PackageName\>\_ROOT vs \<Package\>\_DIR

**URL:** https://discourse.cmake.org/t/packagename-root-vs-package-dir/10361
**Category:** Usage
**Created:** [March 14, 2024, 10:16am UTC](https://discourse.cmake.org/t/packagename-root-vs-package-dir/10361 "2024-03-14T10:16:52Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![adaldev](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/9dc877/32.png) [@adaldev](https://discourse.cmake.org/u/adaldev)
#### Post date: [March 14, 2024, 10:16am UTC](https://discourse.cmake.org/t/packagename-root-vs-package-dir/10361/1 "2024-03-14T10:16:52Z")

</div>

Hi,

Probably a duplicate, but what is the difference between these two environment variables (regarding `find_package` behavior). I’m failling to understand from the documentation.

Regards,  
A.

---

<div class="post-metadata">

### Author: ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)
#### Post date: [March 28, 2024, 10:13am UTC](https://discourse.cmake.org/t/packagename-root-vs-package-dir/10361/2 "2024-03-28T10:13:21Z")

</div>

The difference is in the order they are searched. According to the [docs](https://cmake.org/cmake/help/latest/command/find_package.html#config-mode-search-procedure), `PackageName_ROOT` is used first. If it doesn’t exist/work, some CMake cache variables are tried next (`CMAKE_PREFIX_PATH` and some macOS-specific ones). If these don’t exist/work either, their environment variable equivalents are tried, along with `PackageName_DIR`.

`PackageName_ROOT` was also introduced in CMake version 3.12, while `PackageName_DIR` works in CMake older than that too.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [March 28, 2024, 9:37pm UTC](https://discourse.cmake.org/t/packagename-root-vs-package-dir/10361/3 "2024-03-28T21:37:16Z")

</div>

Actually, I think the docs are wrong. If `PackageName_DIR` is set, and there is a package file in that location, that will be used and no search will be performed at all. I’ll need to go through the code again more carefully to be sure, but the docs listing `PackageName_DIR` in step 3 of the search procedure looks to be an error.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [March 29, 2024, 7:56am UTC](https://discourse.cmake.org/t/packagename-root-vs-package-dir/10361/4 "2024-03-29T07:56:06Z")

</div>

I misread the post and responses as being about cache variables, not environment variables. @Angew’s answer is correct, at least about the search order.

There is one crucial difference between the `<PackageName>_ROOT` and `<PackageName>_DIR` variables (environment or cache). The `..._ROOT` variables are maintained as a stack while resolving `find_package()` calls. When there are nested calls, this allows the nested calls to see the parent locations as part of their search paths, so if the nested dependency is installed to the same place as the parent, it will be found. See policy [CMP0074](https://cmake.org/cmake/help/latest/policy/CMP0074.html) for details, which controls this “search stack” behavior. This does not occur with the `..._DIR` variables.
